Hi everyone,
@DavidNealon thought it would be a nice addition if there was a Time Table Power Query M code function available in this category as well. Now I can’t take any credit because it was designed by Gina Meronek, you can find it and the full article she wrote here.
When invoked the function creates a Time Table at a granularity of Minutes. If your requirement is different that can easily be fixed. Note that other changes might be required as well but to give you some ideas:
For Hourly intervals: Source = List.Times(#time(0, 0, 0), 24, #duration(0, 1, 0, 0))
30 minute intervals: Source = List.Times(#time(0, 0, 0), 48, #duration(0, 0, 30, 0))
15 minute intervals: Source = List.Times(#time(0, 0, 0), 96, #duration(0, 0, 15, 0))
You can add as many attributes to the table as required, examples present are Working hours and Period of Day but if Shifts or some other kind of grouping make sense to your business feel free to share that with the community in a reply .
let CreateTimeTable = () as table =>
let
Source = List.Times(#time(0, 0, 0), 1440, #duration(0,0,1,0)),
TableFromList = Table.FromList(Source, Splitter.SplitByNothing()),
ChangedType = Table.TransformColumnTypes(TableFromList,{{"Column1", type time}}),
RenamedColumns = Table.RenameColumns(ChangedType,{{"Column1", "Time"}}),
InsertHour = Table.AddColumn(RenamedColumns, "Hour", each Time.StartOfHour([Time]), type time),
InsertMinute = Table.AddColumn(InsertHour, "Minute Number", each Time.Minute([Time]), Int64.Type),
InsertQuarterHour = Table.AddColumn(InsertMinute, "Quarter Hour", each if [Minute Number]<15 then [Hour] else if [Minute Number] < 30 then Value.Add([Hour],#duration(0,0,15, 0)) else if [Minute Number] < 45 then Value.Add([Hour],#duration(0,0,30, 0)) else Value.Add([Hour],#duration(0,0,45, 0)), type time),
InsertHourNumber = Table.AddColumn(InsertQuarterHour, "Hour Number", each Time.Hour([Time]), Int64.Type),
InsertNextHour = Table.AddColumn(InsertHourNumber, "Next Hour", each Value.Add([Hour],#duration(0,1,0, 0)), type time),
InsertNextQuarterHour = Table.AddColumn(InsertNextHour, "Next Quarter Hour", each Value.Add([Quarter Hour],#duration(0,0,15, 0)), type time),
InsertWorkingHours = Table.AddColumn(InsertNextQuarterHour, "Working Hours", each if [Hour Number] >= 8 and [Hour Number] < 17 then "Standard" else if [Hour Number] >= 17 and [Hour Number] < 21 then "Overtime" else null, type text),
InsertPeriod = Table.AddColumn(InsertWorkingHours, "Period of Day", each if [Hour Number] >= 0 and [Hour Number] < 4 then "After Midnight" else if [Hour Number] >= 4 and [Hour Number] < 8 then "Early Morning" else if [Hour Number] >= 8 and [Hour Number] < 12 then "Late Morning" else if [Hour Number] >= 12 and [Hour Number] < 16 then "Afternoon" else if [Hour Number] >= 16 and [Hour Number] < 20 then "Evening" else "Late Night", type text),
InsertPeriodSort = Table.AddColumn(InsertPeriod, "PeriodOfDaySort", each if [Hour Number] >= 0 and [Hour Number] < 4 then 0 else if [Hour Number] >= 4 and [Hour Number] < 8 then 1 else if [Hour Number] >= 8 and [Hour Number] < 12 then 2 else if [Hour Number] >= 12 and [Hour Number] < 16 then 3 else if [Hour Number] >= 16 and [Hour Number] < 20 then 4 else 5, Int64.Type),
InsertTimeKey = Table.AddColumn(InsertPeriodSort, "TimeKey", each Time.ToText([Time], "HHmm"), type text)
in
InsertTimeKey
in
CreateTimeTable
.
Tip. If you want to add the Time Table without having to Invoke the function, you can omit the first let expression and the last in clause.