1

I have a simple problem, but I have already tried to solve it in several ways and did not get the expected result. I need to calculate the number of business days between two dates, but without considering the first day.

Example: initial_date: 15/08/2024 (Thursday) final_date: 20/08/2024 (Tuesday)

In Excel, if I calculate calendar days =DAYS(dt_initial;dt_final), I obtain a total of 5 days. In other words, it counts Saturday and Sunday, but that's not what I need. If I try to disregard the weekend =DIAWORKALHOTAL.INTL(dt_initial;dt_final;1), I get the result 4. That is, it includes the 15th in the account.

What I would like is to find a way to count from the next day, that is, in the example above, the result would be = 3 (days 16, 19 and 20).

If anyone knows a way to solve it, whether in Excel, PowerBI or even with Python, could you please let me know?

As I mentioned, in Excel I have already tried using =DIAWORKALHOTAL.INTL(dt_initial;dt_final;1), but it returns a count considering the starting date (the first day).

In PowerBI, I tried the example below, but got the same problem:

work_days = NETWORKDAYS(table[initial_date],table[final_date], 1)

5
  • HI, questions on Stack Overflow must be in English, but you can ask in Spanish here: es.stackoverflow.com Commented Aug 22, 2024 at 19:53
  • 3
    =DIAWORKALHOTAL.INTL(dt_initial + 1;dt_final;1), Commented Aug 22, 2024 at 19:58
  • What happens if first date is e.g. a Saturday? 24.8.24 to 26.8.24? What would be the expected result? Commented Aug 23, 2024 at 6:49
  • 2
    @jessie, do you even try =DIAWORKALHOTAL.INTL(dt_initial + 1;dt_final;1) before replying the comment? =DIAWORKALHOTAL.INTL(date(2024, 8, 19)+ 1;date(2024, 8, 20);1) will return 1, not zero. =DIAWORKALHOTAL.INTL doesn't calculate "duration", it just returns the number of days that satisfy a certain condition (a business day). so =DIAWORKALHOTAL.INTL(date(2024, 8, 20);date(2024, 8, 20);1) will return 1. but I agree, if start date > end date, DIAWORKALHOTAL.INTL won't work. Commented Aug 23, 2024 at 8:09
  • @Jessie - great Q btw. See answer below - it's the most parsimonious (simple) to give the correct answer (for same day start=end etc) that I can see so far (but more may come through etfc.) Commented Aug 23, 2024 at 14:40

4 Answers 4

1

I believe you just need this:

=NETWORKDAYS.INTL(dt_initial, dt_final,1)-NETWORKDAYS.INTL(dt_initial,dt_initial,1)

NETWORKDAYS.INTL(dt_initial, dt_final,1) calculates business days between initial date and final date, both inclusive.

NETWORKDAYS.INTL(dt_initial,dt_initial,1) checks whether initial_date is a business day or not, if it is, deduct one from NETWORKDAYS.INTL(dt_initial, dt_final,1), if not leave NETWORKDAYS.INTL(dt_initial, dt_final,1) ast it is.

Sign up to request clarification or add additional context in comments.

Comments

0

Below/here refer:

=IF(C4<C3,"",NETWORKDAYS(C3,C4)-(WEEKDAY(C3,2)<=5))

solution.

The reason why other calcs were not working (those in comments etc.) is primarily due to the somewhat 'cute' approach of attemting to ignore the first day by setting startdate += 1 (the day after startdate).

This falls down, as you have noted, when start = end. My approach addresses this (and more generalized variations) appropriately/correctly - and is otherwise just as simple/parsimonious to utilize/customize.


To demonstrate this is the correct solution here is a run-off table showcasing results (based on data-table/avail. at link above) across a range of start/end dates.

validation


FYI - not sure where OPs function came from given below - but did say that Excel/Python etc. would also be welcome. I stuck to Excel (per the title ☺)


Google search result for the function provided by OP/1st commenter.

Google search result

(noting AI links reference this specific post!)


Comments

0

You can use my DateDiffWorkdays function:

Date1 = #2024-08-19#
Date2 = #2024-08-20#
? DateDiffWorkdays(Date1, Date2)
 1 

Date1 = #2024-08-19#
Date2 = #2024-08-21#
? DateDiffWorkdays(Date1, Date2)
 2 

Date1 = #2024-08-15#
Date2 = #2024-08-20#
? DateDiffWorkdays(Date1, Date2)
 3 

The function:

' Returns the count of full workdays between Date1 and Date2.
' The date difference can be positive, zero, or negative.
' Optionally, if WorkOnHolidays is True, holidays are regarded as workdays.
'
' Note that if one date is in a weekend and the other is not, the reverse
' count will differ by one, because the first date never is included in the count:
'
'   Mo  Tu  We  Th  Fr  Sa  Su      Su  Sa  Fr  Th  We  Tu  Mo
'    0   1   2   3   4   4   4       0   0  -1  -2  -3  -4  -5
'
'   Su  Mo  Tu  We  Th  Fr  Sa      Sa  Fr  Th  We  Tu  Mo  Su
'    0   1   2   3   4   5   5       0  -1  -2  -3  -4  -5  -5
'
'   Sa  Su  Mo  Tu  We  Th  Fr      Fr  Th  We  Tu  Mo  Su  Sa
'    0   0   1   2   3   4   5       0  -1  -2  -3  -4  -4  -4
'
'   Fr  Sa  Su  Mo  Tu  We  Th      Th  We  Tu  Mo  Su  Sa  Fr
'    0   0   0   1   2   3   4       0  -1  -2  -3  -3  -3  -4
'
' Execution time for finding working days of three years is about 4 ms.
'
' Requires table Holiday with list of holidays.
'
' 2015-12-19. Gustav Brock. Cactus Data ApS, CPH.
'
Public Function DateDiffWorkdays( _
    ByVal Date1 As Date, _
    ByVal Date2 As Date, _
    Optional ByVal WorkOnHolidays As Boolean) _
    As Long
    
    Dim Holidays()      As Date
    
    Dim Diff            As Long
    Dim Sign            As Long
    Dim NextHoliday     As Long
    Dim LastHoliday     As Long
    
    Sign = Sgn(DateDiff("d", Date1, Date2))
    If Sign <> 0 Then
        If WorkOnHolidays = True Then
            ' Holidays are workdays.
        Else
            ' Retrieve array with holidays between Date1 and Date2.
            Holidays = DatesHoliday(Date1, Date2, False) 'CBool(Sign < 0))
            ' Ignore error when using LBound and UBound on an unassigned array.
            On Error Resume Next
            NextHoliday = LBound(Holidays)
            LastHoliday = UBound(Holidays)
            ' If Err.Number > 0 there are no holidays between Date1 and Date2.
            If Err.Number > 0 Then
                WorkOnHolidays = True
            End If
            On Error GoTo 0
        End If
        
        ' Loop to sum up workdays.
        Do Until DateDiff("d", Date1, Date2) = 0
            Select Case Weekday(Date1)
                Case vbSaturday, vbSunday
                    ' Skip weekend.
                Case Else
                    If WorkOnHolidays = False Then
                        ' Check for holidays to skip.
                        If NextHoliday <= LastHoliday Then
                            ' First, check if NextHoliday hasn't been advanced.
                            If NextHoliday < LastHoliday Then
                                If Sgn(DateDiff("d", Date1, Holidays(NextHoliday))) = -Sign Then
                                    ' Weekend hasn't advanced NextHoliday.
                                    NextHoliday = NextHoliday + 1
                                End If
                            End If
                            ' Then, check if Date1 has reached a holiday.
                            If DateDiff("d", Date1, Holidays(NextHoliday)) = 0 Then
                                ' This Date1 hits a holiday.
                                ' Subtract one day to neutralize the one
                                ' being added at the end of the loop.
                                Diff = Diff - Sign
                                ' Adjust to the next holiday to check.
                                NextHoliday = NextHoliday + 1
                            End If
                        End If
                    End If
                    Diff = Diff + Sign
            End Select
            ' Advance Date1.
            Date1 = DateAdd("d", Sign, Date1)
        Loop
    End If
    
    DateDiffWorkdays = Diff

End Function

It requires some supporting functions - too much code to post here - which all can be found in the modules at my repository at GitHub: VBA.Date.

Note, that it also allows for excluding holidays held in a holiday table.

I've added for you an Excel demo in the demos folder: DateWork.xlsm.

Comments

0

Not sure if this fits your use case, but this is what works for me. I use NETWORKDAYS to exclude weekends, and then subtract 1 from the result. If you want to exclude holidays, you'll need to put a third parameter after the end date.

=NETWORKDAYS(start_date, end_date)-1

Using this method, if your start and end dates are the same, you will get 0 days as the result. I interpret that as no days have elapsed between start and end, which is valid for my need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.