-
1. Re: String with number formatted as currency
Shawn Wallwork Sep 20, 2013 3:45 PM (in response to Rossella Blatt Vital)1 of 1 people found this helpfulRossella, as long as you are NOT using an extract then the RAWSQL_STR function is probably available to you. The calculation would probably be something like this:
"This is my string " + RAWSQL_STR("FORMAT(%1,'Currency')",[My_Number])
Here are some other handy formats:
RAWSQL_STR("FORMAT(%1,'$###,###')",[My_Currency_No_Cents])
RAWSQL_STR("FORMAT(%1,'Percent')",[My_Percent])
RAWSQL_STR("FORMAT(%1,'##.0')",[My_One_Decimal_Always])
Note: '#' shows a number if there is one, whereas '0' always show a number and a zero if there is no number.
I use these alot when I'm using a parameter to drive a case statement which includes various number formats. Here's a partial example:
--Shawn
-
2. Re: String with number formatted as currency
Rossella Blatt Vital Sep 20, 2013 3:49 PM (in response to Shawn Wallwork)Hi Shawn,
thanks for your help.
Unfortunately I am using an extract.
Any idea how to do it in that case?
Thanks
Rossella
-
3. Re: String with number formatted as currency
Shawn Wallwork Sep 20, 2013 4:15 PM (in response to Rossella Blatt Vital)1 of 1 people found this helpfulWell sort of, but it ain't pretty and it will probably be pretty slow. You'll need to parse your number to build it as a string. So if your number was 70000 and you wanted 70,000 then you'd write this:
LEFT(STR([My_Number]), 2) + ',' + RIGHT(STR([My_Number]), 3)
But then you're going to need to add a bunch of logic using LEN() to determine how long the number is and then build the string accordingly. You'll also need to search for any decimals FIND([My_Number], '.') and then subtract this from LEN() to get the proper number of decimal places. It all very tedious and can get complicated very quickly.
--Shawn
-
4. Re: String with number formatted as currency
Rossella Blatt Vital Sep 23, 2013 8:01 AM (in response to Shawn Wallwork)Hi Shawn,
thanks! That's exactly what I was looking for!
Thanks a again a lot.
Rossella
-
5. Re: String with number formatted as currency
Daniel Mark Jun 30, 2014 1:39 PM (in response to Shawn Wallwork)4 of 4 people found this helpfulExcellent tip Shawn! This helped me as well.
I even took your advice a step further (as you suggested) and applied the logic to parse for string values ranging from 1-12 in length:
Character Count:
LEN(STR(ROUND(SUM([METRIC]),0)))
String Formula:
IF [CHARACTER COUNT] = 1 THEN "Of " + "$" + STR(ROUND(SUM([METRIC]),0)) + " Budget"
ELSEIF [CHARACTER COUNT] = 2 THEN "Of " + "$" + STR(ROUND(SUM([METRIC]),0)) + " Budget"
ELSEIF [CHARACTER COUNT] = 3 THEN "Of " + "$" + STR(ROUND(SUM([METRIC]),0)) + " Budget"
ELSEIF [CHARACTER COUNT] = 4 THEN "Of " + "$" + LEFT(STR(ROUND(SUM([METRIC]),0)),1) + "," + RIGHT(STR(ROUND(SUM([METRIC]),0)),3) + " Budget"
ELSEIF [CHARACTER COUNT] = 5 THEN "Of " + "$" + LEFT(STR(ROUND(SUM([METRIC]),0)),2) + "," + RIGHT(STR(ROUND(SUM([METRIC]),0)),3) + " Budget"
ELSEIF [CHARACTER COUNT] = 6 THEN "Of " + "$" + LEFT(STR(ROUND(SUM([METRIC]),0)),3) + "," + RIGHT(STR(ROUND(SUM([METRIC]),0)),3) + " Budget"
ELSEIF [CHARACTER COUNT] = 7 THEN "Of " + "$" + LEFT(STR(ROUND(SUM([METRIC]),0)),1) + "," + MID(STR(ROUND(SUM([METRIC]),0)),2,3) + "," + RIGHT(STR(ROUND(SUM([METRIC]),0)),3) + " Budget"
ELSEIF [CHARACTER COUNT] = 8 THEN "Of " + "$" + LEFT(STR(ROUND(SUM([METRIC]),0)),2) + "," + MID(STR(ROUND(SUM([METRIC]),0)),3,3) + "," + RIGHT(STR(ROUND(SUM([METRIC]),0)),3) + " Budget"
ELSEIF [CHARACTER COUNT] = 9 THEN "Of " + "$" + LEFT(STR(ROUND(SUM([METRIC]),0)),3) + "," + MID(STR(ROUND(SUM([METRIC]),0)),4,3) + "," + RIGHT(STR(ROUND(SUM([METRIC]),0)),3) + " Budget"
ELSEIF [CHARACTER COUNT] = 10 THEN "Of " + "$" + LEFT(STR(ROUND(SUM([METRIC]),0)),1) + "," + MID(STR(ROUND(SUM([METRIC]),0)),2,3) + "," + MID(STR(ROUND(SUM([METRIC]),0)),5,3) + "," + RIGHT(STR(ROUND(SUM([METRIC]),0)),3) + " Budget"
ELSEIF [CHARACTER COUNT] = 11 THEN "Of " + "$" + LEFT(STR(ROUND(SUM([METRIC]),0)),2) + "," + MID(STR(ROUND(SUM([METRIC]),0)),3,3) + "," + MID(STR(ROUND(SUM([METRIC]),0)),6,3) + "," + RIGHT(STR(ROUND(SUM([METRIC]),0)),3) + " Budget"
ELSEIF [CHARACTER COUNT] = 12 THEN "Of " + "$" + LEFT(STR(ROUND(SUM([METRIC]),0)),3) + "," + MID(STR(ROUND(SUM([METRIC]),0)),4,3) + "," + MID(STR(ROUND(SUM([METRIC]),0)),7,3) + "," + RIGHT(STR(ROUND(SUM([METRIC]),0)),3) + " Budget"
END
-Dan
-
6. Re: String with number formatted as currency
Kim McAvoy Nov 2, 2017 6:51 PM (in response to Daniel Mark)I cannot believe it has to be this hard to format a number into a string!!! Surely there is something Tableau can do to help here!?
-
7. Re: String with number formatted as currency
Shawn Wallwork Nov 3, 2017 5:59 AM (in response to Kim McAvoy)Kim there is an easier way to do this. Post a new question with a sample packaged workbook and I'll take a look. Please include the version of Tableau you are working with, and ping me using Shawn Wallwork. Thanks,
--Shawn
-
8. Re: String with number formatted as currency
Paula Wigger Mar 14, 2018 3:18 PM (in response to Shawn Wallwork)Hello Shawn,
You mention an easier way to do this and I am running into the same problem. I have slick way to get the decimal place issue (5 into 5.00 or 4.5 into 4.50).
'$' +
left([NumberString]+
if find([NumberString],'.') = 0
then '.00'
else '00'
end
, find([NumberString]+'.','.')+2
)
I am just trying to find a better way to add the commas. Do you have any ideas?
-
9. Re: String with number formatted as currency
Mark Palmberg Apr 5, 2018 8:28 AM (in response to Shawn Wallwork)Did Kim McAvoy ever post that workbook, Shawn Wallwork? I'm plugging away at a version of Daniel's effort above but keep muttering to myself about the complexity of it all. I know, I know, it's a drawing application! I've included a small pkgd workbook here but am glad to submit a new issue if you prefer. Thank you.
-
format_as_string.twbx 65.1 KB
-
-
10. Re: String with number formatted as currency
Kaitlin DiPaola Dec 5, 2018 11:35 AM (in response to Rossella Blatt Vital)I'm having a similar issue, I think, in that I want to show two different formats for one measure. I have attached a sample workbook so you can see what I'm working with. I would like all impact types except employment to be in dollars, and employment to be in units. I was able to create another dimension to get employment to show in units, but it also still shows dollars. Any help would be greatly appreciated! Thanks!
-
two formats same measure.twbx 32.0 KB
-
-
11. Re: String with number formatted as currency
Nancy Abramson Feb 27, 2019 11:39 AM (in response to Rossella Blatt Vital)I can't believe Tableau doesn't have this baked in but... here is another solution for dollars, no cents under a billion.
First create a no decimal string as a placeholders to simplify programming:
[Total Billed as Str No Decimals]
'$' +
STR(ROUND(SUM([Total Billed]),0))
Then use the string to know where to place the commas:
IF ISNULL([Total Billed as Str No Decimals]) THEN
'N/A'
ELSEIF LEN([Total Billed as Str No Decimals]) <= 4 THEN
[Total Billed as Str No Decimals]
ELSEIF LEN([Total Billed as Str No Decimals]) <= 7 THEN
LEFT([Total Billed as Str No Decimals], LEN([Total Billed as Str No Decimals]) - 3) +
',' + RIGHT([Total Billed as Str No Decimals], 3)
ELSE
LEFT([Total Billed as Str No Decimals], LEN([Total Billed as Str No Decimals]) - 6) +
',' +
LEFT(RIGHT([Total Billed as Str No Decimals], 6), 3) +
',' + RIGHT([Total Billed as Str No Decimals], 3)
END
