Question: I’d like to show the top items by a measure but allow my user to select the measure (e.g. swap between top 5 on Sales and top 5 on Profit). AND I want them to be able to change the number of top items (e.g. top 5 or top 10). How can I do this?
The Challenge
We’ve got some categories and are showing multiple measures for them. For example, below we’ve got Sales and Profit by Sub-Category and limited to our top 10 items on Sales:
But now, our user wants to look at this for the top 10 on Profit and then top 5 on Profit. How can we bake in this flexibility?
The Solution
We’re going to build a few parameters to drive our sort and give us our top N. Parameters are great tools – your user can pick what they are interested in, and if you need to add something else to the list in the future, you can!
Parameter 1
Our first parameter is for our sort options. For now, we’re making parameters with our core items: Sales and Profit:
We need a calculation that turns this into something we can reference. We’ll make a simple one that just calls up sum(sales) if Sales is selected, and sum(profit) if Profit is selected:
Finally, we’ll tell Tableau to sort by that calc, so right-click our dimension Sub-Category in Rows, and set to Sort by Descending:
Parameter 2
Now, let’s make another parameter for Top n:
And set my filter to be based on Top n on Sort by, so my user can set the number they’re after:
PRESTO! Now, I can select my measure to Sort by and my Top n in my range. Hoorah!
To explore the data further, you can also download the Tableau workbook below.