[Tableau] 20.Date Format to String

如何將日期轉成字串

如何將日期轉成字串,同T-SQL or 程式,可使用depart 添加運算式。或者Extract Date(此方法多個維度可能會有顯示問題)

//Format date as string mm/dd/yyy
str(datepart('month',[Power Reconnect Date Time]))+'/'
+str(datepart('day',[Power Reconnect Date Time]))+'/'
+str(datepart('year',[Power Reconnect Date Time]))
//add hh:mm AM/PM 
 //build hour (special logic to show 12 hour time instead of 24 hour)
 +' '
 + if datepart('hour',[Power Reconnect Date Time]) > 12 
 then str(datepart('hour',[Power Reconnect Date Time])-12) //After 12pm
 ELSEIF datepart('hour',[Power Reconnect Date Time]) = 0 //12am (midnight)
 then '12'
 ELSE str(datepart('hour',[Power Reconnect Date Time])) // 1am to 12pm
 END
 //build minute
 +':'
 + if datepart('minute',[Power Reconnect Date Time])<10
 then '0'+str(datepart('minute',[Power Reconnect Date Time]))
 ELSE str(datepart('minute',[Power Reconnect Date Time]))
 END
 //add AM/PM
 +' '
 +if datepart('hour',[Power Reconnect Date Time]) >= 12 
 then 'PM'
 else 'AM'
 END

 

同時也歡迎追蹤Tableau Public Gallery- MR.360 |聚沙成塔,裡面包含文章中的案例實作,
期待能帶給您新的啟發或靈感。

未來文章將喬遷新址「一趟數據分析之旅」,歡迎追蹤繼續支持,您將不會錯過任何新知識。