How do I display only the Time from DateTime field in Tableau

I find some time formatting the fields  can really be challenging. I had a particular trouble recently when I needed to display only the time from DateTime field in the format of “H:MM:SS” from a DateTime field in a “MM/DD/YYYY HH:MM:SS AM/PM” format. So I thought I’d share how I converted the datetime field into a time field that Tableau could work with.

Here is the formula that I used for the calculated field:

IF DATEPART('hour',[YourDateTimeField]) = 0 THEN '12'
ELSEIF DATEPART('hour',[YourDateTimeField])<10 THEN STR(DATEPART('hour',[YourDateTimeField]))
ELSEIF DATEPART('hour',[YourDateTimeField])>12 THEN STR(DATEPART('hour',[YourDateTimeField])-12)
ELSE STR(DATEPART('hour',[YourDateTimeField])) END //hour
+ ':' +
IF DATEPART('minute',[YourDateTimeField])<10 THEN '0'+STR(DATEPART('minute',[YourDateTimeField]))
ELSE STR(DATEPART('minute',[YourDateTimeField])) END //minute
+':'+
IF DATEPART('second',[YourDateTimeField])<10 THEN '0'+STR(DATEPART('second',[YourDateTimeField]))
ELSE STR(DATEPART('second',[YourDateTimeField])) END //second
+' '+
IF DATEPART('hour',[YourDateTimeField])>=12 THEN 'PM' ELSE 'AM' END //AM or PM.

And here is how the calculated field looks like in the Tableau Desktop:

Calculated Time Field

And here is how it looks like on the chart:

Only Time from DateTime

Also if you are to use Tableau Public, the dataparse() function can also be used for this purpose.

Please contact us  if you need help resolving Reporting issues.

Scroll to top