4

Tableau Custom Number Formatting – Part 2

 2 years ago
source link: https://datavis.blog/2022/08/28/tableau-custom-number-formatting-part-2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

In part 2 of this blog I’ll cover the following topics:

  • Formatting thousands, millions and billions
  • Adding prefixes and suffixes
  • Formatting currency values
  • Internationalisation (locale settings)
  • Formatting percentages (% vs. “%”)
  • Using Unicode characters
  • When to use quotes in formatting codes

Positive, Negative and Zero

A quick reminder from part 1 of the blog that number codes in Tableau are split into three sections, separated by semicolons:

03-pos-neg-zero.png?w=604

For simplicity, most of the examples below will omit the negative section as this can easily be replicated by copying the positive number section and adding a “-” sign or parentheses as desired, as described in part 1 of the blog.

That said, note that if only the positive numbers section is specified, Tableau will automatically add a negative sign “-” to the display of negative numbers and will use the positive numbers section to format zero values, for example, the below number formatting code only includes the positive numbers section:

00-positive-format-only.png?w=648

Also, see part 1 for explanations of the primary placeholder characters (“0” and “#”).

Thousands Separators

As we start to look at larger numbers we’ll want to show a comma separator after each group of three digits so the numbers are easier to read.

Let’s use Tableau’s Number (custom) dialog to create a template to start with. Here I’ve set the number of Decimal places to zero (0), and checked the Include thousands separators checkbox.

01-show-separator.png?w=543

Here’s the effect in the view on the Formatted numbers. One or more thousand separators are added depending on the size of the number and no decimal places are displayed:

01-custom-1000s.png?w=648

Clicking Custom will show the number code that Tableau generated to create these settings:

01-custom-show-separator.png?w=450

You can see that both the positive and negative sections now include a comma.

We can actually simplify this code to be #,0 and still get the same effect. Remember from part 1 that, left of the decimal place, a single placeholder can be used to represent all digits in the underlying data (at least this was the case throughout my testing).

Here’s the simplified number code and the formatted results, again excluding negative values. The results are the same as the earlier screenshot that used the longer generated code:

02-hash-comma-zero.png?w=648

There needs to be a zero at the end to ensure zero values in the data are shown. If two hashes were used (#,#) the zero would not show.

03-hash-comma-hash.png?w=648

Thousands, Millions, Billions

Let’s now format all the numbers as thousands, first with no decimal places. To do this, add a comma at the end of the code:

04-thousands-0dp.png?w=648

The comma at the end essentially divides the raw data value by a thousand, thereby removing the last three digits. Because we’ve not added decimal places yet, we’re starting to lose information. We’ll add decimals shortly.

To show the numbers as millions, we simply add another comma:

05-millions-0dp.png?w=648

Any number less than a million is now showing as zero. If #,# was used at the start of the code (instead of #,0) the zeros in the table above would not show and those cells would be blank.

To show billions, we again add one more comma:

06-billions-0dp.png?w=648

Two types of comma

The above demonstrates the different uses of a comma in custom number codes.

  • A single comma added between two placeholders (0 or #) will ensure thousands separator(s) are displayed throughout the whole number
  • Adding a comma with no placeholder immediately after it scales the whole number by a thousand for each successive comma added

Adding decimal places to large numbers

For the previous example to make more sense, we need to add decimal places to show fractions of the large number. Here, I’ve switched back to millions (by removing one of the three commas) and am forcing the display of two decimal places by adding .00 at the end:

#,0,,.00
07-millions-2dp.png?w=648

The third row, above, represents 1.23 Million

Adding display units as a suffix

To make the above clearer we can add an “M” display unit by literally adding an M at the end of the code:

#,0,,.00M
08-millions-2dp-m.png?w=648

If you prefer a space before the M, you can add one in.

Here, I’ve switched back to billions, forced the display of four decimal places and added a space before the B display unit (not a format I’d recommend but just as an example):

09-billions-4dp-b.png?w=648

Adding a prefix

If the raw data is currency values, we might want to add a currency symbol to the format code to make that clear. Here I’ve added a $ symbol in quotes at the start (and switched back to millions with two decimal places).

10-currency-millions.png?w=648

We can add other text to the prefix, such as “2Q Sales $”, to specify these are second quarter sales in $.

"2Q Sales $"#,0,,.00 M
11-2q-sales-prefix.png?w=648

Currency and internationalisation (locale settings)

If text is enclosed in quotes “” as above, it will always display as shown in the format code. 

Going back to a simple number code that just adds a thousands separator and a dollar symbol at the start:

12-unit-in-quotes.png?w=648

If we remove the quotes “” and keep the $ symbol, this will now update the display based on the Workbook Locale settings. For example, my locale is set to “English (United Kingdom)”:

12b-locale-english-uk.png?w=622

So the $ currency symbol displays as a pound symbol £:

13-unit-no-quotes.png?w=648

Changing my locale to “Spanish (Spain)” will display a euro € currency symbol with the exact same number formatting code: 

14-euro-symnol.png?w=648

Note also that the thousands separator (in the formatted column) has changed from a comma to a period and the decimal point separator (in the raw data column) has changed from a period to a comma. That is because this is the standard number format in most European (and many other) countries – see this page for a more complete list.

This is obviously very important if you are working with currency data and need to display the correct currency symbol regardless of where in the world people may be opening your workbook!

To be sure the correct currency symbol displays for the currency data you are working with, always enclose it in quotes, like so: “$”

Formatting percentages (%)

Again, let’s start by having Tableau generate a number formatting code for us by selecting Percentage and specifying one decimal place:

15-percentage-1dp-tableau.png?w=450

Selecting Custom, we can then see the code Tableau generated and the effect this has on our new raw data in the view:

16-pc-1dp.png?w=648

If you have a mix of percentage values that are less than and more than 1%, you can’t turn off the decimal place on those values over 1% only. Removing the decimal place code, will remove decimal places from values less than 1% and they will then round up to 1% (or down to 0%, if the raw values are less than 0.005):

17-0dp.png?w=648

The % symbol has two purposes:

  • Convert the raw data to a percentage (i.e. shift the decimal place two positions to the right)
  • Add a % symbol to the display

If we remove the % symbol and format the display to two decimal places we see this:

18-no-pc-sign-2dp.png?w=648

Adding the % symbol performs the two previously mentioned actions:

19-pc-2dp.png?w=648

What if our data is storing percentage values as whole percentages and not as decimal values representing percentages? 

In that case we need to wrap the percentage symbol in quotes: “%” so it becomes a text suffix only and has no effect on the decimal point position.

This example has raw data containing percentages not in decimal format (i.e. 0.5 is 0.5% and not 50%):

20-pc-suffix.png?w=648

If the quotes are removed from the above example, erroneous results will be displayed:

21-pc-bad-format.png?w=648

Unicode Symbols

For visual encoding, we can add unicode characters to each part of the number format code. The most common use case is adding up and down arrows to highlight a positive or negative change/value, for example, in a year-over-year (YoY) comparison:

Showing a percentage change in values using ▲▼ unicode characters:

22-unicode-up-down.png?w=648

Note, the zero section of the code needs to be added (without a unicode character), otherwise the positive section will also be used to format zero values resulting in an upward arrow for zeros:

23-zeros-as-positive-1.png?w=648

Another example, using currencies formatted with a $ symbol and zero decimal places. Zero values are displayed as an em dash “—” only:

"▲ $"#,#;"▼ $"#,#;"—"
24-unicode-dollar-dash.png?w=648

Other unicode characters are below, which you can copy and paste as needed:

See this site for many more Unicode characters.

The use of quotes in custom number codes

If you add characters to either the prefix or suffix boxes in Tableau’s Number (Custom) dialog and then switch to the Custom view, you will see that Tableau’s generated code wraps both of them in quotes. For example, here I’ve added “Sales” as a prefix and “ in Q2” as a suffix:

25-quotes-1.png?w=648

On the Custom view, Tableau has generated the number code below, which includes quotes. I’ve bolded the prefix and suffix in the positive section of the code: 

"Sales "#,##0.00" in Q2";-"Sales "#,##0.00" in Q2"
26.png?w=450

If the quotes are removed from the suffix, “ in Q2”, it is unaffected and displays as before (I’ve removed the negative and zero sections for clarity):

27-suffix-no-quotes.png?w=648

However, if we also remove the quotes around the prefix “Sales “, then the formatting breaks:

28-preffix-no-quotes.png?w=648

What’s happening? 

Custom codes can also be used to determine the formatting of dates and times. Various codes (mostly letters) are used to determine which date/time parts to show and in what format. These include letters to represent: days (d), weeks (w), hours (h), seconds (s) and so on. See here for a detailed list. 

Because “Sales” contains one of these letters, “S”, Tableau interprets the raw data to be of that date/time part and displays it as such. The whole number code is then interpreted to be formatting dates, so the “n” and “Q” that are contained within the suffix “in Q2” are then also interpreted as date formatting (as minutes and quarters respectively in this example). 

So everything should be in quotes? 

Not necessarily. Various characters can be added without quotes. Indeed, Tableau does this when generating codes that contain Display Units (K, M, B) or negative values. The below Number (Custom) settings will generate a code that shows negative values surrounded by parentheses and uses the letter “M” to signal to the reader that values are displayed in Millions. (Remember, it’s not the letter M that converts the raw data values to millions, that is done with the use of two commas as seen earlier).

29-tableau-m-neg-code.png?w=648

Switching to the Custom dialog, we can see that the code Tableau generated does not have quotes around the parentheses or the display unit M:

#,##0,,.00M;(#,##0,,.00M)
30-custom-m-neg-code.png?w=450

Quotes could be added around each of these characters but it will have no effect on the formatted output

#,##0,,.00"M";"("#,##0,,.00"M)"
31-custom-m-neg-code-quotes.png?w=648

To summarise, below are recommendations for using quotes based on my testing: 

  • Always use quotes with text prefixes
  • Use quotes on longer suffixes for consistency
  • Quotes are optional for display unit suffixes (e.g. K, M or B)
  • Quotes are optional for the minus sign and parentheses used to format negative numbers
  • For currency data, unless you require a dynamic currency symbol based on the consumer’s locale settings, use quotes around the currency symbol.
  • For percentage data stored as decimals (e.g. 0.1 = 10%), don’t use quotes around the percentage symbol: %
  • For percentage data stored as percentage values (e.g. 10 = 10%), use quotes around the percentage symbol: “%”
  • Quotes are optional for Unicode arrows (that I tested), such as: ▲▼

Combination of codes

This final example combines many of the codes we’ve looked at so far. Just to be clear, this isn’t a format I’d recommend for this data but it’s useful for demonstrating the various options:

"Profit $"#,0,,.### M;🠋 ("$"#,0,,.000) M;"⚑ NO SALES ⚑"
xx-custom-number-format.png?w=426

Let’s break down each part:

33-explanation-of-number-formatting-codes.png?w=831

Thanks for making it to the end of this fairly long and detailed post. I hope it was useful.

The guidance in this post is based on my own test results, so if you find anything that doesn’t work as expected or you have any other general feedback, please feel free to message me on Twitter.

Thanks!
Marc


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK