Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not 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 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
1 ACCEPTED SOLUTION
edhans
Super User
Super User

Take a look at the M code below. Put it in a blank query. It returns this table:

2020-04-23 10_26_49-Untitled - Power Query Editor.png

 

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. 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

3 REPLIES 3
edhans
Super User
Super User

Take a look at the M code below. Put it in a blank query. It returns this table:

2020-04-23 10_26_49-Untitled - Power Query Editor.png

 

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. 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
danextian
Super User
Super 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










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Greg_Deckler
Super User
Super User

I do not know a solution for Power Query, @ImkeF or @edhans might.

 

The DAX version is here: https://community.powerbi.com/t5/Quick-Measures-Gallery/Net-Work-Days/td-p/367362


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors