Disable Highlight

Wednesday, March 19, 2014

Tableau Tip : How to Calculate Time Difference

Today I was in a situation to calculate time difference between two time fields in Tableau. But Tableau doesn't offers us with any Time Functions . I was able to solve the use case by writing a lengthy Calculated field .

Here is the Logic I Implemented , I Calculated the time difference between both the time stamps in seconds. Later I converted the seconds in to hh:mm:ss format.

The Calculation Logic I used to Calculated time in seconds is

( DATEPART('hour',DATEtime([End time ]))* 3600+ 
DATEPART('minute',DATEtime([End time ]))*60 + 
DATEPART('second',DATEtime([End time ])) ) - ( DATEPART('hour',DATEtime([Start time]))* 3600+ 
DATEPART('minute',DATEtime([Start time]))*60 + 
DATEPART('second',DATEtime([Start time])))


The Calculation Logic I used to Calculated time in seconds

(str(([total time in seconds]- (([total time in seconds]-([total time in seconds]%60))%3600)-([total time in seconds]%60))/3600)+ ":" + str((([total time in seconds]-([total time in seconds]%60))%3600)/60)) + ":" + str([total time in seconds]%60)


The workbook can be downloaded from here



1 comment: