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
Shuwyyyy
Advocate I
Advocate I

How to exclude the holidays and weekends from number of days by using M code in power bi

Hi All,

 

How to exclude the holidays and weekends from number of days calculations  by using M code in power bi.

In this scenario i got how to exclude the weekends but after that how can i add the logic to exclude the public holidays .

 

Please assist me on this. Thanks in advance!!

 

 Regards,

Pratima

 

 

 

1 ACCEPTED SOLUTION

Hi, @rajulshah 

 

Based on your description, you may create a table of holidays called 'Holidays', and has a single column called Date. 

b1.png

 

Then you may create a new query in the Query Editor, go to 'View' ribbon, click 'Advanced Editor'.

b2.png

 

You may input codes as below.

 

let
    Networkdays = (StartDate as date, EndDate as date) as number =>
let
    Source = List.Dates,
    #"Invoked FunctionSource" =
    if StartDate <= EndDate then
        Source(StartDate,Duration.Days(EndDate-StartDate)+1, Duration.From(1))
    else
        Source(EndDate, Duration.Days(StartDate-EndDate)+1,Duration.From(1)),
    #"Converted to Table" = Table.FromList(#"Invoked FunctionSource", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Inserted Day of Week" = Table.AddColumn(#"Converted to Table", "DayOfWeek", each Date.DayOfWeek([Column1]), type number),
    #"Merged Queries" = Table.NestedJoin(#"Inserted Day of Week",{"Column1"},Holidays,{"Date"},"Holidays",JoinKind.LeftOuter),
    #"Expanded Holidays" = Table.ExpandTableColumn(#"Merged Queries", "Holidays", {"Date"}, {"HolidaysDate"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Holidays", each ([DayOfWeek] <> 0 and [DayOfWeek] <> 6)),
    #"Filtered Rows1" = Table.SelectRows(#"Filtered Rows", each [HolidaysDate] = null),
    Custom1 = if StartDate <= EndDate then Table.RowCount(#"Filtered Rows1") else Table.RowCount(#"Filtered Rows1") * (-1)
in
    Custom1
in
    Networkdays

 

 

Result:

b3.png

b4.png

b5.png

b6.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Tahreem24
Super User
Super User

@Shuwyyyy ,

 

You can refer the below link with accepted solution to meet your requirement.

https://community.powerbi.com/t5/Desktop/How-exclude-holidays-from-networking-days/m-p/785656

 

Please don't forget to hit THUMBS UP and Accept this as a solution if it helps you!

 

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard
rajulshah
Super User
Super User

Hello @Shuwyyyy,

You can create a calculated column in Dates table which has values as "Weekend", "Weekday" & "Holiday".

And then in measure you can filter out only those who has "Weekday" value.

Hi, @rajulshah 

 

Based on your description, you may create a table of holidays called 'Holidays', and has a single column called Date. 

b1.png

 

Then you may create a new query in the Query Editor, go to 'View' ribbon, click 'Advanced Editor'.

b2.png

 

You may input codes as below.

 

let
    Networkdays = (StartDate as date, EndDate as date) as number =>
let
    Source = List.Dates,
    #"Invoked FunctionSource" =
    if StartDate <= EndDate then
        Source(StartDate,Duration.Days(EndDate-StartDate)+1, Duration.From(1))
    else
        Source(EndDate, Duration.Days(StartDate-EndDate)+1,Duration.From(1)),
    #"Converted to Table" = Table.FromList(#"Invoked FunctionSource", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Inserted Day of Week" = Table.AddColumn(#"Converted to Table", "DayOfWeek", each Date.DayOfWeek([Column1]), type number),
    #"Merged Queries" = Table.NestedJoin(#"Inserted Day of Week",{"Column1"},Holidays,{"Date"},"Holidays",JoinKind.LeftOuter),
    #"Expanded Holidays" = Table.ExpandTableColumn(#"Merged Queries", "Holidays", {"Date"}, {"HolidaysDate"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Holidays", each ([DayOfWeek] <> 0 and [DayOfWeek] <> 6)),
    #"Filtered Rows1" = Table.SelectRows(#"Filtered Rows", each [HolidaysDate] = null),
    Custom1 = if StartDate <= EndDate then Table.RowCount(#"Filtered Rows1") else Table.RowCount(#"Filtered Rows1") * (-1)
in
    Custom1
in
    Networkdays

 

 

Result:

b3.png

b4.png

b5.png

b6.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi v-alq-msft ,

 

Thank you so much for the detail explanation.It helped me to get the solution i was looking for.

 

Thanks and Regards,

Pratima

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.