Going Further: Cleaning Up Your Measures Through Calculated Fields

Photo by Dan Cristian Pădureț on Unsplash

If you've started using parameters to create selectable metrics in Tableau, exploring their uses through blogs by Charles Yi, Michael Bellamy, Vivian Ng, or others, you might have noticed some very annoying problems with bad units on your labels when using calculated fields to display those metrics. In a dashboard I made with Superstore, I had a parameter that let me select from Sales, Profit, or Profit Ratio for a dynamic bar chart:

You can refer to Charles or Vivian's blogs for instructions on how to set this up

To support that parameter, I had a calculated field to interpret the parameter and bring up the appropriate metrics (note that Profit Ratio is already an aggregate and doesn't need further aggregation:

The SM refers to "Same Month" because of a comparison task not relevant to the blog

And I got an output on Sales and Profits with no units that looked like this:

No dollar signs (also a label that's a bit hidden, but that's a more complicated problem)

No problem, right? Right click the SM Metric field, change the number formatting to Currency (standard) or Currency (custom). Exceeeept...

Profit ratio is in decimal, and we can't let it become currency

If we do that, the profit ratio - which is a ratio best expressed as a percentage - will be in currency format. That would be raw anarchy, and our labels would look terrible. Or, we can leave the numbers unformatted, and our client will reasonably ask "Where are the units?" So, instead, just for the label and/or tooltip, we write two (or more) calculated fields to account for the two outcomes:

One calculated field accounts for Sales and Profits; the other for Profit Ratio

For time purposes, the first field says "If the parameter is not 'Profit Ratio', then pass through the calculated field for the metric; otherwise (i.e. if it is 'Profit Ratio' pass through nothing". You can right click this calculated field and turn it into Currency. The second field says "If it is 'Profit Ratio, pass through Profit Ratio specifically; otherwise, do nothing." This field I formatted as Percentage with one decimal place. Then, drag them onto the Marks card on the Label, Tooltip, or Detail tools:

The first one may or may not be strictly necessary 

In the label card, drop in the Metric Label Display and the Metric Profit Ratio Display next to each other. Even though both are active, only one will show at any given time, since you set up the logic in the calculated field making them mutually exclusive. And because of that, you can change the number format for both of the calculated fields to reflect Currency and Percentage, as needed, and give you two beautifully rendered labels depending on which view you've selected:

Profit is appropriately labeled in Currency ($), and Profit Ratio labled as Percentage.

This takes time, and it's one of the more common things that comes up in client project reviews as a next step that couldn't be accomplished in the time allotted. There may be more efficient ways, but this method requires very little technical knowledge if you already know something of how to use parameters and calculated fields.

Author:
Aaron Potts
Powered by The Information Lab
1st Floor, 25 Watling Street, London, EC4M 9BR
Subscribe
to our Newsletter
Get the lastest news about The Data School and application tips
Subscribe now
© 2024 The Information Lab