Forum Discussion
Anonymous
6 years agoNot applicable
WORKDAYS formula in Power Query
Hi Guys I am trying to find a solution that exist in EXCEL (WORKDAYS function) that can do the same thing in Power Query Below I have a Delivery Date and I want to substract Shipping and WHS ...
- 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.
danextian
6 years agoSuper User
Hi Anonymous ,
You may try using a custom function to get the workdays. There is a recent thread here: https://community.powerbi.com/t5/Power-Query/WorkDays-Function-in-Power-Query-M/m-p/1007028#M34312