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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Walter56
Frequent Visitor

Power BI Calendar with Specific Days of Week and Hours to Count

Hello,

 

I am using PowerBI for an analysis and I am a bit stuck on how to do one specific calculation. I have the times that an shift of employees (for simplicity) started working and the time they stopped. In this example they can work for multiple days at a time. So one row will have the information about that shift as well as their start date/time (say 10/1/2023 at 11am) to their stop date/time (say 10/3/2023 at 12pm).

 

I need to do an analysis to count how many hours each shift worked that were specifically weekends (Saturdays and Sundays) from 8am-5pm. The purpose of this is to calculate the hours paid that could have been avoidable overtime if it was not a weekend. I know how to create a calendar and how to flag specific days as weekends, but not sure how to include the time element or how to do a count to see how many hours each employee shift worked that was avoidable overtime.

 

Could someone help to point me in the right direction or idea? Thanks

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

Hi @Walter56 ,

Base on your description, it seems that you want to get the hours which exclude the weekends. And the normal working time is from 8:00 AM to 5:00 PM. You can refer the following thread to get it:

Solved: Re: Calculating working hours between 2 dates disr... - Microsoft Fabric Community

Working Hours =
VAR StartDate = 'YourTable'[Start Date/Time]
VAR EndDate = 'YourTable'[End Date/Time]
VAR BusinessHoursStart =
    TIME ( 8, 0, 0 )
VAR BusinessHoursEnd =
    TIME ( 17, 0, 0 )
VAR BusinessDays =
    COUNTROWS (
        FILTER (
            'YourCalendarTable',
            'YourCalendarTable'[Date] >= StartDate
                && 'YourCalendarTable'[Date] <= EndDate
                && WEEKDAY ( 'YourCalendarTable'[Date], 2 ) < 6
                && NOT ( 'YourCalendarTable'[Date] IN VALUES ( 'YourHolidaysTable'[Date] ) )
        )
    )
VAR StartHour =
    IF (
        WEEKDAY ( StartDate, 2 ) < 6,
        MAX (
            BusinessHoursStart,
            TIME ( HOUR ( StartDate ), MINUTE ( StartDate ), SECOND ( StartDate ) )
        ),
        BusinessHoursStart
    )
VAR EndHour =
    IF (
        WEEKDAY ( EndDate, 2 ) < 6,
        MIN (
            BusinessHoursEnd,
            TIME ( HOUR ( EndDate ), MINUTE ( EndDate ), SECOND ( EndDate ) )
        ),
        BusinessHoursEnd
    )
RETURN
    IF (
        BusinessDays > 0,
        ( BusinessDays - 1 ) * ( BusinessHoursEnd - BusinessHoursStart ) + ( EndHour - StartHour ),
        0
    )

If the above one can't help you get the expected result, please provide some raw data in the table (exclude sensitive data) with Text format and your expected result with backend logic and special examples.  It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

 

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Community Support Team _ Rena
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
v-yiruan-msft
Community Support
Community Support

Hi @Walter56 ,

Base on your description, it seems that you want to get the hours which exclude the weekends. And the normal working time is from 8:00 AM to 5:00 PM. You can refer the following thread to get it:

Solved: Re: Calculating working hours between 2 dates disr... - Microsoft Fabric Community

Working Hours =
VAR StartDate = 'YourTable'[Start Date/Time]
VAR EndDate = 'YourTable'[End Date/Time]
VAR BusinessHoursStart =
    TIME ( 8, 0, 0 )
VAR BusinessHoursEnd =
    TIME ( 17, 0, 0 )
VAR BusinessDays =
    COUNTROWS (
        FILTER (
            'YourCalendarTable',
            'YourCalendarTable'[Date] >= StartDate
                && 'YourCalendarTable'[Date] <= EndDate
                && WEEKDAY ( 'YourCalendarTable'[Date], 2 ) < 6
                && NOT ( 'YourCalendarTable'[Date] IN VALUES ( 'YourHolidaysTable'[Date] ) )
        )
    )
VAR StartHour =
    IF (
        WEEKDAY ( StartDate, 2 ) < 6,
        MAX (
            BusinessHoursStart,
            TIME ( HOUR ( StartDate ), MINUTE ( StartDate ), SECOND ( StartDate ) )
        ),
        BusinessHoursStart
    )
VAR EndHour =
    IF (
        WEEKDAY ( EndDate, 2 ) < 6,
        MIN (
            BusinessHoursEnd,
            TIME ( HOUR ( EndDate ), MINUTE ( EndDate ), SECOND ( EndDate ) )
        ),
        BusinessHoursEnd
    )
RETURN
    IF (
        BusinessDays > 0,
        ( BusinessDays - 1 ) * ( BusinessHoursEnd - BusinessHoursStart ) + ( EndHour - StartHour ),
        0
    )

If the above one can't help you get the expected result, please provide some raw data in the table (exclude sensitive data) with Text format and your expected result with backend logic and special examples.  It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

 

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Greg_Deckler
Super User
Super User

@Walter56 See if this approach gets you there:

Hour Breakdown - Microsoft Fabric Community


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

Check out the July 2024 Power BI update to learn about new features.

July Newsletter

Fabric Community Update - July 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors