Forum Discussion
NEtwork Days M language
- 6 years ago
Hello
I tried to speed up my version. so if you have no performance issue with this solution, I would go for it.
This solution is slower af the other proposed Anonymous . But if we come to include holiday it's faster
// fnNetWorkDays (DateStart as date, DateEnd as date, optional Holidays as list) => let Switch = DateStart > DateEnd, StartDate = if Switch then DateEnd else DateStart, EndDate = if Switch then DateStart else DateEnd, WeekDaysInRange= ( Duration.TotalDays ( ( EndDate- ( StartDate +Duration.From ( Date.DayOfWeek ( EndDate ) -Date.DayOfWeek ( StartDate ) ) ) ) ) /7*5 ) +Date.DayOfWeek ( EndDate ) -Date.DayOfWeek ( StartDate ) +1 + ( if Date.DayOfWeek ( StartDate )=0 then -1 else 0 ) + ( if Date.DayOfWeek ( EndDate )=6 then -1 else 0 ), HolidaysInRange = if Holidays = null then {} else List.Select(Holidays, each StartDate <= _ and _ <= EndDate and Date.DayOfWeek(_, Day.Monday) <5 ), Result = (if Switch then -1 else 1) * ((WeekDaysInRange) - List.Count(HolidaysInRange)) in Result // fnNetWorkDays2 (DateStart as date, DateEnd as date, optional Holidays as list) => let Switch = DateStart > DateEnd, StartDate = if Switch then DateEnd else DateStart, EndDate = if Switch then DateStart else DateEnd, HolidayIntern = if Holidays = null then List.Buffer({}) else List.Buffer(Holidays), CreateListDates =List.Buffer( List.Dates ( StartDate, Duration.TotalDays ( EndDate - StartDate ) +1, #duration(1,0,0,0) )), ExcludeWeekend = List.Select ( CreateListDates, each Date.DayOfWeek ( _, Day.Monday ) <5 ), ExcludeHoliday = if Holidays = null then ExcludeWeekend else List.Difference ( ExcludeWeekend, HolidayIntern ), Result = List.Count ( ExcludeHoliday ), FinalResult = try (if Switch then -1 else 1) * (Result) otherwise null in FinalResultIf this post helps or solves your problem, please mark it as solution.
Kudos are nice to - thanks
Have fun
Jimmy - 6 years ago
Hey
I was just now that i posted both version.. and this confused you 🙂 sorry 🙂
// fnNetWorkDays2 (DateStart as date, DateEnd as date, optional Holidays as list) => let Switch = DateStart > DateEnd, StartDate = if Switch then DateEnd else DateStart, EndDate = if Switch then DateStart else DateEnd, HolidayIntern = if Holidays = null then List.Buffer({}) else List.Buffer(Holidays), CreateListDates =List.Buffer( List.Dates ( StartDate, Duration.TotalDays ( EndDate - StartDate ) +1, #duration(1,0,0,0) )), ExcludeWeekend = List.Select ( CreateListDates, each Date.DayOfWeek ( _, Day.Monday ) <5 ), ExcludeHoliday = if Holidays = null then ExcludeWeekend else List.Difference ( ExcludeWeekend, HolidayIntern ), Result = List.Count ( ExcludeHoliday ), FinalResult = try (if Switch then -1 else 1) * (Result) otherwise null in FinalResultinvoke it like this
=try fn([Start],[End]) otherwise null
If this post helps or solves your problem, please mark it as solution.
Kudos are nice to - thanks
Have fun
Jimmy - 6 years ago
😪ğŸ˜
oooh. .how sad
Hi ,
Days ( hours is good to know too)
Hello
this function gives back the diffence in hours, excluding the weekends.
You have to pass starttime and endtime. You can pass optionally a list with holidays to exclude them to
(StartTime as datetime, EndTime as datetime, ListHoliday as nullable list) as number =>
let
//StartTime = #datetime(2019,12,3,8,00,00),
//EndTime = #datetime(2019, 12, 8, 8, 0, 0),
//ListHoliday = {"03.12.19", "04.12.19"},
ListHolidayCheck = if ListHoliday = null then {} else ListHoliday,
ListHolidayInternal = List.Transform(ListHolidayCheck, each Date.From(_)),
DurationInHours = (Number.From(EndTime)-Number.From(StartTime))*24,
DateTimesFromStartEnd = List.DateTimes(StartTime, DurationInHours, #duration(0,1,0,0)),
FilterHolidays = List.Transform(DateTimesFromStartEnd, each if List.Contains(ListHolidayInternal, Date.From(_))= true then null else _),
TableWithDateTime = #table({"DateTime"}, List.Zip({FilterHolidays})),
#"Added Custom" = Table.AddColumn(TableWithDateTime, "Hours", each 1),
#"Inserted Day of Week" = Table.AddColumn(#"Added Custom", "Day of Week", each Date.DayOfWeek([DateTime], Day.Monday), Int64.Type),
#"Filtered Rows" = Table.SelectRows(#"Inserted Day of Week", each ([Day of Week] < 5)),
HoursSum = List.Sum(#"Filtered Rows"[Hours])
in
HoursSum
If this post helps or solves your problem, please mark it as solution.
Kudos are nice to - thanks
Have fun
Jimmy
- Ritaf6 years agoResponsive Resident
Hi, this is one of solutions i tried an i get an error:
Expression.Error: We cannot convert the value null to type DateTime.
Details:
Value=
Type=[Type]How can i solve that?