Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Calculate hours between two datetime excluding weekends and non working hours

Dear Experts, I want to calculate hours between 2 dates , CreationDate and CompletionDate   Please exclude Saturday and Sunday Include only 9:00 AM to 6:00 PM hours   Will be very very great ...
  • parry2k's avatar
    4 years ago

    Anonymous check this video that talks about a similar question.

     

    ✨ Follow us on LinkedIn

     

    Learn about conditional formatting at Microsoft Reactor

    My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would ❤ Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

     

    ⚡ Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.⚡

     

  • v-chenwuz-msft's avatar
    v-chenwuz-msft
    4 years ago

    Hi Anonymous ,

     

    Please try this code

    WorkHour =
    VAR _allmin =
        GENERATESERIES(
            MAX( 'Test Data'[Creation Date] ),
            MAX( 'Test Data'[Completion Date] ),
            1 / 1440
        )
    VAR _workmin =
        FILTER(
            ADDCOLUMNS(
                _allmin,
                "workday", WEEKDAY( [Value], 2 ),
                "worktime",
                    IF( AND( HOUR( [Value] ) >= 9, HOUR( [Value] ) <= 17 ), 1, BLANK() )
            ),
            [workday] < 6
                && [worktime] = 1
        )
    RETURN
        COUNTROWS( _workmin ) / 60
    

    Result:

     

    Need more attention:

    Your created time is text from you provided pbix file, you should change it to date/time type. I recommend use the power query edito to shape it.

     

    PBIX file in the end you can refer.

     

    Best Regards

    Community Support Team _ chenwu zhu

     

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