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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Help: Calculating Duration in hours excluding weekends

Hi,

 

I am calculating the duration between 2 events.

We have an event start date and event end date.

Both fields are DATETIME.

 

I can calculate the duration between the two events and transform to total hours easily.

 

How can I remove weekends from the calculation?

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Ended up using this.

 

Worked perfectly.

 

 

Datediff(HH, Event_Date_Start, Event_Date_end)
+ CASE WHEN Datepart(dw, Event_Date_Start) = 7 THEN 1 ELSE 0 END
- (Datediff(wk, Event_Date_Start, Event_Date_end) * 48 )
- CASE WHEN Datepart(dw, Event_Date_Start) = 1 THEN 1 ELSE 0 END +
- CASE WHEN Datepart(dw, Event_Date_end) = 1 THEN 1 ELSE 0
end

 

You can change the 48 to 2 if you want this to work in days.

View solution in original post

5 REPLIES 5
prateekraina
Memorable Member
Memorable Member

Hi @Anonymous,

Why dont you use a time dimension table in which you can have 'IsWeekDay' flag.
Create relationship between this dimension table with your other table based on date.
And then use this flag in your duration calculation.

I have just suggested an approach to you, you can try it.

Thanks !!

Anonymous
Not applicable

Thanks! Do you have a link to a thread where someone has done this?

I'm quite new to Power BI/BI in general.

Hi @Anonymous,

Sorry, not aware of any link.
Just Google DateDimension table and download the SQL script.

In case if that flag is not available then you can create using simple SQL DateTime functions.

Thanks !!

Anonymous
Not applicable

After a bit of reading it seems that it would exclude start and end times on the weekend as well.

 

Just say an event starts on a Thrusday at 14:00 and ends on Monday at 14:00.

I want my duration to be 48 hours and not 96 Hours (48 hours of weekend to be excluded).

 

Or if an item starts on a Saturday or Sunday the timing must only commence on Monday at 00:00.

 

Anyone done something like this in Power BI or sql?

Anonymous
Not applicable

Ended up using this.

 

Worked perfectly.

 

 

Datediff(HH, Event_Date_Start, Event_Date_end)
+ CASE WHEN Datepart(dw, Event_Date_Start) = 7 THEN 1 ELSE 0 END
- (Datediff(wk, Event_Date_Start, Event_Date_end) * 48 )
- CASE WHEN Datepart(dw, Event_Date_Start) = 1 THEN 1 ELSE 0 END +
- CASE WHEN Datepart(dw, Event_Date_end) = 1 THEN 1 ELSE 0
end

 

You can change the 48 to 2 if you want this to work in days.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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