Count days date range within 12 date ranges

strooman

Active Member
Joined
Oct 29, 2013
Messages
329
Office Version
  1. 2016
Platform
  1. Windows
How many days of a given date range in C2:D2, fall within multiple date ranges in columns A2:B13?
I'm interested in formulas in column E2:E13 (count per period) and F2 (total count). I typed in the desired results in Green.

Help is much appreciated.

ABCDEF
1Start_Of_MonthEnd_Of_MonthStartEndCountTotal_Count
21-1-201431-1-201415-3-201427-7-20140133
31-2-201428-2-20140
41-3-201431-3-201416
51-4-201430-4-201430
61-5-201431-5-201431
71-6-201430-6-201430
81-7-201431-7-201426
91-8-201431-8-20140
101-9-201430-9-20140
111-10-201431-10-20140
121-11-201430-11-20140
131-12-201431-12-20140

<tbody>
</tbody>
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Peter,

I think your formula in F2 will return an incorrect result if you change say B7 to 20/6/14.
Andrew

Given the column B heading, I don't see how B7 could be that value.

If it could, then I would expect that A8 would become, 21/6/14 after which both our F2 formulas would again return the same value of 133.
 
Upvote 0
Peter

I wouldn't have proposed such a complex formula had I thought that start date was always end date plus 1. The specific question was "How many days of a given date range in C2:D2, fall within multiple date ranges in columns A2:B13?". For me this implies that the multiple date ranges need not be continuous.
 
Upvote 0
Peter

I wouldn't have proposed such a complex formula had I thought that start date was always end date plus 1. The specific question was "How many days of a given date range in C2:D2, fall within multiple date ranges in columns A2:B13?". For me this implies that the multiple date ranges need not be continuous.
Fair enough.

I was influenced by the combination of column headings, the actual sample dates given in columns A:B and the "12 date ranges" in the topic title. Posts #6 and #8 also referred to "months". Hence my suggestion.

Up to the OP to choose based on the actual circumstances.
 
Last edited:
Upvote 0
Peter,
I think your formula in F2 will return an incorrect result if you change say B7 to 20/6/14.

Peter
I wouldn't have proposed such a complex formula had I thought that start date was always end date plus 1. The specific question was "How many days of a given date range in C2:D2, fall within multiple date ranges in columns A2:B13?". For me this implies that the multiple date ranges need not be continuous.

Fair enough.
I was influenced by the combination of column headings, the actual sample dates given in columns A:B and the "12 date ranges" in the topic title. Posts #6 and #8 also referred to "months". Hence my suggestion.

Up to the OP to choose based on the actual circumstances.

Well, as often in this kind of situations there is no absolute solution. Depends on the situation were to use the best solution. I discovered that Peter's example works for dates and also for hours I think both solutions are thorough and fit my needs. Thanks gentlemen.

At the moment I try to figure out what the:
ROW(INDIRECT(C$2+1&":"&D$2-1))
from Andrew does.

Works flawlessly but when I highlight that part and hit F9 to evaluate and give the actual values it gives me an #REF error. But the whole function works.
 
Upvote 0
In Excel dates are just integers, as are rows. So that part of the formula returns an array of integers starting with the day after the first date and ending with the day before the last date. Each of those integers (dates) is passed in turn to the COUNTIFS function (returning 1 or 0) and the result is summed.

You can use the Evaluate Formula tool to see how it works.
 
Upvote 0
I think it's best to stay in the same thread because the new question is the same in general.

I was a little to quick on the draw when I praised the versatality of Peter's formula for even working with hours. It works when I use a large interval 05:00-23:00 in C2:D2 which spans all given intervals in A2:B5. But fails when I use 05:00-09:00 in C2:D2.

I tried to modify Andrew's formula but it fails on the:

ROW(INDIRECT(C$2+1&":"&D$2-1))

part, because when you calculate with hours, 00:00 is just the number zero and 24:00 is 1. So because there is no row zero it fails. I'm just curious how to handle the same question but then with hours. So the question is:

How many hours of a given interval in C2:D2, fall within the intervals in columns A2:B5?
I'm interested in formulas in column E2:E5 (count per interval) and F2 (total count). Again, I typed in the desired results in Green.

Help is much appreciated.

ABCDEF
1Start_TimeEnd_TimeStartEndCountTotal_Count
200:0006:0005:0023:0015
306:0007:001
420:0022:002
522:0024:001

<tbody>
</tbody>
 
Upvote 0
I don't understand your results, but does this work for you?


Excel 2010
ABCDEF
1Start_TimeEnd_TimeStartEndCountTotal_Count
200:0006:0005:0023:0017
306:0007:002
420:0022:003
522:0024:00:001
Sheet1
Cell Formulas
RangeFormula
E2=SUMPRODUCT(COUNTIFS(A2,"<="&(ROW(INDIRECT((C$2*24)+1&":"&(D$2*24)-1))/24),B2,">="&(ROW(INDIRECT((C$2*24)+1&":"&(D$2*24)-1))/24)))
F2=SUMPRODUCT(COUNTIFS(A2:A5,"<="&(ROW(INDIRECT((C$2*24)+1&":"&(D$2*24)-1))/24),B2:B5,">="&(ROW(INDIRECT((C$2*24)+1&":"&(D$2*24)-1))/24)))
 
Upvote 0
I don't understand your results, but does this work for you?
Unfortunately not.

05:00 - 23:00 has to be split up (divided) over the intervals. That said:

05:00 - 06:00 = 1 hour, that goes into interval 00:00 - 06:00
06:00 - 07:00 = 1 hour that goes into interval 06:00 - 07:00
07:00 - 20:00 = 0 because there is no interval for that period (doesn't have to show up in the results)
20:00 - 22:00 = 2 hours that goes into interval 20:00 - 22:00
22:00 - 23:00 = 1 hours that goes into interval 22:00 - 00:00 (24:00)
 
Upvote 0
I don't understand your numbers sorry. There are 3 hours between 20:00 and 22:00 (20:00, 21:00 and 22:00). Perhaps you meant to start at 22:01.
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,106
Members
449,096
Latest member
provoking

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