Dynamic Label Formatting in Tableau
Sometimes there is a need to display different data formats within the same visual. However, if there are multiple formats, such as currency and percentage in the same visual, how does Tableau know what format to display for each underlying data object? For example, a college may want a dashboard user to have the ability to toggle between displaying Student Headcount (integer), Tuition Revenue (currency), and Course Success Rate (percentage) in a bar chart. You can leverage the following parameter to drive this interactivity:
This Metric parameter can then be referenced in the following Selected Metric calculated measure:
Adding the Selected Metric measure along with course subject to a column chart yields the following visual:
At this point the user can select between the three measures, and the chart will update accordingly. However, if data labels are required to be displayed on the chart, dragging the Selected Metric to the Label card displays only one data format for all three measures. While Headcount displays correctly as integer format, the Course Success Rate does not display in percentage format:
To resolve this a separate calculation can be created exclusively to serve the Label card of the visualization. This calculation will do the following:
Convert the number to text
Add any prefixes and suffixes ($, %, etc.) that might be needed for each individual metric
Perform the appropriate formatting for each metric
Once the Dynamic Data Label object is placed on the Label marks card, the selected metric will now display with the appropriate formatting:
There you have it! The best part is this solution can work for any Tableau dashboard solution as it uses native, out of the box functionality available in Tableau Desktop. Please comment and let us know your thoughts!