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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Oded-Dror
Helper III
Helper III

TSQL calculating Hours to DAX

Hi there,

 

I have TSQL function that calculates Weekdays Hours and I tried to convert it to DAX but I fall short 

Here is the TSQL function my question how do  I convert it to DAX ?

CREATE FUNCTION [dbo].[fn_getWeekDaysHour] (@StartDate DATETIME, @EndDate DATETIME)
RETURNS DECIMAL(6,2)
AS
BEGIN
DECLARE @TotalHour DECIMAL(6,2)
SET @TotalHOur=
(
( DATEDIFF(MINUTE, @StartDate, @EndDate)
- ( DATEDIFF(wk, @StartDate,@EndDate)*(2*24*60)
-- End on Sunday
-(CASE WHEN DATEPART(dw, @EndDate) = 1 THEN 24.0*60-DATEDIFF(MINUTE,CONVERT(DATE,@EndDate),@EndDate) ELSE 0 END)
-- Start on Saturday
-(CASE WHEN DATEPART(dw, @StartDate) = 7 THEN DATEDIFF(MINUTE,CONVERT(DATE,@StartDate),@StartDate) ELSE 0 END)
-- End on Saturday
+(CASE WHEN DATEPART(dw, @EndDate) = 7 THEN DATEDIFF(MINUTE,CONVERT(DATE,@EndDate),@EndDate) ELSE 0 END)
-- Start on Saturday
+(CASE WHEN DATEPART(dw, @StartDate) = 1 THEN 24.0*60-DATEDIFF(MINUTE,CONVERT(DATE,@StartDate),@StartDate) ELSE 0 END)
))/60
)

RETURN @TotalHour

END

--- To test
Select dbo.fn_getWeekDaysHour('2022-03-20 12:06:58 AM','2022-03-25 08:48:34 AM') -- 104.80 Hrs

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Oded-Dror , refer if this blog on business hour can help

https://exceleratorbi.com.au/calculating-business-hours-using-dax/

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

2 REPLIES 2
Oded-Dror
Helper III
Helper III

Thanks

amitchandak
Super User
Super User

@Oded-Dror , refer if this blog on business hour can help

https://exceleratorbi.com.au/calculating-business-hours-using-dax/

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors