Custom Function of NetworkDays in Power Query not givng Negative result.

Anton01

New Member
Joined
May 10, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Good Afternoon, I'm trying to run the following Function, however if the answer is negative it just shows as Error, is there a way to get this to show the actual Negative number instead?
I'm very new to the world of Power Query, so any help is gratefully received.

(StartDate as date, EndDate as date, HolidayList as list) as number =>
let
Weekend = #table(type table[Weekday = Int64.Type],{{0},{6}}),
Holidays = Table.FromColumns({HolidayList},type table[Weekday = date]),
Dates = Table.FromColumns({List.Dates(StartDate,1+Duration.Days(EndDate-StartDate),#duration(1,0,0,0))}, type table[Dates = date]),
#"Inserted Day of Week" = Table.AddColumn(Dates, "Day of Week", each Date.DayOfWeek([Dates]), type number),
#"Merged Queries" = Table.NestedJoin(#"Inserted Day of Week",{"Day of Week"},Weekend,{"Weekday"},"Inserted Day of Week",JoinKind.LeftAnti),
#"Merged Queries1" = Table.NestedJoin(#"Merged Queries",{"Dates"},Holidays,{"Weekday"},"Holidays",JoinKind.LeftAnti),
NetWorkdays = Table.RowCount(#"Merged Queries1")
in
NetWorkdays
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Power Query:
(StartDate as date, EndDate as date, HolidayList as list) as number =>
let
    Weekend = #table(type table[Weekday = Int64.Type],{{0},{6}}),
    Holidays = Table.FromColumns({HolidayList},type table[Weekday = date]),
    m = if EndDate>=StartDate then 1 else -1,
    End = if m=1 then EndDate else StartDate,
    Start = if m =1 then StartDate else EndDate,
    Dates = Table.FromColumns({List.Dates(Start,1+Duration.Days(End-Start),#duration(1,0,0,0))}, type table[Dates = date]),
    #"Inserted Day of Week" = Table.AddColumn(Dates, "Day of Week", each Date.DayOfWeek([Dates]), type number),
    #"Merged Queries" = Table.NestedJoin(#"Inserted Day of Week",{"Day of Week"},Weekend,{"Weekday"},"Inserted Day of Week",JoinKind.LeftAnti),
    #"Merged Queries1" = Table.NestedJoin(#"Merged Queries",{"Dates"},Holidays,{"Weekday"},"Holidays",JoinKind.LeftAnti),
    NetWorkdays = m*Table.RowCount(#"Merged Queries1")
in
NetWorkdays
 
Upvote 1
Solution
Power Query:
(StartDate as date, EndDate as date, HolidayList as list) as number =>
let
    Weekend = #table(type table[Weekday = Int64.Type],{{0},{6}}),
    Holidays = Table.FromColumns({HolidayList},type table[Weekday = date]),
    m = if EndDate>=StartDate then 1 else -1,
    End = if m=1 then EndDate else StartDate,
    Start = if m =1 then StartDate else EndDate,
    Dates = Table.FromColumns({List.Dates(Start,1+Duration.Days(End-Start),#duration(1,0,0,0))}, type table[Dates = date]),
    #"Inserted Day of Week" = Table.AddColumn(Dates, "Day of Week", each Date.DayOfWeek([Dates]), type number),
    #"Merged Queries" = Table.NestedJoin(#"Inserted Day of Week",{"Day of Week"},Weekend,{"Weekday"},"Inserted Day of Week",JoinKind.LeftAnti),
    #"Merged Queries1" = Table.NestedJoin(#"Merged Queries",{"Dates"},Holidays,{"Weekday"},"Holidays",JoinKind.LeftAnti),
    NetWorkdays = m*Table.RowCount(#"Merged Queries1")
in
NetWorkdays
Thanks, That works perfectly! exactly what I needed :)
 
Upvote 0
Power Query:
(StartDate as date, EndDate as date, HolidayList as list) as number =>
let
    Weekend = #table(type table[Weekday = Int64.Type],{{0},{6}}),
    Holidays = Table.FromColumns({HolidayList},type table[Weekday = date]),
    m = if EndDate>=StartDate then 1 else -1,
    End = if m=1 then EndDate else StartDate,
    Start = if m =1 then StartDate else EndDate,
    Dates = Table.FromColumns({List.Dates(Start,1+Duration.Days(End-Start),#duration(1,0,0,0))}, type table[Dates = date]),
    #"Inserted Day of Week" = Table.AddColumn(Dates, "Day of Week", each Date.DayOfWeek([Dates]), type number),
    #"Merged Queries" = Table.NestedJoin(#"Inserted Day of Week",{"Day of Week"},Weekend,{"Weekday"},"Inserted Day of Week",JoinKind.LeftAnti),
    #"Merged Queries1" = Table.NestedJoin(#"Merged Queries",{"Dates"},Holidays,{"Weekday"},"Holidays",JoinKind.LeftAnti),
    NetWorkdays = m*Table.RowCount(#"Merged Queries1")
in
NetWorkdays
I've just realised that if Start Date is the same as End Date it is giving me a result of 1, really it should be 0, , tomorrow should be -1, yesterday should be 1, is there a simple change to the m code to fix this?
Many Thanks
Anton.
 
Upvote 0
remove the 1+ from this line

Power Query:
Dates = Table.FromColumns({List.Dates(Start,1+Duration.Days(End-Start),#duration(1,0,0,0))}, type table[Dates = date]),
 
Upvote 1
remove the 1+ from this line

Power Query:
Dates = Table.FromColumns({List.Dates(Start,1+Duration.Days(End-Start),#duration(1,0,0,0))}, type table[Dates = date]),
Thanks, That has helped somewhat, however I'm still having another very odd issue, Where I should get a result of -6 i.e. 6 working days until due date, I'm getting -7, Jobs that are due in 7 days are correctly showing as -7, so very odd. Any Ideas?
 
Upvote 0
Can you post a few start and end date pairs, your holiday list and what the result should be? Something like this (this is with the 1 + removed, and an empty holiday list):

StartEndNetWorkDaysRight/Wrong?Result should be?
12/12/202312/29/202313
11/23/202312/12/202313
1/3/202412/5/2023-21
11/18/202312/24/202325
12/31/202312/21/2023-7
12/25/202312/29/20234
12/2/202312/30/202320
12/10/202311/28/2023-9
11/26/202311/25/2023-0
11/25/202312/23/202320
1/1/20241/1/20240
12/14/202311/29/2023-11
12/28/202311/21/2023-27
12/21/202312/11/2023-8
12/20/202312/15/2023-3
11/19/20231/3/202432
11/20/202312/5/202311
12/13/202312/24/20238
12/7/202311/27/2023-8
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top