Forum Discussion
WorkDays Function in Power Query M
- 6 years ago
Hi Anonymous
that looks a bit buggy, indeed.
However, you formula works if you avoid using the same name for a step than for a variable like so:
let func = (StartDate as date, WorkDays as number) => let WorkDays2 = (WorkDays*2)+7, startDate = if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,2) else if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,1) else StartDate, ListOfDates = List.Dates(startDate, WorkDays2,#duration(1,0,0,0)), DeleteWeekends = List.Select(ListOfDates, each Date.DayOfWeek(_,1) < 5 ), WorkDate = List.Range(DeleteWeekends,WorkDays,1), Result = WorkDate{0} in Result in func - Anonymous6 years ago
Thank you for solving it, I tested it and noticed it doesn't work with negative values,
so I went away and updated with the following,
its a bit messy but it works
Hopefully someone who needs it can quickly use it.
//fnWorkDays let func = (StartDate as date, WorkDays as number) => let WorkDays2 = if WorkDays<0 then (WorkDays*2)-7 else (WorkDays*2)+7, StartDate2 = if WorkDays<0 then if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,2) else if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,1) else StartDate else if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,-1) else if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,-2) else StartDate, ListofDates = if WorkDays<0 then List.Dates(Date.AddDays(StartDate2,WorkDays2), -1*WorkDays2+1,#duration(1,0,0,0)) else List.Dates(StartDate2, WorkDays2,#duration(1,0,0,0)), DeleteWeekends = List.Select(ListofDates, each Date.DayOfWeek(_) < 5 ), StartDateRange = if WorkDays<0 then List.PositionOf(DeleteWeekends,StartDate2) else 0, WorkDateRange = if WorkDays<0 then StartDateRange+WorkDays else WorkDays, WorkDate = List.Range(DeleteWeekends,WorkDateRange,1), Result = if WorkDays =0 then StartDate else WorkDate{0} in Result in func
Hi Anonymous
that looks a bit buggy, indeed.
However, you formula works if you avoid using the same name for a step than for a variable like so:
let func = (StartDate as date, WorkDays as number) =>
let
WorkDays2 = (WorkDays*2)+7,
startDate = if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,2) else
if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,1) else StartDate,
ListOfDates = List.Dates(startDate, WorkDays2,#duration(1,0,0,0)),
DeleteWeekends = List.Select(ListOfDates, each Date.DayOfWeek(_,1) < 5 ),
WorkDate = List.Range(DeleteWeekends,WorkDays,1),
Result = WorkDate{0}
in
Result
in
func
Thank you for solving it, I tested it and noticed it doesn't work with negative values,
so I went away and updated with the following,
its a bit messy but it works
Hopefully someone who needs it can quickly use it.
//fnWorkDays
let func = (StartDate as date, WorkDays as number) =>
let
WorkDays2 = if WorkDays<0 then
(WorkDays*2)-7 else (WorkDays*2)+7,
StartDate2 =
if WorkDays<0 then
if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,2) else
if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,1) else StartDate
else
if Date.DayOfWeek(StartDate)=5 then Date.AddDays(StartDate,-1) else
if Date.DayOfWeek(StartDate)=6 then Date.AddDays(StartDate,-2) else StartDate,
ListofDates = if WorkDays<0 then
List.Dates(Date.AddDays(StartDate2,WorkDays2), -1*WorkDays2+1,#duration(1,0,0,0))
else
List.Dates(StartDate2, WorkDays2,#duration(1,0,0,0)),
DeleteWeekends = List.Select(ListofDates, each Date.DayOfWeek(_) < 5 ),
StartDateRange = if WorkDays<0 then List.PositionOf(DeleteWeekends,StartDate2) else 0,
WorkDateRange = if WorkDays<0 then StartDateRange+WorkDays else WorkDays,
WorkDate = List.Range(DeleteWeekends,WorkDateRange,1),
Result = if WorkDays =0 then StartDate else WorkDate{0}
in
Result
in
func- Anonymous5 years agoNot applicable
ImkeF - i have a requriement to extract data from DWH only on weekdays , so i was thinking of using this function to pass the date , for e.g. for Monday run date , my run_date should be last Friday date . The above function works fine , as long as you pass the start date .
but in my case , my start date would always be today to get the last business work date , could you please help here . Thanks in advance
- ImkeF5 years ago
Community Champion
Hi Anonymous ,
I don't understand the requirment, unfortunately.
Could you please give an example with sample values (before and desired after)?
Thanks!- Anonymous5 years agoNot applicable
Hi ImkeF ,
Apologies if i was not clear on my requirement . Request : i just want to retrive the last working day and pass this date as a parameter in another script to fetch data .
So from this function , all i want is to retreive the last working day . E.g. If today is Monday (31 May 2021) , then my last working day is Last friday ( 28 May 2021) .
For clarity - all days Mon-Fridays are working day in our firm , which includes all public holidays as well, so data is made available from Monday to Friday in our DWH .
The above function you had shared is great but i would need to pass a date there , in my case the start date would always be today . i tried to pass the start date as (DateTime.Date(DateTime.LocalNow())) but was unsuccessful .
Hope you can help me here .