{"id":917,"date":"2017-08-03T06:08:44","date_gmt":"2017-08-03T13:08:44","guid":{"rendered":"http:\/\/www.sqldbpros.com\/?p=917"},"modified":"2017-08-03T11:35:41","modified_gmt":"2017-08-03T18:35:41","slug":"tableau-format-date-as-string-mmddyyyy-hhmm","status":"publish","type":"post","link":"http:\/\/sqldbpros.com\/wordpress\/2017\/08\/tableau-format-date-as-string-mmddyyyy-hhmm\/","title":{"rendered":"Tableau: Format Date As String mm\/dd\/yyyy hh:mm"},"content":{"rendered":"<p>gah.<\/p>\n<p>Why is some of this stuff so challenging in Tableau? \u00a0You have a date and you want to format it as a string? Why? So you can concatenate it with another string perhaps? Maybe you're creating a calculated field to use in a label (that's what I was doing).<\/p>\n<p>Unfortunately, when you convert a datetime to a string in Tableau it uses the format yyyy-mm-dd hh:mm:ss. Fine for a lot of cases, not fine for a lot of cases.<\/p>\n<p>Being the purveyor of fine calculated measures I created this wonderful calc to convert a datetime to a string in the format: mm\/dd\/yyyy hh:mm AM\/PM. Are you looking for the same thing with time removed (mm\/dd\/yyyy)? No problem! That's here too!<\/p>\n<p>If you want your string date in the format mm\/dd\/yyyy hh:mm AM\/PM create a calculated field as follows:<\/p>\n<pre>\/\/Format date as string mm\/dd\/yyy\r\nstr(datepart('month',[Power Reconnect Date Time]))+'\/'\r\n+str(datepart('day',[Power Reconnect Date Time]))+'\/'\r\n+str(datepart('year',[Power Reconnect Date Time]))\r\n\/\/add hh:mm AM\/PM \r\n \/\/build hour (special logic to show 12 hour time instead of 24 hour)\r\n +' '\r\n + if datepart('hour',[Power Reconnect Date Time]) &gt; 12 \r\n then str(datepart('hour',[Power Reconnect Date Time])-12) \/\/After 12pm\r\n ELSEIF datepart('hour',[Power Reconnect Date Time]) = 0 \/\/12am (midnight)\r\n then '12'\r\n ELSE str(datepart('hour',[Power Reconnect Date Time])) \/\/ 1am to 12pm\r\n END\r\n \/\/build minute\r\n +':'\r\n + if datepart('minute',[Power Reconnect Date Time])&lt;10\r\n then '0'+str(datepart('minute',[Power Reconnect Date Time]))\r\n ELSE str(datepart('minute',[Power Reconnect Date Time]))\r\n END\r\n \/\/add AM\/PM\r\n +' '\r\n +if datepart('hour',[Power Reconnect Date Time]) &gt;= 12 \r\n then 'PM'\r\n else 'AM'\r\n END\r\n\r\n<\/pre>\n<p>Do you only want\u00a0mm\/dd\/yyyy? No problema! The same formula with the last section removed will do it:<\/p>\n<pre>\/\/Format date as string mm\/dd\/yyy\r\nstr(datepart('month',[Your Date Time]))+'\/'\r\n+str(datepart('day',[Your Date Time]))+'\/'\r\n+str(datepart('year',[Your Date Time]))<\/pre>\n<p>&nbsp;<\/p>\n<p>Got another format you're looking for? Ask and you shall receive! Post a comment and I'll add it to the article. Together we can make Tableau a better place. \ud83d\ude09<\/p>\n<figure id=\"attachment_919\" aria-describedby=\"caption-attachment-919\" style=\"width: 3264px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-919\" src=\"http:\/\/www.sqldbpros.com\/wordpress\/wp-content\/uploads\/2017\/08\/IMG_2463.jpg\" alt=\"\" width=\"3264\" height=\"2448\" srcset=\"http:\/\/sqldbpros.com\/wordpress\/wp-content\/uploads\/2017\/08\/IMG_2463.jpg 3264w, http:\/\/sqldbpros.com\/wordpress\/wp-content\/uploads\/2017\/08\/IMG_2463-300x225.jpg 300w, http:\/\/sqldbpros.com\/wordpress\/wp-content\/uploads\/2017\/08\/IMG_2463-768x576.jpg 768w, http:\/\/sqldbpros.com\/wordpress\/wp-content\/uploads\/2017\/08\/IMG_2463-1024x768.jpg 1024w\" sizes=\"auto, (max-width: 3264px) 100vw, 3264px\" \/><figcaption id=\"caption-attachment-919\" class=\"wp-caption-text\">Picture of Stella. Because everyone loves dogs. And dogs LOVE dates formatted as strings.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>gah. Why is some of this stuff so challenging in Tableau? \u00a0You have a date and you want to format it as a string? Why? So you can concatenate it with another string perhaps? Maybe you're creating a calculated field to use in a label (that's what I was doing). Unfortunately, when you convert a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[193],"tags":[20,196,25,194],"class_list":["post-917","post","type-post","status-publish","format-standard","hentry","category-tableau","tag-business-intelligence","tag-calculated-field","tag-chart","tag-tableau"],"_links":{"self":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/comments?post=917"}],"version-history":[{"count":6,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/917\/revisions"}],"predecessor-version":[{"id":924,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/917\/revisions\/924"}],"wp:attachment":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/media?parent=917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/categories?post=917"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/tags?post=917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}