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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
MateoBI
New Member

Calculate Working Date Between two dates Error

Hello developpers,

 

I use the fonction for calculate the number of working date between two dates.

The function work but not all time. He doesn't work when StartDate > EndDate.

 

The function : 

(StartDate as date, EndDate as date, Holidays as list) as number =>
let
DateList = List.Dates(StartDate, Number.From(EndDate- StartDate), #duration(1, 0, 0, 0)),
RemoveWeekends = List.Select(DateList, each Date.DayOfWeek(_, Day.Monday) < 5),
RemoveHolidays = List.RemoveItems(RemoveWeekends, Holidays),
CountDays = List.Count(RemoveHolidays)
in
Number.Abs(CountDays)

 

The result : 

 

MateoBI_0-1673015734216.png

 

Help me plz ...

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @MateoBI ,

 

According to your description, here are my steps you can follow as a solution.

(1) This is my test data. 

Holidays:

vtangjiemsft_0-1673234596770.png

Table1:

vtangjiemsft_0-1673241662145.png

(2) Open Advanced Editor and paste the following code.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddBLCoAwDIThu2TdwCTa11lK738NlZSKwSyHj38zYxCEUVmFEinUBmimW/QrzzQ4XNJ2cjopjGySnXSW1ZRQaigtlB6JIBR/wSv6K/MC", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [StartDate = _t, EndDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type date}, {"EndDate", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Count(
            if 
                [StartDate] >[EndDate]
            then
            
                  List.Select(
                List.Difference(
                    List.Dates(
                        [EndDate],  
                        Duration.Days
                            (
                                [StartDate] - [EndDate]
                            ) + 1
                        ,   
                        #duration(1, 0, 0, 0)
                    ),
                    Holidays[Holiday]
                ),
                each Date.DayOfWeek(_,Day.Monday) < 5
            )
            
            else
            
            List.Select(
                List.Difference(
                    List.Dates(
                        [StartDate],  
                        Duration.Days
                            (
                                [EndDate] - [StartDate]
                            ) + 1
                        ,   
                        #duration(1, 0, 0, 0)
                    ),
                    Holidays[Holiday]
                ),
                each Date.DayOfWeek(_,Day.Monday) < 5
            )
        )),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each -[Custom]),
    #"Added Conditional Column" = Table.AddColumn(#"Added Custom1", "Custom.2", each if [StartDate] <= [EndDate] then [Custom] else [Custom.1]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column",{"Custom", "Custom.1"})
in
    #"Removed Columns"

(3)Or we can create a calculated column.

Work Days 2 = NETWORKDAYS('Table1'[StartDate],'Table1'[EndDate],1, distinct('Holidays'[Holiday])) 

(4) Then the result is as follows.

vtangjiemsft_2-1673242218312.png

Please refer to my PBIX.

 

Please refer to the following document for more information.

Power BI: DAX Function NETWORKDAYS | by Amit Chandak | Medium

Calculate Days between dates using Power Query / Power BI — The Power User

Power Query M code - find number of working days b... - Microsoft Power BI Community

 

Best Regards,

Neeko Tang

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

2 REPLIES 2
Anonymous
Not applicable

Hi @MateoBI ,

 

According to your description, here are my steps you can follow as a solution.

(1) This is my test data. 

Holidays:

vtangjiemsft_0-1673234596770.png

Table1:

vtangjiemsft_0-1673241662145.png

(2) Open Advanced Editor and paste the following code.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddBLCoAwDIThu2TdwCTa11lK738NlZSKwSyHj38zYxCEUVmFEinUBmimW/QrzzQ4XNJ2cjopjGySnXSW1ZRQaigtlB6JIBR/wSv6K/MC", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [StartDate = _t, EndDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type date}, {"EndDate", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Count(
            if 
                [StartDate] >[EndDate]
            then
            
                  List.Select(
                List.Difference(
                    List.Dates(
                        [EndDate],  
                        Duration.Days
                            (
                                [StartDate] - [EndDate]
                            ) + 1
                        ,   
                        #duration(1, 0, 0, 0)
                    ),
                    Holidays[Holiday]
                ),
                each Date.DayOfWeek(_,Day.Monday) < 5
            )
            
            else
            
            List.Select(
                List.Difference(
                    List.Dates(
                        [StartDate],  
                        Duration.Days
                            (
                                [EndDate] - [StartDate]
                            ) + 1
                        ,   
                        #duration(1, 0, 0, 0)
                    ),
                    Holidays[Holiday]
                ),
                each Date.DayOfWeek(_,Day.Monday) < 5
            )
        )),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each -[Custom]),
    #"Added Conditional Column" = Table.AddColumn(#"Added Custom1", "Custom.2", each if [StartDate] <= [EndDate] then [Custom] else [Custom.1]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column",{"Custom", "Custom.1"})
in
    #"Removed Columns"

(3)Or we can create a calculated column.

Work Days 2 = NETWORKDAYS('Table1'[StartDate],'Table1'[EndDate],1, distinct('Holidays'[Holiday])) 

(4) Then the result is as follows.

vtangjiemsft_2-1673242218312.png

Please refer to my PBIX.

 

Please refer to the following document for more information.

Power BI: DAX Function NETWORKDAYS | by Amit Chandak | Medium

Calculate Days between dates using Power Query / Power BI — The Power User

Power Query M code - find number of working days b... - Microsoft Power BI Community

 

Best Regards,

Neeko Tang

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

 

amitchandak
Super User
Super User

@MateoBI , would you like to use newtorkday DAX function ?

Power BI - Business day with and without using DAX Function NETWORKDAYS: https://www.youtube.com/watch?v=Qs03ZZXXE_c

https://amitchandak.medium.com/power-bi-dax-function-networkdays-5c8e4aca38c

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors