Forum Discussion
Calculating Business days excluding holidays function with null values
- 1 year ago
Try this
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.
Hi Txtcher
You're super close —
honestly just a tiny typo is causing your problem.
Here’s exactly what’s wrong:
🔴 You have mismatched variable names:
-
You create
varBusDays(with capital D) -
Then in the final
in, you writefBusdays(lowercased).
Power Query is case sensitive, so fBusdays and fBusDays are not the same for it!
That's why it's breaking even though it looks fine to us.
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!