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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
mina97
Helper III
Helper III

hour difference without workhours

i have the following data 

 

 

start date end datestatus
2023-07-02 6:01:002023-07-04 15:10:11confirmed

 

1- i want to calculate the time difference in hours only if it is between 8:00:00 and 17:10:00 so the example upove should be 25 hours

 

2- and i want the same thing but in minutes 

please help me and thank you  

 

3 ACCEPTED SOLUTIONS
OwenAuger
Super User
Super User

Hi @mina97 

I have attached a PBIX with suggested calculated colums in DAX for Work Hours and Work Minutes.

The code is a bit long-winded in order to capture the required logic.

I assumed:

  • Weekends/holidays do not need to be excluded. If you do, you would need to adjust and use NETWORKDAYS.
  • You want to round down to the nearest integer.

Here is the expression for Work Hours (Work Minutes is similar):

Work Hours = 
-- 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 = Data[start date]
VAR EndingDateTime   = Data[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
VAR TotalTimeInHoursRounded =
    ROUNDDOWN ( TotalTimeInHours, 0 )
RETURN
    TotalTimeInHoursRounded

This is a good article on a similar but different calculation:

https://www.sqlbi.com/blog/alberto/2019/03/25/using-dax-with-datetime-values/

 

Does this work for you?

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

Hi @mina97 

Glad it helped.

The expression does just count working hours.

The variable TotalTimeInDays does just include working hours, with an adjustment for the start/end times.

 

But let me know if you have a specific example where it is not working.

Regards

 

OwenAuger_1-1696891115061.png

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

That's odd - it's in my post above, but I reattached here in case that helps.

 

Here is the link created by the forum:

https://community.fabric.microsoft.com/oxcrx34285/attachments/oxcrx34285/DAXCommands/131682/1/Work%2...

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

9 REPLIES 9
OwenAuger
Super User
Super User

Hi @mina97 

I have attached a PBIX with suggested calculated colums in DAX for Work Hours and Work Minutes.

The code is a bit long-winded in order to capture the required logic.

I assumed:

  • Weekends/holidays do not need to be excluded. If you do, you would need to adjust and use NETWORKDAYS.
  • You want to round down to the nearest integer.

Here is the expression for Work Hours (Work Minutes is similar):

Work Hours = 
-- 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 = Data[start date]
VAR EndingDateTime   = Data[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
VAR TotalTimeInHoursRounded =
    ROUNDDOWN ( TotalTimeInHours, 0 )
RETURN
    TotalTimeInHoursRounded

This is a good article on a similar but different calculation:

https://www.sqlbi.com/blog/alberto/2019/03/25/using-dax-with-datetime-values/

 

Does this work for you?

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

but you did not exclude the out of working hours 

Hi @mina97 

Glad it helped.

The expression does just count working hours.

The variable TotalTimeInDays does just include working hours, with an adjustment for the start/end times.

 

But let me know if you have a specific example where it is not working.

Regards

 

OwenAuger_1-1696891115061.png

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

your work is amazing !! but how to shift to calculate minutes 

🙂

The PBIX attached earlier has a "Work Minutes" calculated column that is essentially Work Hours * 60.


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

i can not download the file 😞

That's odd - it's in my post above, but I reattached here in case that helps.

 

Here is the link created by the forum:

https://community.fabric.microsoft.com/oxcrx34285/attachments/oxcrx34285/DAXCommands/131682/1/Work%2...

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

thank you so much

 

thank you it helped

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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