Thursday, 9 June 2016

hh:mm:ss to seconds in Tableau

The DATEPART() function is used in the # of Seconds calc to extract the hours, minutes, and seconds to turn everything into seconds with the following formula:
 
DATEPART('hour',[Time DateTime]) * 3600 + DATEPART('minute',[Time DateTime])*60 + DATEPART('second',[Time DateTime])
 
Now there's a measure that can be used. To go from # of Seconds back to HH:MM:SS, here's a hh:mm:ss string calc with the following formula:
 
STR(INT(SUM( [# of Seconds] )/3600)) + ":" +
// convert minutes back to a string
IF LEN(STR(INT((SUM([# of Seconds]) % 3600 ) / 60))) = 1 THEN
    "0" + STR(INT((SUM([# of Seconds]) % 3600 ) / 60))
ELSE
    STR(INT((SUM([# of Seconds]) % 3600 ) / 60))
END
+ ":" +
//convert seconds back to a string
IF LEN(STR(INT(SUM([# of Seconds]) % 60))) = 1 THEN
    "0" + STR(INT(SUM([# of Seconds]) % 60))
ELSE
    STR(INT(SUM([# of Seconds]) % 60))
END




https://community.tableau.com/message/224840