KNOWLEDGE BASE

STR() Function Ignores the Default Date and Number Formatting


Published: 12 Jun 2013
Last Modified Date: 15 Aug 2023

Issue

When you wrap date or number fields in the STR() function in calculated fields, the default number and date formatting might not be respected.

For example, if you set the default format for a date field (Default Properties > Date Format) to display only hours and minutes and then use the same field in a calculation and wrapped in the STR() function, the resulting date will display hours, minutes, and seconds.

Environment

Tableau Desktop

Resolution

Use string functions to control date and number formatting in calculated fields. For example, the formula below will display [Profit] with a "$" sign and a comma in the thousands place. Only negative numbers will have a "-" sign.
IF ABS(SUM([Profit])) > 999
THEN 
     IF SUM([Profit]) < 0 THEN "-" ELSE "" END
     + "$"
     + LEFT(STR(FLOOR(ABS(SUM([Profit])))),LEN(STR(FLOOR(ABS(SUM([Profit])))))-3) 
     + "," 
     + RIGHT(STR(FLOOR(ABS(SUM([Profit])))), 3)
 ELSE 
     IF SUM([Profit]) < 0 THEN "-" ELSE "" END
     + "$"
     + LEFT(STR(ABS(SUM([Profit]))), 3)
 END

For more information about strings, see String Functions in Tableau Help.

Did this article resolve the issue?