I am Joshua Poehls. Say hello Archives (not so) silent thoughts

Parse .NET TimeSpans in a Tableau Calculated Field

Here’s a formula you can use to create a calculated field in Tableau for converting strings in the format of .NET TimeSpan values to a total number of seconds.

Given a string 79.23:08:01.3976607 (79 days, 23 hours, 8 minutes, 1.3976607 seconds), it would return the total number of seconds.

IF ISNULL([TimeSpan]) THEN
   NULL
ELSE
   (IFNULL(INT(REGEXP_EXTRACT_NTH([TimeSpan], "^((\d+)\.)?(\d+):(\d+):(\d+(\.(\d+))?)$", 2)), 0) * 86400)
   + (INT(REGEXP_EXTRACT_NTH([TimeSpan], "^((\d+)\.)?(\d+):(\d+):(\d+(\.(\d+))?)$", 3)) * 3600)
   + (INT(REGEXP_EXTRACT_NTH([TimeSpan], "^((\d+)\.)?(\d+):(\d+):(\d+(\.(\d+))?)$", 4)) * 60)
   + FLOAT(REGEXP_EXTRACT_NTH([TimeSpan], "^((\d+)\.)?(\d+):(\d+):(\d+(\.(\d+))?)$", 5))
END
⦿