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
Vivek26
Frequent Visitor

Calculate number of hours between 2 dates excluding Weekends

I have a specific scenario where I need to calculate the number of hours between 2 dates excluding Weekends.

we get orders that move through different stages and we want to calculate how much time each stage is taking on an average.

 

For example , for an order the step 1 completed on  30th Dec' 2022 at 11 PM 

while the step 2nd completed on  2nd Jan 2022 at 8 AM.

The time taken for step 2 should be shown as 9 hours ( Excluding Saturday and Sunday ) and not 57 hours.

This should be accounted for while calculating all averages.

I really appreciate the help here 

 

Vivek26_0-1672849317962.png

 

 

1 ACCEPTED SOLUTION
v-cgao-msft
Community Support
Community Support

Hi @Vivek26 ,

 

Assuming that the dates of all steps are workdays, try this:

Hours = 
VAR _time_1 =
    CALCULATE (
        MAX ( 'Table'[Status Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Order NO] = EARLIER ( 'Table'[Order NO] )
                && 'Table'[Step] < EARLIER ( 'Table'[Step] )
        )
    )
VAR _hours =
    IF (
        ISBLANK ( _time_1 ),
        0,
        DIVIDE ( DATEDIFF ( _time_1, 'Table'[Status Date], SECOND ), 3600 )
            - MAX (
                DATEDIFF ( _time_1, 'Table'[Status Date], DAY ) + 1
                    - NETWORKDAYS ( _time_1, 'Table'[Status Date] ),
                0
            ) * 24
    )
RETURN
    _hours

vcgaomsft_0-1672901235265.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

View solution in original post

2 REPLIES 2
v-cgao-msft
Community Support
Community Support

Hi @Vivek26 ,

 

Assuming that the dates of all steps are workdays, try this:

Hours = 
VAR _time_1 =
    CALCULATE (
        MAX ( 'Table'[Status Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Order NO] = EARLIER ( 'Table'[Order NO] )
                && 'Table'[Step] < EARLIER ( 'Table'[Step] )
        )
    )
VAR _hours =
    IF (
        ISBLANK ( _time_1 ),
        0,
        DIVIDE ( DATEDIFF ( _time_1, 'Table'[Status Date], SECOND ), 3600 )
            - MAX (
                DATEDIFF ( _time_1, 'Table'[Status Date], DAY ) + 1
                    - NETWORKDAYS ( _time_1, 'Table'[Status Date] ),
                0
            ) * 24
    )
RETURN
    _hours

vcgaomsft_0-1672901235265.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

IIPowerBlog
Helper I
Helper I

hi @Vivek26  have you tried to revert the Step values (rows) to a column? this will provide you with the opportuinity to create a measure or a calculated field with a Date Difference between steps, according to your needs.

https://learn.microsoft.com/en-us/power-query/transpose-table 

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