Tableau is a powerful data visualization tool that allows users to explore and analyze data from various perspectives. One key aspect of data analysis is choosing the right metric to measure and track. For a more exploratory dashboard, what if you want to provide users with the flexibility to change metrics dynamically within a Tableau dashboard? In this blog post, I'll walk you through the process of implementing metric changes using buttons in Tableau.
First off I wanted to point you towards an amazing viz created by Kevin Flerage. The buttons I will be using in this demo were altered from ones he created on the below dashboard. https://public.tableau.com/app/profile/kevin.flerlage/viz/ButtonswithPowerPointTemplate/Buttons
In my example, I'm going to have 6 different buttons as I want the buttons to be grey when they aren't selected. Once you've created these buttons, in your file explorer, go to My Tableau Repository -> Shapes and create a new file. Drop in the buttons you have created.
I'm going to be using the Sample - Superstore data so my metrics will be Sales, Profit and Quantity. First step is to create a parameter by clicking the drop down arrow in the data pane. I'm going to call this Sales/Profit/Quantity Parameter. Select string as the data type, click list and add in the values Sales, Profit and Quantity.
After you've clicked ok, click the dropdown arrow and select show parameter.
We then want to create 3 calculated fields that return Boolean values. For my Profit metric, I wrote this calculation.
SPF Buttons - Profit:
IF [Sales/Profit/Quantity Parameter] = "Profit"
Then "Profit.Selected"
else "Profit.Grey"
END
Note: if you only have 3 buttons (i.e. you don't want a separate greyed out button for each metric) then skip the line else "Profit.Grey". Repeat this process for Quantity and Sales.
Now go into the columns shelf and type in max(0).
Repeat this process two times (or ctrl drag it twice). You'll notice you now have three marks cards. Take your SPF Buttons - Profit field into the detail of your first AGG(max(0)) marks card, change the chart type to shape and drag SPF Buttons - Profit onto the shape.
Ensuring that in the dropdown on the right hand side, Sales/Profit/Quantity Parameter is currently set to Profit, click on the Shape in the marks card, reload shapes and find your button for when Profit is selected. If you're using another button when Profit isn't selected, change the Sales/Profit/Quantity Parameter to Sales or Quantity and change the shape again.
Repeat this process for SPF Buttons - Sales and SPF Buttons - Quantity. Go ahead and remove any grid or border lines. Here's how mine ended up.
Now we need to create a calculated field that changes the metric depending on the selection in the parameter using the below code.
Sales/Profit/Quantity Metric:
CASE [Sales/Profit/Quantity Parameter]
WHEN "Sales" THEN [Sales]
WHEN "Profit" THEN [Profit]
WHEN "Quantity" THEN [Quantity]
END
Now you can use the buttons on any chart as long as you drag in your Sales/Profit/Quantity Metric but for the purposes of this demo, I'll use a bar chart.
So drag the Sales/Profit/Quantity Metric to the columns shelf and segment to the rows. Click the button to sort the bars.
Now create a new dashboard and drag your buttons and the bar chart into the view.
At the top of the page, select Dashboard -> Actions -> Add Action -> Change Parameter. Label this Change Metric Parameter - Profit, select Sales/Profit/Quantity Parameter as your metric and SPF Buttons - Profit.
Repeat this process for Quantity and Sales and voila! A nice finishing touch here would be to right click on the Sales/Profit/Quantity axis and in the title area, click the dropdown on custom, hover on parameter and select Sales/Profit/Quantity Parameter. Now the axis title should change based on which parameter is selected.