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
BYENER
Helper V
Helper V

Duration in days, hours and minutes incorrect when output is negative

Hi All,

 

I am calculating the duration between planned and actual inbound datetime. I am using the dateiff formula for this and I'm showing the output in this way : 1 days 2 hours 20 minutes. When the actual arival is earlier than planned the calculation will not work. It only working correctly when I use DATEDIFF(average(TBL_SHIPMENTS[Planned DateTime of Arrival]),average(TBL_SHIPMENTS[ACTUAL_ARRIVAL]),SECOND)  but I want to see in this way 1 days 2 hours 20 minutes

BYENER_0-1655978656995.png

 



Measure = 
var vSeconds = DATEDIFF(average(TBL_SHIPMENTS[Planned DateTime of Arrival]),average(TBL_SHIPMENTS[ACTUAL_ARRIVAL]),SECOND)
var vMinutes=int( vSeconds/60)
var vRemainingSeconds=MOD(vSeconds, 60)
var vHours=INT(vMinutes/60)
var vRemainingMinutes=MOD(vMinutes,60)
var vDays=INT(vHours/24)
var vRemainingHours=MOD(vHours,24)
return
vDays&" Days & "&
vRemainingHours&" Hours & "&
vRemainingMinutes&" Minutes & "&
vRemainingSeconds& " Seconds"
1 ACCEPTED SOLUTION
v-zhangti
Community Support
Community Support

Hi, @BYENER 

 

According to the data you provided, there should be a difference of about 13 hours. To avoid the time becoming negative, you can add the function ABS(). https://docs.microsoft.com/en-us/dax/abs-function-dax 

Measure = 
var vSeconds = ABS(DATEDIFF(average(TBL_SHIPMENTS[Planned DateTime of Arrival]),average(TBL_SHIPMENTS[ACTUAL_ARRIVAL]),SECOND))
var vMinutes=int( vSeconds/60)
var vRemainingSeconds=MOD(vSeconds, 60)
var vHours=INT(vMinutes/60)
var vRemainingMinutes=MOD(vMinutes,60)
var vDays=INT(vHours/24)
var vRemainingHours=MOD(vHours,24)
return
vDays&" Days & "&
vRemainingHours&" Hours & "&
vRemainingMinutes&" Minutes & "&
vRemainingSeconds& " Seconds"

vzhangti_0-1656313154283.png

Does this match the output you expect?

 

Best Regards,

Community Support Team _Charlotte

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

2 REPLIES 2
BYENER
Helper V
Helper V

@v-zhangti  thanks, this works fine!!

v-zhangti
Community Support
Community Support

Hi, @BYENER 

 

According to the data you provided, there should be a difference of about 13 hours. To avoid the time becoming negative, you can add the function ABS(). https://docs.microsoft.com/en-us/dax/abs-function-dax 

Measure = 
var vSeconds = ABS(DATEDIFF(average(TBL_SHIPMENTS[Planned DateTime of Arrival]),average(TBL_SHIPMENTS[ACTUAL_ARRIVAL]),SECOND))
var vMinutes=int( vSeconds/60)
var vRemainingSeconds=MOD(vSeconds, 60)
var vHours=INT(vMinutes/60)
var vRemainingMinutes=MOD(vMinutes,60)
var vDays=INT(vHours/24)
var vRemainingHours=MOD(vHours,24)
return
vDays&" Days & "&
vRemainingHours&" Hours & "&
vRemainingMinutes&" Minutes & "&
vRemainingSeconds& " Seconds"

vzhangti_0-1656313154283.png

Does this match the output you expect?

 

Best Regards,

Community Support Team _Charlotte

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

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.