SOLVED

COUNTIFS between date/time ranges

%3CLINGO-SUB%20id%3D%22lingo-sub-2355774%22%20slang%3D%22en-US%22%3ECOUNTIFS%20between%20date%2Ftime%20ranges%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2355774%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20can't%20find%20an%20answer%20for%20this.%20I%20am%20trying%20to%20get%20a%20count%20between%20two%20time%20ranges%20and%20I%20can't%20get%20it%20to%20work%20as%20it%20should.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20times%20in%20two%20columns%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EStart%20time%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20End%20Time%3C%2FP%3E%3CP%3E00%3A01%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B01%3A00%3C%2FP%3E%3CP%3E01%3A01%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B02%3A00%3C%2FP%3E%3CP%3E02%3A01%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B03%3A00%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20so%20on%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20problem%20I%20am%20having%20I%20can't%20get%20the%20countifs%20formula%20to%20include%20the%20end%20time%20in%20the%20count.%20If%20the%20time%20in%20the%20main%20sheet%20is%2001%3A00%20for%20example%20then%20the%20count%20will%20come%20up%20as%200.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20formula%20I%20am%20using%20is%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETIFS('Main%20sheet'!%24D%242%3A%24D%2410%2C%22%26gt%3B%3D%22%26amp%3B%20A3%2C'Main%20sheet'!%24D%242%3A%24D%2410%2C%22%26lt%3B%3D%22%26amp%3B%20B3)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20attached%20the%20excel%20workbook%20if%20that%20helps.%20I%20am%20pulling%20my%20hair%20out%20and%20I%20know%20it'll%20be%20something%20simple%20that%20I%20am%20not%20doing!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20in%20advance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2355774%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2355868%22%20slang%3D%22en-US%22%3ERe%3A%20COUNTIFS%20between%20date%2Ftime%20ranges%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2355868%22%20slang%3D%22en-US%22%3EThe%20formula%20works%20for%20working%20out%20the%20count%20between%20the%20time%20periods.%20But%20say%20for%20example%20I%20have%203%20times%3CBR%20%2F%3E01%3A02%3CBR%20%2F%3E01%3A10%3CBR%20%2F%3E02%3A00%3CBR%20%2F%3E%3CBR%20%2F%3EAnd%20I%20want%20a%20count%20between%20the%20time%20periods%20of%2001%3A01%20and%2002%3A00.%20The%20count%20should%20come%20out%20as%203%2C%20however%20it%20comes%20out%20as%202%20as%20it%E2%80%99s%20not%20counting%20the%2002%3A00%20time.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2355904%22%20slang%3D%22en-US%22%3ERe%3A%20COUNTIFS%20between%20date%2Ftime%20ranges%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2355904%22%20slang%3D%22en-US%22%3EOh%2C%20I%20understand%20now.%20I%20would%20recommend%20using%20flash%20fill%20for%20the%20insert%20of%20the%20'start%20times'%20and%20then%20modifying%20them%20as%20your%20own%20data%2C%20that%20way%20it%20recognizes%20all%20of%20the%20start%20times%20as%20an%20insertion%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2356981%22%20slang%3D%22en-US%22%3ERe%3A%20COUNTIFS%20between%20date%2Ftime%20ranges%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2356981%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1054168%22%20target%3D%22_blank%22%3E%40XavierMaestas%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20avoid%20rounding%20errors%20I'd%20use%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3DSUMPRODUCT(('Main%20sheet'!%24D%242%3A%24D%2410%26gt%3B%3D%20A2)*('Main%20sheet'!%24D%242%3A%24D%2410%26lt%3B%3DB2))%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThat%20will%20be%20more%20reliable.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2355844%22%20slang%3D%22en-US%22%3ERe%3A%20COUNTIFS%20between%20date%2Ftime%20ranges%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2355844%22%20slang%3D%22en-US%22%3ESo%2C%20you're%20saying%20that%20even%20using%20the%20formula%2C%20the%20start%20times%20remain%20consistent%20using%20fill%2C%20but%20the%20end%20times%20all%20come%20out%20as%200%3F%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi,

I can't find an answer for this. I am trying to get a count between two time ranges and I can't get it to work as it should.

 

I have times in two columns:

 

Start time      End Time

00:01             01:00

01:01             02:00

02:01             03:00

 

And so on

 

The problem I am having I can't get the countifs formula to include the end time in the count. If the time in the main sheet is 01:00 for example then the count will come up as 0.

 

The formula I am using is:

 

TIFS('Main sheet'!$D$2:$D$10,">="& A3,'Main sheet'!$D$2:$D$10,"<="& B3)

 

I have attached the excel workbook if that helps. I am pulling my hair out and I know it'll be something simple that I am not doing!

 

Thank you in advance.

 

6 Replies
So, you're saying that even using the formula, the start times remain consistent using fill, but the end times all come out as 0?
The formula works for working out the count between the time periods. But say for example I have 3 times
01:02
01:10
02:00

And I want a count between the time periods of 01:01 and 02:00. The count should come out as 3, however it comes out as 2 as it’s not counting the 02:00 time.
Oh, I understand now. I would recommend using flash fill for the insert of the 'start times' and then modifying them as your own data, that way it recognizes all of the start times as an insertion

@XavierMaestas 

To avoid rounding errors I'd use

=SUMPRODUCT(('Main sheet'!$D$2:$D$10>= A2)*('Main sheet'!$D$2:$D$10<=B2))

That will be more reliable.

best response confirmed by vish93 (New Contributor)
Solution

@Sergei Baklan  wrote: ``To avoid rounding errors I'd use

=SUMPRODUCT(('Main sheet'!$D$2:$D$10>=A2)*('Main sheet'!$D$2:$D$10<=B2))

That will be more reliable.``

 

Sergei might be thinking of the formatting ("external rounding") defect that affects COUNTIFs et al, notably with times that are accurate to the second.

 

But the "external rounding" defect has no impact on COUNTIFS with times (and date+times) that are accurate to the minute.

 

And indeed, the SUMPRODUCT formula suggested above has the same results in this case, when using the same comparison operators (see #1 below), because the problem here is not due to the "external rounding" defect.

 

Instead, the problem here is the "garden variety" rounding issue, namely: the lack of explicit rounding when calculating MOD(B2,1) in column D of the Main worksheet.

 

One method of correction is (in D2):

 

=--TEXT(MOD(B2,1),"h:m")

 

Copy D2 into D3:D24.

 

Then the COUNTIFS formulas perform as expected, based on how they are written. (But see #4 below.)

 

However, there are many other mistakes in the Excel file that might eventually contribute to other counting errors.

 

-----

 

1. In the August worksheet, the second condition is "<"&B2, not "<="&B2 as it is in the July worksheet and as stated in the original posting. Presumably, the correction is (in C2):

 

=COUNTIFS('Main sheet'!$D$11:$D$12, ">=" & A2,
'Main sheet'!$D$11:$D$12, "<=" & B2)

 

Copy C2 into C3:C25.

 

-----

 

2. The Start and End times in columns A and B of the July and August worksheets are not the correct binary values for the displayed times.

 

And Fill Series (in Excel 2010) does not remedy the problem.

 

Instead, one method of correction is (in A3):

 

=--TEXT(A2+"1:0","[h]:m")

 

Copy A3 in B3.  And copy A3:B3 into A4:B25.

 

"[h]" displays 24 instead of zero. (But see #4 below.)

 

-----

 

3. The ranges of the COUNTIFS are tailored to the July'19 and August'19 data in the Main worksheet.

 

If that is your intent, you might write the following in C2 of the July worksheet, for example:

 

=COUNTIFS('Main sheet'!$B$2:$B$100, ">=" & DATE(2019,7,1),
'Main sheet'!$B$2:$B$100, "<" & DATE(2019,8,1),
'Main sheet'!$D$2:$D$100, ">=" & A2,
'Main sheet'!$D$2:$D$100, "<=" & B2)

 

But if you want to count all values in July of all years, for example, you might write the following in C2:

 

=SUMPRODUCT((MONTH('Main sheet'!$B$2:$B$100)=7)

* ('Main sheet'!$D$2:$D$100>=A2)
* ('Main sheet'!$D$2:$D$100<=B2))

 

Here, we use SUMPRODUCT not because it is more reliable, but because it has functionality that is lacking in COUNTIFS, namely bona fide arrays, not just cell ranges.

 

Copy C2 into C3:C25.

 

-----

 

4. You make mixed assumptions about the interpretation of the time 00:00 (midnight).

 

MOD(B2,1) in D2 of the Main worksheet (zero) will never be counted because it does not match 24:00 in B25 of the July and August worksheets, which is formatted to display 0:00.

 

In fact, midnight is the beginning of a day, not the end of a day. So I would suggest the following changes.

 

a. Change just A2 of the July and August worksheets to 0:00, instead of 0:01.

 

b. With the changes in #2 and #4a, change the corrected formula in just A3 of the July and August worksheets to:

 

=--TEXT(A2+"1:1","[h]:m")

 

Thus, all the other start times 1 min after the hour, as you have now.

 

c. Change the format of A2:A25 and B2:B25 of the July and August worksheets to Custom [h]:mm.

 

Thus, B25 displays 24:00, not 0:00.

 

-----

 

All of these changes are implemented in the attached Excel file.

 

@Joe User 

You are an absolute life saver! 

 

I applied all the logic you stated in your post in my actual spreadsheet and it all worked out how it should! 

 

I can't thank you enough.