Skip to content

Working Day functions

This page builds two custom functions in Power Query: one counts the working days between two dates, and the other finds the date a given number of working days after a start date. Both skip weekends and public holidays, and both allow the weekend to differ from one country to another.

Excel has these as NETWORKDAYS.INTL and WORKDAY.INTL. Power Query has no built-in equivalent of either, so the functions here are written in M.

The Lantern Keepers has a main unit in the UK and a second unit filming in Jordan. The production office keeps a table of crew leave bookings, each with a first and last day of leave, and wants two things for every booking:

  • how many working days it uses, which is what comes off the crew member’s allowance;
  • the date the crew member is back at work, which is the first working day after the booking ends.

The dates are in 2025. The leave bookings arrive as a query named Leave:

Name Country Start Date End Date
Maya Okafor UK 14/04/2025 17/04/2025
Tom Hale UK 01/05/2025 09/05/2025
Owen Price Jordan 27/04/2025 30/04/2025
Priya Nair Jordan 20/05/2025 27/05/2025

The UK unit’s weekend is Saturday and Sunday, while the unit in Jordan works Sunday to Thursday and takes Friday and Saturday off. A query named Weekends holds one row for each weekend day of each country:

Country Weekend Day Day Number
UK Saturday 5
UK Sunday 6
Jordan Friday 4
Jordan Saturday 5

Day Number is the number that Date.DayOfWeek(date, Day.Monday) returns for that day, which counts Monday as 0 and Sunday as 6. The Weekend Day column is there for people reading the table, and the functions use only Day Number.

A query named Holidays holds the public holidays that fall in the same weeks:

Country Date Holiday
UK 18/04/2025 Good Friday
UK 21/04/2025 Easter Monday
UK 05/05/2025 Early May bank holiday
UK 26/05/2025 Spring bank holiday
Jordan 01/05/2025 Labour Day
Jordan 25/05/2025 Independence Day
  • Load the leave bookings, the weekend days and the public holidays into Power Query, and name the queries Leave, Weekends and Holidays.
  • Set the type of Start Date, End Date and the holidays’ Date column to Date, and the type of Day Number to Whole Number.

The date types matter. The functions compare dates with List.Contains, and in M a date and a datetime are not equal, even when they fall on the same day, so a Date column left as Date/Time would match no holiday at all.

Create a blank query, name it fnNetWorkdays, and enter this code in the Advanced Editor:

(startDate as date, endDate as date, country as text) as number =>
let
WeekendDays = List.Buffer(Table.SelectRows(Weekends, each [Country] = country)[Day Number]),
HolidayDates = List.Buffer(Table.SelectRows(Holidays, each [Country] = country)[Date]),
IsWorkday = (d as date) as logical =>
not List.Contains(WeekendDays, Date.DayOfWeek(d, Day.Monday))
and not List.Contains(HolidayDates, d),
FirstDate = List.Min({startDate, endDate}),
LastDate = List.Max({startDate, endDate}),
AllDates = List.Dates(FirstDate, Duration.Days(LastDate - FirstDate) + 1, #duration(1, 0, 0, 0)),
WorkingDates = List.Select(AllDates, IsWorkday),
DayCount = List.Count(WorkingDates),
Result = if startDate > endDate then -DayCount else DayCount
in
Result

The function works through these steps:

  1. WeekendDays keeps the rows of Weekends for the given country and takes their Day Number column as a list, such as {4, 5} for Jordan. HolidayDates does the same with Holidays, giving a list of that country’s holiday dates. List.Buffer holds each list in memory, so that Power Query does not read the table again for every date it checks.
  2. IsWorkday is a small function inside the function. It returns true for a date whose day number is not in WeekendDays and which is not in HolidayDates.
  3. FirstDate and LastDate put the two dates in order, so that the function also works when the start date is after the end date.
  4. AllDates uses List.Dates to list every date from FirstDate to LastDate, both included. Duration.Days(LastDate - FirstDate) + 1 is the number of dates in that span.
  5. WorkingDates uses List.Select to keep only the dates for which IsWorkday returns true, and DayCount counts them.
  6. Result returns the count, made negative when the start date is after the end date, as NETWORKDAYS.INTL does.

Inside Table.SelectRows, each reads a row of the table, so [Country] there is the table’s Country column, while country is the function’s parameter. M is case-sensitive, so the two names do not clash.

For example, fnNetWorkdays(#date(2025, 5, 20), #date(2025, 5, 27), "Jordan") lists the eight dates from Tuesday 20 May to Tuesday 27 May. It drops Friday 23 and Saturday 24 May, which are the weekend in Jordan, and Sunday 25 May, which is Independence Day. That leaves 20, 21, 22, 26 and 27 May, so it returns 5. Monday 26 May is a bank holiday in the UK, but it counts here, because the function looks only at Jordan’s holidays.

Create a second blank query, name it fnWorkday, and enter this code:

(startDate as date, days as number, country as text) as date =>
let
WeekendDays = List.Buffer(Table.SelectRows(Weekends, each [Country] = country)[Day Number]),
HolidayDates = List.Buffer(Table.SelectRows(Holidays, each [Country] = country)[Date]),
IsWorkday = (d as date) as logical =>
not List.Contains(WeekendDays, Date.DayOfWeek(d, Day.Monday))
and not List.Contains(HolidayDates, d),
Direction = if days < 0 then -1 else 1,
DaysToCheck = Number.Abs(days) * 2 + 14,
Candidates = List.Transform({1..DaysToCheck}, each Date.AddDays(startDate, Direction * _)),
WorkingDates = List.Select(Candidates, IsWorkday),
Result = if days = 0 then startDate else WorkingDates{Number.Abs(days) - 1}
in
Result

The first three steps are the same as in fnNetWorkdays. The rest works like this:

  1. Direction is 1 when the function counts forward and -1 when days is negative and it counts back.
  2. DaysToCheck is how many calendar days the function looks at. A week holds at least five working days, so twice the number of working days asked for, plus two weeks, leaves room for weekends and several holidays.
  3. Candidates lists the dates after the start date in that direction: the day after it, the day after that, and so on. {1..DaysToCheck} is the list of whole numbers from 1 to DaysToCheck, and List.Transform turns each number into a date with Date.AddDays. The start date itself is not a candidate, because WORKDAY.INTL does not count it either.
  4. WorkingDates keeps the candidates that are working days, nearest first.
  5. Result picks the working day at the position asked for. List positions in M start at 0, so the first working day is WorkingDates{0}, and so on. When days is 0, the function returns the start date unchanged, as WORKDAY.INTL does, even if the start date is not a working day.

For example, fnWorkday(#date(2025, 4, 17), 1, "UK") looks forward from Thursday 17 April. It skips Good Friday on the 18th, the weekend of the 19th and 20th and Easter Monday on the 21st, and returns Tuesday 22 April. Counting back, fnWorkday(#date(2025, 4, 22), -1, "UK") returns Thursday 17 April.

The function expects days to be a whole number. If a country had so many holidays close together that Candidates ran out of working days, WorkingDates{Number.Abs(days) - 1} would return an error, and the fix would be to raise the 14 in DaysToCheck.

Create a query named Leave - Working Days with this code:

let
Source = Leave,
AddWorkingDays = Table.AddColumn(Source, "Working Days", each fnNetWorkdays([Start Date], [End Date], [Country]), Int64.Type),
AddBackAtWork = Table.AddColumn(AddWorkingDays, "Back At Work", each fnWorkday([End Date], 1, [Country]), type date)
in
AddBackAtWork
  • AddWorkingDays calls fnNetWorkdays once for each booking, with its start date, end date and country.
  • AddBackAtWork calls fnWorkday with the booking’s end date and 1, which gives the first working day after the leave ends.

The query returns:

Name Country Start Date End Date Working Days Back At Work
Maya Okafor UK 14/04/2025 17/04/2025 4 22/04/2025
Tom Hale UK 01/05/2025 09/05/2025 6 12/05/2025
Owen Price Jordan 27/04/2025 30/04/2025 4 04/05/2025
Priya Nair Jordan 20/05/2025 27/05/2025 5 28/05/2025

Each row comes out as follows:

  • Maya Okafor takes Monday 14 to Thursday 17 April, which are four working days. She is back on Tuesday 22 April, because Good Friday, the weekend and Easter Monday come first.
  • Tom Hale’s booking runs from Thursday 1 to Friday 9 May. It uses six working days, because the weekend of 3 and 4 May and the bank holiday on Monday 5 May do not count. He is back on Monday 12 May.
  • Owen Price books Sunday 27 to Wednesday 30 April. Sunday is a working day in Jordan, so the booking uses four days. He is back on Sunday 4 May, because Labour Day on Thursday 1 May and the weekend of Friday 2 and Saturday 3 May come first.
  • Priya Nair’s booking uses five working days, as worked through for fnNetWorkdays, and she is back on Wednesday 28 May.

In Excel, NETWORKDAYS.INTL and WORKDAY.INTL take the weekend as a number, where 1 means Saturday and Sunday and 7 means Friday and Saturday, and the holidays as a range of dates. With the UK holidays in a range named UKHolidays, the Jordan holidays in a range named JordanHolidays, and a booking’s start and end dates in C2 and D2, the formulas are:

UK:
=NETWORKDAYS.INTL(C2, D2, 1, UKHolidays)
=WORKDAY.INTL(D2, 1, 1, UKHolidays)
Jordan:
=NETWORKDAYS.INTL(C2, D2, 7, JordanHolidays)
=WORKDAY.INTL(D2, 1, 7, JordanHolidays)

The weekend code has to be chosen for each country by hand in Excel. In the Power Query version, the weekend and the holidays both come from tables keyed by country, so a unit in a new country needs only new rows in Weekends and Holidays, not new formulas.