SUMPRODUCT with COUNTIFS results in #DIV/0!

AOB

Well-known Member
Joined
Dec 15, 2010
Messages
669
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
I have a table of data which includes a timestamp column (date/time)

I need a formula to calculate the average number (count) of rows per week but only up-to and including a given date/time (so not the average across the entire dataset, but the average up to a certain point in the dataset)

I have additional calculated columns [Year] and [Week of Year] derived from the timestamp so that I can identify / group distinct weeks together

This formula works to calculate the average number (count) of rows per week across the entire dataset :

Excel Formula:
=COUNT(MyTable[Timestamp])/SUMPRODUCT(1/COUNTIFS(MyTable[Year],MyTable[Year],MyTable[Week of Year],MyTable[Week of Year]))

To incorporate the cutoff point, I tried this :

Excel Formula:
=COUNTIF(MyTable[Timestamp],"<="&[@[Cutoff]])/SUMPRODUCT(1/COUNTIFS(MyTable[Year],MyTable[Year],MyTable[Week of Year],MyTable[Week of Year],MyTable[Timestamp],"<="&[@[Cutoff]]))

But obviously this falls over because the COUNTIFS returns zeros in the array for those records which come after the [Cutoff] date, resulting in the 1/COUNTIFS having division by zero (#DIV/0!)

Any suggestions as to how I can get the average weekly count of records but only focusing on those records up to and including a given date and not the entire dataset?

Thanks!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Solved my own problem by eliminating the complexity of the SUMPRODUCT / COUNTIFS and replacing it with something simpler :

Excel Formula:
=COUNTIF(MyTable[Timestamp],"<="&[@[Cutoff]])/ROUNDUP(DATEDIF(MIN(MyTable[Timestamp])-WEEKDAY(MIN(MyTable[Timestamp]))+1,[@[Cutoff]],"d")/7,0)

To explain, for anybody who needs it...

To get the weekly average count of rows/records between two dates, I need two figures :
  1. The number of rows/records between those two dates
  2. The number of weeks covered between those two dates
And simply divide one by the other

The number of rows/records between the two dates is straightforward - the same COUNTIF numerator as before :

Excel Formula:
=COUNTIF(MyTable[Timestamp],"<="&[@[Cutoff]])

To get the number of weeks, you take the start date (which is simply the earliest date in the dataset) :

Excel Formula:
=MIN(MyTable[Timestamp])

Then determine the Sunday prior to that date :

Excel Formula:
=MIN(MyTable[Timestamp])-WEEKDAY(MIN(MyTable[Timestamp]))+1

Then determine the number of days between that Sunday and the arbitrary end date

Excel Formula:
=DATEDIF(MIN(MyTable[Timestamp])-WEEKDAY(MIN(MyTable[Timestamp]))+1,[@[Cutoff]],"d")

Then divide that number by 7 and round the result up to get the number of weeks in play :

Excel Formula:
=ROUNDUP(DATEDIF(MIN(MyTable[Timestamp])-WEEKDAY(MIN(MyTable[Timestamp]))+1,[@[Cutoff]],"d")/7,0)

Resulting in the formula above.

The reason I move the start date back to the previous Sunday is to get around the problem of a start date of, say, Friday, and an end date of the following Monday. Those dates are in two different weeks so the result should be 2 but the number of days between a consecutive Friday and Monday is only 3 so ROUNDUP(3/7) will only give you 1. By moving the start date back to the beginning of the initial week, you ensure you will count the number of whole weeks covered by the two dates, irrespective of the day of the week of either.

Hope this helps somebody...
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top