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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
nony97
Regular Visitor

Dax that calculate employee hours

i wrote the following dax query that calculate work hourse 8am to 5 pm however i need it to exclude the weekend (exclude saturday and friyday) please help 

for example this is how it currenlty is being calculatined

start dateend datework hourse
4/22/2022 3:004/23/2022 14:04:0015

 this is how i want it to be 

start dateend datework hourse
4/22/2022 3:004/23/2022 14:04:000

 

and here is the query 

 

workhours new = 
-- Working Start and End time 
VAR WorkTimeStart = TIME ( 08, 00, 00 )
VAR WorkTimeEnd = TIME ( 17, 10, 10 )
VAR WorkingHours = ( WorkTimeEnd - WorkTimeStart )

-- Start and End date/time on current row
VAR StartingDateTime = [start date]
VAR EndingDateTime   = [end date]

VAR StartingTime= StartingDateTime - TRUNC ( StartingDateTime )
VAR StartingDate = StartingDateTime - StartingTime

VAR EndingTime = EndingDateTime - TRUNC ( EndingDateTime )
VAR EndingDate = EndingDateTime - EndingTime

-- Adjust start/end times to fall within working hours.
VAR StartingTimeEffective =
    MIN (
        MAX ( StartingTime, WorkTimeStart ),
        WorkTimeEnd
    )
VAR EndingTimeEffective =
    MAX (
        MIN ( EndingTime, WorkTimeEnd ),
        WorkTimeStart
    )
-- Adjust for hours not worked on StartingDate
--   StartingTimeOffset will always be <= 0
VAR StartingTimeOffset =
    WorkTimeStart - StartingTimeEffective
-- Adjust for hours not worked on EndingDate
--   EndingTimeOffset will always be <= 0
VAR EndingTimeOffset =
    EndingTimeEffective - WorkTimeEnd
VAR DayCount =
    EndingDate - StartingDate + 1
VAR TotalTimeInDays =
    DayCount * WorkingHours + StartingTimeOffset + EndingTimeOffset
VAR TotalTimeInHours =
    TotalTimeInDays * 24

RETURN
    TotalTimeInHours

 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

The standard approach is to decide on the desired level of granularity  (does it need to be minutes, or are quarter hours or higher enough?), then you GENERATESERIES of these timeslots and finally you do an INTERSECT with your "permitted"  date range excluding your definition of weekend and holidays.

 

This becomes interesting when your users work across timezones or have regional holiday differences.

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

The standard approach is to decide on the desired level of granularity  (does it need to be minutes, or are quarter hours or higher enough?), then you GENERATESERIES of these timeslots and finally you do an INTERSECT with your "permitted"  date range excluding your definition of weekend and holidays.

 

This becomes interesting when your users work across timezones or have regional holiday differences.

No that is not acceptable, because if the order came on Saturday it will start counting the hours which us wrong 

Real life may not care what you consider to be "wrong".  Better be prepared for such a scenario.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors