cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
heytherejem Post Patron
Post Patron

A M-Code equivalent for =NETWORKDAYS() in excel?

So I have a start and an end date and I need to calculate turnaround time in working days.

Excel has NETWORKDAYS which does the removing of weekends all by itself. 


Is there an equivalent for M-Code? I have googled and can only find complex code to work out days, weeks, weekends etc and subtracting these.

 

Thanks!

Jemma

1 ACCEPTED SOLUTION

Accepted Solutions
Chihiro Solution Sage
Solution Sage

Re: A M-Code equivalent for =NETWORKDAYS() in excel?

You would invoke it in your original table as Add Columns->Invoke Custom Function. Set each argument as column and use your start date column and end date column.

 

Edit: Or alternately use it in Advanced Editor, and pass on Start & End as variable (from calculation, another query etc). 

 

Oh, custom function should be copied and pasted into new blank query (using advanced editor). Name it something that makes sense. (Ex: fnWorkday).

View solution in original post

5 REPLIES 5
damonsmithies
Regular Visitor

Re: A M-Code equivalent for =NETWORKDAYS() in excel?

Could you not create a custom dates table and denote when a date is a weekend and when it is a weekday?

 

See @BhaveshPatel solution here: https://community.powerbi.com/t5/Desktop/Date-Duration-exclude-weekends/td-p/87735

Highlighted
Chihiro Solution Sage
Solution Sage

Re: A M-Code equivalent for =NETWORKDAYS() in excel?

No built-in "M" function to do it. You can create custom function like below.

Ex: Week starting Monday, Sat & Sun as weekend.

(sDate as date, eDate as date)=>
let
    vDur = Duration.Days(Duration.From(eDate - sDate)),
    Source = List.Dates(sDate, vDur + 1, #duration(1, 0, 0, 0)),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Added Custom" = Table.AddColumn(#"Converted to Table", "Weekday", each Date.DayOfWeek([Column1],Day.Monday)),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Weekday] <> 5 and [Weekday] <> 6)),
    Row_Count = Table.RowCount(#"Filtered Rows")
in
    Row_Count

 

Edit: There are other ways to write custom function (such as using List.Generate/List.Accumulate), but above should be easier to maintain/change if you needed to.

heytherejem Post Patron
Post Patron

Re: A M-Code equivalent for =NETWORKDAYS() in excel?

Hello again Chihiro,

 

Where should I insert your formula? Into a new custom column within Query Editor? 

 

I am trying to avoid the creation of a new table with a row of dates - it seems a fairly inefficient way of doing something so I don't mind a function even if it is complex if it does the job in one column or measure!

 

🙂

Jemma

Chihiro Solution Sage
Solution Sage

Re: A M-Code equivalent for =NETWORKDAYS() in excel?

You would invoke it in your original table as Add Columns->Invoke Custom Function. Set each argument as column and use your start date column and end date column.

 

Edit: Or alternately use it in Advanced Editor, and pass on Start & End as variable (from calculation, another query etc). 

 

Oh, custom function should be copied and pasted into new blank query (using advanced editor). Name it something that makes sense. (Ex: fnWorkday).

View solution in original post

CodySydney
Frequent Visitor

Re: A M-Code equivalent for =NETWORKDAYS() in excel?

Thanks! I suspect this is something I'm expected to do in DAX but I can think in M, DAX gives me headaches 🙂

 

Helpful resources

Announcements
Announcing the New Spanish Forum

Announcing the New Spanish Forum

Do you need help in Spanish? Check out our new Spanish community section.

April 2020 Community Highlights

April 2020 Community Highlights

Info on our Super Users, MBAS content and badges, and updates to our support articles. - Read the full Community Highlights.

MBAS Gallery 2020

MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

Top Solution Authors
Top Kudoed Authors