BASE DE CONNAISSANCES

STR() Function Ignores the Default Date and Number Formatting


Date de publication : 12 Jun 2013
Date de dernière modification : 18 Oct 2022

Problème

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.

Environnement

Tableau Desktop

Résolution

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.

Cet article vous a-t-il permis de résoudre le problème ?