Share via

Count number of dates from range, where dates fall between a particular start and end date pair that include today's date.

Anonymous
2024-05-24T14:25:20+00:00

I am trying to display a count of particular holiday dates that fall within a specific pair of dates, the pair of dates chosen if they include today's date.

My intent in pseudocode:

StartDate = Closest value in StartDateRange <= TODAY();
EndDate = Closest value in EndDateRange >= TODAY();
For each value in HolidayDateRange, count if value is between StartDate and EndDate;

Here is the link to my sample workbook, and all the following formulas will reference that file.

My initial thinking is this, but I'm receiving a general formula error:

=COUNTIFS($B$2:$B$8,>=XLOOKUP(TODAY(),$C$2:$C$53,$C$2:$C$53,"Error",-1,1),$B$2:$B$8,>=XLOOKUP(TODAY(),$D$2:$D$53,$D$2:$D$53,"Error",1,-1))

the expected result as of 23May24 is "1" for Memorial Day.

I did some troubleshooting on 23May24 of the of the formula and its components, and the most complex two appear to be working as expected:

Cell F5 =XLOOKUP(TODAY(),$C$2:$C$53,$C$2:$C$53,"Error",-1,1) returns 5/15/2024 as expected. 

Cell F7 =XLOOKUP(TODAY(),$D$2:$D$53,$D$2:$D$53,"Error",1,-1) returns 5/28/2024 as expected. 

However, when I attempt to plug them in as conditionals for COUNTIF or COUNTIFS, I start seeing errors:

Cell F10 =COUNTIF($B$2:$B$8,>=XLOOKUP(TODAY(),$C$2:$C$53,$C$2:$C$53,"Error",-1,1)) returns an error. 

Cell F12 =COUNTIF($B$2:$B$8,<=XLOOKUP(TODAY(),$D$2:$D$53,$D$2:$D$53,"Error",1,-1)) returns an error. 

I'm suspicious it has something to do with the logical processors not being handled correctly in the formula, so I tried several things to see if that fixed it, with no luck:

Cell F15 =COUNTIF($B$2:$B$8,">=XLOOKUP(TODAY(),$C$2:$C$53,$C$2:$C$53,"Error",-1,1)")

encapsulating the whole conditional in quotes, returns an error.

Cell F17 =COUNTIF($B$2:$B$8,>="XLOOKUP(TODAY(),$C$2:$C$53,$C$2:$C$53,"Error",-1,1)")

encapsulating only the retrieved date value of the conditional in quotes, returns an error.

I'm obviously doing something wrong, but I'm not sure how to proceed from here. Any help in figuring out how to make this behave would be greatly appreciated, as would suggestions on making the whole thing simpler, if possible.

Thanks in advance,

- SilverbackPrime
Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2024-05-28T19:33:54+00:00

    =LET(
    Timeframe,XLOOKUP(TODAY(),C:C,C:D,NA(),1),
    StartDate,INDEX(Timeframe,1),
    EndDate,INDEX(Timeframe,2),
    Holidays,B2:B8,
    NETWORKDAYS.INTL(StartDate,EndDate,"0000000")-NETWORKDAYS.INTL(StartDate,EndDate,"0000000",Holidays))

    So I'm not sure if this is quite what I'm looking for. Let me see if I can explain more thoroughly using the most recent holiday (Memorial Day: 27May2024) as an example:

    1. Memorial Day (27May2024) is specified in B4
    2. The "Timeframe Start Date" and "Timeframe End Date" pair containing Memorial Day (15May2024, 28May2024) are specified in C37:D37
    3. My expectation is that if TODAY() is greater-than or equal to 15May2024 (C37) AND is less-than or equal to 28May2024 (D37), the formula should return "1" due to Memorial Day being within that range.

    I tested the formula this morning (28May2024), and not only does it not currently return "1", expectation as stated in 3. above, it does not return "1" until the date specified in "XLOOKUP(search_key" is manually set to a value between 2May2024 and 15May2024 by substituting "DATE(year,month,day)" for "TODAY()", indicating that the formula provided is utilizing the previous two week range prior to the holiday, instead of the two week range containing the holiday.

    I believe the correct formula to achieve the expected result is as follows (change in green):

    =LET( Timeframe,XLOOKUP(TODAY(),C:C,C:D,NA(),-1),

    StartDate,INDEX(Timeframe,1),

    EndDate,INDEX(Timeframe,2),

    Holidays,B2:B8,

    NETWORKDAYS.INTL(StartDate,EndDate,"0000000")-NETWORKDAYS.INTL(StartDate,EndDate,"0000000",Holidays))

    Will that be sufficient to correctly handle each instance of the values in Holidays, or are there other changes that need to be made as well?

    Was this answer helpful?

    0 comments No comments
  2. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2024-05-24T14:46:08+00:00

    I am trying to display a count of particular holiday dates that fall within a specific pair of dates, the pair of dates chosen if they include today's date.

    =LET(
    Timeframe,XLOOKUP(TODAY(),C:C,C:D,NA(),1),
    StartDate,INDEX(Timeframe,1),
    EndDate,INDEX(Timeframe,2),
    Holidays,B2:B8,
    NETWORKDAYS.INTL(StartDate,EndDate,"0000000")-NETWORKDAYS.INTL(StartDate,EndDate,"0000000",Holidays))

    Was this answer helpful?

    0 comments No comments