RattlingCarp3048
Board Regular
- Joined
- Jan 12, 2022
- Messages
- 202
- Office Version
- 365
- Platform
- Windows
i need to calculate the difference between 2 date/time columns with the returned value in hours rounded to one decimal place. i have tried several different codes from online, below is the most promising thus far. i opened a blank query and created a custom function using the below code. However, when i invoke the function to test i get an error and when i go back to the query with my data, create a custom column and add the function all i get are errors returned. everything is in date/time/table formats so i'm not sure where i am going wrong.
(StartDate as datetime, EndDate as datetime, BusinessHoursStart as number, BusinessHoursEnd as number, Holidays as table) =>
let
//create date list
DateList = List.Generate(() => StartDate,
each _ <= EndDate,
each _ = #duration(0,0,60,0)),
//convert date list to table
DateListTable =
Table.FromList(DateList, Splitter.SplitByNothing()),
//convert to datetime
DateTimeList =
Table.TransformColumnTypes(DateListTable, {"Column1", type datetime}),
//add weekday column
Weekdays =
Table.AddColumn(DateTimeList, "Weekday", each Date.DayOfWeek([Column1]), Int32.Type),
//exclude weekends
NoWeekends =
Table.SelectRows(Weekdays, each ([Weekday] <> 0 and [Weekday] <> 6)),
//add time column
TimeColAdded =
Table.AddColumn(NoWeekends, "Time", each DateTime.Time([Column1])),
//limited to working hours
WorkingHours =
Table.SelectRows(TimeColAdded, each [Time] > #time(BusinessHoursStart,0,0) and [Time] <= #time(BusinessHoursEnd,0,0)),
//convert date time to date
DateConversion =
Table.TransformColumns(WorkingHours, {{"Column1", DateTime.Date, type date}}),
//exclude holidays
ExclHolidays =
Table.NestedJoin(DateConversion,("Column1"), Holidays, ("Holidays"), "Holidays", JoinKind.LeftAnti),
//count rows
RowCount = Number.Round(Table.RowCount(WorkingHours)/60,1),
ReturnCount = if RowCount <0 then 0 else RowCount
in
ReturnCount
(StartDate as datetime, EndDate as datetime, BusinessHoursStart as number, BusinessHoursEnd as number, Holidays as table) =>
let
//create date list
DateList = List.Generate(() => StartDate,
each _ <= EndDate,
each _ = #duration(0,0,60,0)),
//convert date list to table
DateListTable =
Table.FromList(DateList, Splitter.SplitByNothing()),
//convert to datetime
DateTimeList =
Table.TransformColumnTypes(DateListTable, {"Column1", type datetime}),
//add weekday column
Weekdays =
Table.AddColumn(DateTimeList, "Weekday", each Date.DayOfWeek([Column1]), Int32.Type),
//exclude weekends
NoWeekends =
Table.SelectRows(Weekdays, each ([Weekday] <> 0 and [Weekday] <> 6)),
//add time column
TimeColAdded =
Table.AddColumn(NoWeekends, "Time", each DateTime.Time([Column1])),
//limited to working hours
WorkingHours =
Table.SelectRows(TimeColAdded, each [Time] > #time(BusinessHoursStart,0,0) and [Time] <= #time(BusinessHoursEnd,0,0)),
//convert date time to date
DateConversion =
Table.TransformColumns(WorkingHours, {{"Column1", DateTime.Date, type date}}),
//exclude holidays
ExclHolidays =
Table.NestedJoin(DateConversion,("Column1"), Holidays, ("Holidays"), "Holidays", JoinKind.LeftAnti),
//count rows
RowCount = Number.Round(Table.RowCount(WorkingHours)/60,1),
ReturnCount = if RowCount <0 then 0 else RowCount
in
ReturnCount