Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

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 days but only days that are working days. 

So in column Release date it is substructing all days including Weekends but what I want to acheive is the Releas date WORKING DAYS ONLY that is substrating only working days. 

 

Hopefully you can help me 

 

Thank you for your efforts 

 

 

 

Del dateShippingWHSSubstractReleas date
Del date - (shipping +WHS)
Releas date WORKING DAYS ONLY
Del date - (shipping +WHS)
27-03-2023522-03-2020-03-20
13-04-2031409-04-2007-04-20
06-04-2041501-04-2030-03-20
02-04-2022429-03-2027-03-20
02-04-2031429-03-2027-03-20
02-04-2011231-03-2031-03-20
02-04-2032528-03-2026-03-20
02-04-2051627-03-2025-03-20
  • 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. 

3 Replies

  • edhans's avatar
    edhans
    Community Champion

    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.