1

Dynamic Field Formatting in Tableau

 1 year ago
source link: https://datavis.blog/2023/03/30/dynamic-field-formatting-in-tableau/
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.

The Problem

A common requirement when building dashboards in Tableau is the ability to swap measures in a chart. This can be easily implemented using a calculation similar to the one below that returns a measure based on a parameter selection:

w5YT34rgAJl2CAqtB-5hyOlbQuoI0-7QUtNIuOEkBph1mj9S-tAxBPku_v6RXTyXiobxtT5C9RdgT6zlcISUun3gNatGl4y55-z5z4zO93axxE6Isv_dfM5j5lLc9Cv-sw0bvAyFJevVwh3m0JdMAMI

CASE [p.Select Measure]
WHEN “Profit” THEN SUM([Profit])
WHEN “Discount” THEN AVG([Discount])
WHEN “Days to Ship” THEN AVG([Days to Ship])
END

The problem is that each of these measures will likely require different formatting, for example: 

  • Profit – displayed in thousands with a $ prefix and two decimal places
  • Discount – displayed as a percentage with a % suffix and one decimal place
  • Days to ship – shown as an integer with no suffix or prefix

While formatting can be applied to this dynamic measure, it cannot be dynamically updated based on the parameter selection. So Discount could end up formatted as a $ amount or your Profit formatted as a very large percentage:

7-s7spr5qaTQusg1BIR8uDhpu_8rLucok8iFE-b19wcI-fKTA9koRO9A9gFXhE_TBcQ-NGrPAfG7xX94OFzffIPsXQKrhaqL98nr9rYN8linl9rYJJb-p0PNOfVJLuIg7LWyKh0EDJZZ9kiXg3CTY9Q

Workarounds

As always, the creative Tableau community provides various workarounds.  A few such workarounds are linked below:

Below are three possible solutions that would remove the need for these workarounds:

Solution 1 – Inherit formatting of the referenced measure

Default formatting can be applied to any measure in the data model. When one (dynamic) measure is returning another measure, the default formatting of the returned measure could be applied. 

This is not as straightforward as it seems, however, as what’s returned in these dynamic measure scenarios is not always a single measure. It could be the result of multiplying different measures together, for example, a measure to forecast profits increasing by a user defined percentage, such as: 

WHEN “Profit + N%” THEN [Profit] * [% Increase]

Should the dynamic measure inherit the formatting from the [Profit] field or from the [% Increase] field? One resolution here is to move that Profit * % increase calculation out to a separate calculated field and apply default formatting to that calculated field which could then be inherited within the dynamic measure field. 

Solution 2 – Add a format field option in the formatting panel

Tableau recently added the ability to create dynamic axis titles by using the value of a parameter or calculated field:

n069jGD4eOOq-TiUH9VoQipZWUvvcdwNdJ_A-lGaoyeXayEjR5NNH-SyBEzFmC7PE_WA98v8OBC8LsMo-0G9Wrk59neymlUrjon6oK6i3RGAu2P_rt9WKHWrgORf1_zlU9nbO5N4BuGrMC8H4Qk7ChI

A similar approach could be used for field formatting. Currently, custom formatting can be applied to fields using formatting strings (see here and here for a deep dive into how this works). In addition to typing these codes in, they could also be sourced from a calculated field or parameter with a UI similar to that used for dynamic axis titles:

ybjIsjruG8ajujL9UOuhjM76T0hyMdcIxZ2oOcuuGRxndEE4ud5jbgWobFcdSjk_0kg30RfOgyKy-rOhwzVrFnBddNUGn5LHogD9dU-nmaUnotFbbN9my6DwzND848f9Gjmse8DT0Q8i0kI_APSTq_Y

A calculated field, e.g. [Dynamic Formatting] could then be used to format our dynamic measure seen earlier. This formatting calculation might look like this: 

CASE [p.Select Measure]
WHEN “Profit” THEN “$ #,0,.0K”
WHEN “Discount” THEN “0.0%” 
ELSE “0”
END

This also opens up the option to dynamically change the formatting by updating the Dynamic Formatting calculation with a parameter if needed.

Solution 3 – Add a FORMAT() function

Perhaps the easiest solution is simply to have a FORMAT() function that would allow formatting to be applied to a field inside a calculation. The syntax for such a function could be:

FORMAT( <measure>, <format string> )

Our dynamic measure calculation might then look like this:

CASE [p.Selected Measure] 
WHEN “Profit” THEN FORMAT( [Profit], “$ #,0,.0K”)
WHEN “Discount” THEN FORMAT( [Discount], “0.0%”
WHEN “Days to Ship” THEN FORMAT( [Days to Ship], “0”)
END

Dynamically Format Colour

As a bonus, it would be nice to also format the colour of the resulting measure based on an additional argument to the FORMAT() calculation: 

FORMAT( <measure>, <format string>, <colour> )

For example, to show positive profits in blue text and negative profits in red text, we could update the first line of the CASE statement to be: 

WHEN “Profit” THEN FORMAT( [Profit], “$ 0,,”, IIF([Profit]<0,”Red”,”Blue”))    

Hex codes or RGB could also be used.

This does open up questions of precedence vis-à-vis encoding that might be applied to the colour shelf but this could be resolved, for example, with a fourth <override> argument where 1 = override colour shelf encoding and 0 = colour shelf encoding takes precedence. This would also allow for this setting to be dynamic based on a parameter.

No doubt there are other dependencies but it seems worth considering.

Current Ideas Forum Links

Custom formatting is a much requested feature and you can add your vote to one or more of the below ideas on Tableau’s Ideas Forum if you would like to see one of these implemented.

Idea 1 – Option 1 – Inherit field formatting
Idea 2 – Option 1 – Inherit field formatting
Idea 3 – Option 3 – Add a Format Function
Idea 4 – Option 3 – Add a Format Function

I couldn’t find an existing idea for option 2, but #1 and #3 seem like better options to me anyway, so I won’t add another idea to the already long list of live ideas on the forums.

Thanks for reading,
Marc

Latest YouTube video
This video tutorial steps through how to create a dot plot in Tableau.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK