Forum Discussion

Txtcher's avatar
Txtcher
Helper V
1 year ago
Solved

Calculating Business days excluding holidays function with null values

I need a function in power query to calculate the number of business days, excluding holidays and accounting for any null date values. I also need it to replace any results <0 with null, or replace a...
  • Amar_Kumar's avatar
    1 year ago

    Try this

     

    Txtcher 

    let

        fBusDays = (StartDate as nullable date, EndDate as nullable date, HolidayList as list) as nullable number =>

            let

                varBusDays =

                    if StartDate = null or EndDate = null then 

                        null

                    else 

                        let

                            // Ensure end date is included by adding 1 to the count

                            DateList = List.Dates(StartDate, Duration.Days(EndDate - StartDate) + 1, #duration(1,0,0,0)),

                            RemoveWeekends = List.Select(DateList, each Date.DayOfWeek(_, Day.Monday) < 5),

                            RemoveHolidays = List.RemoveItems(RemoveWeekends, HolidayList),

                            BusDays = List.Count(RemoveHolidays)

                        in

                            if BusDays < 0 then null 

                            else if BusDays = 0 then 1 

                            else BusDays

            in

                varBusDays

    in

        fBusDays

     

    You can invoke it like this in a custom column:

    fBusDays([Start Date], [End Date], HolidayList)

     

    Make sure HolidayList is a list of dates.