Forum Discussion
WORKDAYS formula in Power Query
- 6 years ago
Take a look at the M code below. Put it in a blank query. It returns this table:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Donelet Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJQ0lECMSDsWJ1oJROEhIm+kTFUIhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Start Date" = _t, #"End Date" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}), #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1), #"Added Date Range" = Table.AddColumn(#"Added Index", "Date Range", each {Number.From([Start Date])..Number.From([End Date])}), #"Expanded Date Range" = Table.ExpandListColumn(#"Added Date Range", "Date Range"), #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Date Range",{{"Date Range", type date}}), #"Added Custom" = Table.AddColumn(#"Changed Type1", "Is workday", each if Date.DayOfWeek([Date Range],Day.Monday) <= 4 then 1 else 0), #"Grouped Rows" = Table.Group(#"Added Custom", {"Start Date", "End Date", "Index"}, {{"Workdays", each List.Sum([Is workday]), type number}}), #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Start Date", "End Date", "Workdays"}) in #"Removed Other Columns"You should be able to modify it to suite your specific scenario. Ping back if you cannot or need help.
Take a look at the M code below. Put it in a blank query. It returns this table:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJQ0lECMSDsWJ1oJROEhIm+kTFUIhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Start Date" = _t, #"End Date" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"Added Date Range" = Table.AddColumn(#"Added Index", "Date Range", each {Number.From([Start Date])..Number.From([End Date])}),
#"Expanded Date Range" = Table.ExpandListColumn(#"Added Date Range", "Date Range"),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Date Range",{{"Date Range", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type1", "Is workday", each if Date.DayOfWeek([Date Range],Day.Monday) <= 4 then 1 else 0),
#"Grouped Rows" = Table.Group(#"Added Custom", {"Start Date", "End Date", "Index"}, {{"Workdays", each List.Sum([Is workday]), type number}}),
#"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Start Date", "End Date", "Workdays"})
in
#"Removed Other Columns"
You should be able to modify it to suite your specific scenario. Ping back if you cannot or need help.