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
crispybc
Frequent Visitor

Preview Feature Dynamic Formatting (time duration format)

I have seen the new preview feature for dynamic formatting and though I'd give it a go formating a duration measure (hours) into the format years,days,hours using the following formatting dax:

 

var col = [last status change duration]
var totaldays =  QUOTIENT(col,24)
var years = if (totaldays > 365, CONCATENATE(CONVERT(ROUNDDOWN(DIVIDE(totaldays,365),0),STRING),"y"))
var days = IF(MOD(totaldays, 365) >=1 , CONCATENATE(CONVERT(ROUNDDOWN(MOD(totaldays, 365),0),STRING),"d"))
var hours = if (MOD(col, 24) >=1, CONCATENATE(convert(TRUNC(MOD(col, 24)),STRING),"h"))
var string =  COMBINEVALUES(" ",years , days, hours)
return string

 

 

However, this is sometime returning incorrect values.

 

crispybc_0-1681827278196.png

Not sure if i am doing something wrong, or it is just a bug with the new feature. Has anyone else managed to format a time duration measure into an easily readable format?

1 ACCEPTED SOLUTION
crispybc
Frequent Visitor

I figured out the issue. If your output string contains a 0, then it is substituted out for the measure value. 

You can work around this with the SUBSTITUTE function.

 

So new solution:

var col = [last status change duration]
var totaldays =  ROUNDDOWN(DIVIDE(col,24),0)
var years = if (totaldays > 365, CONCATENATE(CONVERT(ROUNDDOWN(DIVIDE(totaldays,365),0),STRING),"y"))
var days = IF(MOD(totaldays, 365) >=1 , CONCATENATE(CONVERT(ROUNDDOWN(MOD(totaldays, 365),0),STRING),"d"))
var hours = if (MOD(col, 24) >=1, CONCATENATE(convert(TRUNC(MOD(col, 24)),STRING),"h"))
var string =  COMBINEVALUES(" ",years , days, hours)
return SUBSTITUTE(string, "0", "\0")

 

View solution in original post

1 REPLY 1
crispybc
Frequent Visitor

I figured out the issue. If your output string contains a 0, then it is substituted out for the measure value. 

You can work around this with the SUBSTITUTE function.

 

So new solution:

var col = [last status change duration]
var totaldays =  ROUNDDOWN(DIVIDE(col,24),0)
var years = if (totaldays > 365, CONCATENATE(CONVERT(ROUNDDOWN(DIVIDE(totaldays,365),0),STRING),"y"))
var days = IF(MOD(totaldays, 365) >=1 , CONCATENATE(CONVERT(ROUNDDOWN(MOD(totaldays, 365),0),STRING),"d"))
var hours = if (MOD(col, 24) >=1, CONCATENATE(convert(TRUNC(MOD(col, 24)),STRING),"h"))
var string =  COMBINEVALUES(" ",years , days, hours)
return SUBSTITUTE(string, "0", "\0")

 

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.