Forum Discussion

sulleyinoz's avatar
sulleyinoz
Advocate II
2 years ago
Solved

Power Query - Time Intelligence Table - Week Days or working days in Month

So I have a very comprehensive Time Intelligence table at the core of my system which creates dates from 1 July 2019 to 30 June 2030 and has tonnes of columns for fiscal terms.   I have a column th...
  • sulleyinoz's avatar
    sulleyinoz
    2 years ago

    I already have about 28 columns in teh Calendar table, so needed something that worked with what was already there ... 

    Ended up appending this to the existing table ... which has worked ... the below code is from the ADvanced Editor in Power Query. Hopefully this helps someone else. Thx


    // Calculate the number of weekdays in each month separately

     

    #"Add IsWeekday" = Table.AddColumn(#"Andre-DayNumber", "IsWeekday", each if Date.DayOfWeek([Date], Day.Monday) < 5 then 1 else 0),

     

    #"Filter Weekdays" = Table.SelectRows(#"Add IsWeekday", each [IsWeekday] = 1),

     

    #"Group By Year and Month" = Table.Group(#"Filter Weekdays", {"Year", "MonthNum"}, {{"WeekdaysInMonth", each Table.RowCount(_), Int64.Type}}),

     

    // Merge the weekdays count back into the original table
    #"Merged Weekdays" = Table.NestedJoin(#"Andre-DayNumber", {"Year", "MonthNum"}, #"Group By Year and Month", {"Year", "MonthNum"}, "WeekdayCounts", JoinKind.LeftOuter),

     

    #"Expanded WeekdayCounts" = Table.ExpandTableColumn(#"Merged Weekdays", "WeekdayCounts", {"WeekdaysInMonth"})
    in

    #"Expanded WeekdayCounts"