Calculate number of a specific day between two dates - KING OF EXCEL

Wednesday, January 8, 2020

Calculate number of a specific day between two dates

Calculate number of a specific day between two dates

Image
Answer:
=SUMPRODUCT(--(TEXT(ROW(INDIRECT(B1&":"&B2)),"DDDD")="Sunday"))
‘where B1 is start date; B2 is end date.
Excuse me? What it says?
Before we start, let’s remind you the fundamental basic about Date in Excel.

 “Excel stores dates as sequential serial numbers so they can be used in calculations. By default, January 1, 1900 is serial number 1, and January 1, 2008 is serial number 39448 because it is 39,448 days after January 1, 1900.” From Excel Help
In this sense, we can easily calculate the number of days between two dates by simple subtraction:
B1 = 01/04/2014
B2 = 12/04/2014
The difference will be:
= B2–B1
which gives you a result of 11.
You may also use DATEDIF function that gives you more options in excluding/including number of years/months.
How about if you want to know how many “Sunday” between two dates? There is no function for that. However, a combination of functions would lead you there. This is why Excel can be so powerful.
Here’s the formula:
=SUMPRODUCT(--(TEXT(ROW(INDIRECT(B1&":"&B2)),"DDDD")="Sunday"))

Let’s explore the formula inside out

B1&”:”&B2
Remember that 01/04/2014 and 12/04/2014 are actually a number of 41730 and 41741 respectively. By concatenating them with a semi colon, it gives “41730:41741″ as a result. However the result is a text string that cannot be used directly as a reference for the subsequent ROW function.
ROW(“41730:41741”) will not be accepted by Excel as the argument of ROW must be a reference.
Therefore we need to enclose the “41730:41741” with INDIRECT
INDIRECT(B1&”:”&B2) turns the resulting text string into a reference (41730:41741)
ROW(INDIRECT(“41730:41741”)) yields ROW(41730:41741)
ROW returns the row number of a reference. For example, ROW(A1) or ROW(1) returns 1.  ‘Note: Without column reference means whole row reference
  • If reference is omitted, it is assumed the position of the cell in which the ROW function is applied to.
  • If reference is a range of cells, it returns the rows number as an array.
In our example,
ROW(41730:41741) gives an array of
{41730;41731;41732;41733;41734;41735;41736;41737;41738;41739;41740;41741}
 This is the list of sequential serial numbers from the start date to the end date, isn’t it?
Text(Value,”DDDD”) turns the above array into another array of
{“Tuesday”;”Wednesday”;”Thursday”;”Friday”;”Saturday”;”Sunday”;”Monday”;”Tuesday”;”Wednesday”;”Thursday”;”Friday”;”Saturday”}
‘Note: Text function converts a value into a specific formatted text.  In our example, it converts Date (value) into corresponding Day (text). 
Now we basically have a list of “Days” between the two specified dates in an array.
By putting a logical text =“Sunday” at the end of the array
 {“Tuesday”;”Wednesday”;”Thursday”;”Friday”;”Saturday”;”Sunday”;”Monday”;”Tuesday”;”Wednesday”;”Thursday”;”Friday”;”Saturday”}=”Sunday”
It compares each value in the array with “Sunday” and returns FALSE if the value is not equal to “Sunday”; returns TRUE if it is equal to “Sunday”.
As a result, we have an array of TRUE or FALSE:
{FALSE;FALSE;FALSE;FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE} ‘Note: There is only one TRUE in the array as there is only one “Sunday” between 01/04/2014 and 12/04/2014
Putting double negative (- -) in front of the array to turn FALSE to 0; TRUE to 1
{0;0;0;0;0;1;0;0;0;0;0;0} ‘Tips: Applying other mathematical operations like +0 or *1 would yield the same result
The final step is to count the number of 1 in the array, which essentially means to have the SUM of the array. SUMPRODUCT does this job nicely as it can handle array formula.
=SUMPRODUCT({0;0;0;0;0;1;0;0;0;0;0;0})
gives you a result of 1, that means there is ONE Sunday between 01/04/2014 and 12/04/2014.
Now does the formula make sense to you?
=SUMPRODUCT(--(TEXT(ROW(INDIRECT(B1&":"&B2)),"DDDD")="Sunday"))
Please feel free to test the formula by changing the dates / day.
Tips:
  • To make the formula more flexible, do not hard copy the day you want to look at. E.g. input Sunday in A3, replace =“Sunday” in the formula with =A3.   In this way, you may count different days (Monday to Sunday) easily by changing the value in A3.
  • If you prefer short day, i.e. “Sun” instead of “Sunday”, replace “DDDD” in the formula with “DDD”.
#evba #etipfree #kingexcel
📤You download App EVBA.info installed directly on the latest phone here : https://www.evba.info/p/app-evbainfo-setting-for-your-phone.html?m=1

Popular Posts