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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
nony97
Regular Visitor

Dax query that calculate InWork hours excluding weakeneds

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
v-yilong-msft
Community Support
Community Support

Hi @nony97 ,

I create a table as you mentioned.

vyilongmsft_0-1728626155959.png

Then I think you can use this DAX code.

WorkHours =
VAR StartDate = [StartDate]
VAR EndDate = [EndDate]
VAR WorkingHoursPerDay = 9
VAR StartTime =
    TIME ( 8, 0, 0 )
VAR EndTime =
    TIME ( 17, 0, 0 )
VAR TotalDays =
    DATEDIFF ( StartDate, EndDate, DAY )
VAR Weekdays =
    FILTER (
        GENERATE (
            CALENDAR ( StartDate, EndDate ),
            VAR CurrentDay = [Date]
            RETURN
                ROW (
                    "Day", CurrentDay,
                    "IsWeekday", IF ( WEEKDAY ( CurrentDay, 2 ) IN { 5, 6 }, FALSE, TRUE )
                )
        ),
        [IsWeekday] = TRUE
    )
VAR TotalWeekdays =
    COUNTROWS ( Weekdays )
VAR TotalWorkHours =
    IF (
        TotalDays = 0,
        IF (
            WEEKDAY ( StartDate, 2 )
                IN { 5, 6 }
                    || WEEKDAY ( EndDate, 2 ) IN { 5, 6 },
            0,
            DATEDIFF ( StartDate, EndDate, HOUR )
        ),
        TotalWeekdays * WorkingHoursPerDay
    )
RETURN
    IF ( ISBLANK ( TotalWorkHours ), 0, TotalWorkHours )

vyilongmsft_1-1728626932127.png

 

 

Best Regards

Yilong Zhou

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

9 REPLIES 9
v-yilong-msft
Community Support
Community Support

Hi @nony97 ,

I create a table as you mentioned.

vyilongmsft_0-1728626155959.png

Then I think you can use this DAX code.

WorkHours =
VAR StartDate = [StartDate]
VAR EndDate = [EndDate]
VAR WorkingHoursPerDay = 9
VAR StartTime =
    TIME ( 8, 0, 0 )
VAR EndTime =
    TIME ( 17, 0, 0 )
VAR TotalDays =
    DATEDIFF ( StartDate, EndDate, DAY )
VAR Weekdays =
    FILTER (
        GENERATE (
            CALENDAR ( StartDate, EndDate ),
            VAR CurrentDay = [Date]
            RETURN
                ROW (
                    "Day", CurrentDay,
                    "IsWeekday", IF ( WEEKDAY ( CurrentDay, 2 ) IN { 5, 6 }, FALSE, TRUE )
                )
        ),
        [IsWeekday] = TRUE
    )
VAR TotalWeekdays =
    COUNTROWS ( Weekdays )
VAR TotalWorkHours =
    IF (
        TotalDays = 0,
        IF (
            WEEKDAY ( StartDate, 2 )
                IN { 5, 6 }
                    || WEEKDAY ( EndDate, 2 ) IN { 5, 6 },
            0,
            DATEDIFF ( StartDate, EndDate, HOUR )
        ),
        TotalWeekdays * WorkingHoursPerDay
    )
RETURN
    IF ( ISBLANK ( TotalWorkHours ), 0, TotalWorkHours )

vyilongmsft_1-1728626932127.png

 

 

Best Regards

Yilong Zhou

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

thank you 

but the calculation is not 100% accurate can you plwasw help 

for wxample 

start ebdcuurent result (Hours)Desired result(Hours)
04/06/2024
04/06/20242.001.19
SachinNandanwar
Solution Specialist
Solution Specialist

Do you have a seperate date table that defines the weekends ?

 

 



Regards,
Sachin
Check out my Blog

No unfortunately 

I think its better that you create one.It would make a lot of things much simpler.

DimDate = 
ADDCOLUMNS(
    CALENDAR(DATE(2020, 1, 1), DATE(2030, 12, 31)),   -- Define the date range
    "Year", YEAR([Date]),                              -- Year
    "Month Number", MONTH([Date]),                     -- Month Number (1 to 12)
    "Month Name", FORMAT([Date], "MMMM"),              -- Full Month Name (January, February, etc.)
    "Day", DAY([Date]),                                -- Day of the month
    "Weekday Number", WEEKDAY([Date], 2),              -- Weekday Number (1 = Monday, 7 = Sunday)
    "Weekday Name", FORMAT([Date], "dddd"),            -- Full Weekday Name (Monday, Tuesday, etc.)
    "IsWeekend", IF(WEEKDAY([Date], 2) >= 6, TRUE(), FALSE()), -- Weekend (True/False)
    "Quarter", QUARTER([Date]),                        -- Quarter (1 to 4)
    "Quarter Name", "Q" & QUARTER([Date]),             -- Quarter Name (Q1, Q2, etc.)
    "Year-Quarter", YEAR([Date]) & "-Q" & QUARTER([Date]), -- Year and Quarter (2020-Q1, etc.)
    "Week Number", WEEKNUM([Date], 2),                 -- Week Number of the year
    "Year-Month", FORMAT([Date], "YYYY-MM"),           -- Year and Month (2020-01, etc.)
    "IsLeapYear", IF(MOD(YEAR([Date]), 4) = 0 && (MOD(YEAR([Date]), 100) <> 0 || MOD(YEAR([Date]), 400) = 0), TRUE(), FALSE()) -- Is it a Leap Year?
)


Regards,
Sachin
Check out my Blog

What is the next step after creating it? 

Ignore the data in your calculation that are marked as weekends in the date table.



Regards,
Sachin
Check out my Blog

But what if the 

request came on Saturday at 5 and ended on Sunday, it will calculate the workhours in Saturday which is not what i want

If saturday is marked as weekend in your date table then your calculation should ignore it.



Regards,
Sachin
Check out my Blog

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.