This article will explain how to show the Top N values in Tableau; the Bottom N values; and also both the Top AND Bottom N at the same time. There are a number of different ways to do show the top n, with the most appropriate depending on a number of factors.
The simplest method is to filter for the Top N. However this can be too simple for some scenarios. Sets are another option, as are table calculations.
The same techniques also work to select the Bottom N. Scroll to the bottom of this article for the section on keeping the Bottom N and also keeping both the top and bottom N.
Showing the Top N records in Tableau
Look at some examples with a very simple data set. It only has three fields; two dimensions, Category and Region; and a measure, Metric.
To begin sort the Region by Metric:
Top N by Filter or Set
Starting with a very simple example, use a filter to show the top 5 Regions. First sort the Region descending by the sum of Metric. Therefore we can see the expected top 5 regions.
Using a filter on a Set instead achieves the same result. Create a set on Regions and set to Top 5.
Replace the Region filter with the Set, the outcome will be the same. However a Set has other uses so worth making you aware. For example if you only want to highlight those regions in the top 5, while displaying all, a set is a good way to do that.
Top N using a table calculation
Another technique is to use a table calculation. Either INDEX(), FIRST() or RANK work for this. For this example I’ll use INDEX but the other options work equally as well.
Create a calculated field Index with the formula INDEX().
Drag the Index field to Filters and allow a maximum of 5.
Bring Category into the view
Next add the Category with any of these. You will notice it may not do as you expect with any case. With a filter and a Set it keeps the same 5 regions for all categories. Also it maintains the overall Region sorting, it doesn’t sort the Region within the Category. This is because the filter on [Region] restricts the data to only include those regions that are in the top 5 in total.
Using Index it keeps the same 5 regions but only for 1 category. Effectively it is keeping the first 5 records.
Show a nested top N in Tableau
For this example we will show the top 5 Regions per Category. This is a nested Tableau Top N
To keep the top 5 regions per category requires a different approach.
Table calculations are the solution. Again either Index, First or Rank.
A normal filter Top N won’t work. As ascertained a normal filter will return the Top N regions across the total data set. Here we want the top 5 per category, not across the data set.
Using Index for the nested top N
To use Index is a little more complex but it effective. Begin by placing the Index field on Rows. Make it discrete, turning it blue, and place it between Category and Region. Now it will appear like a count of rows, from 1 to the number of rows displayed.
We want it to show 1 to X descending per Category. Each category will contain numbers 1 to X with 1 being the largest Metric and X the smallest. This is the more complicated part.
Right click on the pill and Edit Table Calculation. Compute Using Specific Dimensions.
- Make sure Category and Region are checked
- Category has to be on top to restart the numbering per Category
- “At the level” Deepest
- “Restarting Every” Category
- Sort Order: Custom – Metric – Sum – Descending
That should give the correct numbers for the regions per category. Drag that field to Filters and, as it’s discrete, select 1,2,3,4 & 5. Alternatively drag to filters then convert to Continuous (green). Then set it to have a maximum of 5. This conversion to continuous helps when choosing a larger number of Top N.
The important thing is the Index calculation in the Filters has the exact same Compute Using as in Rows. Note, the field doesn’t need to be on Rows but it helps when developing.
Now you’ll correctly have the top 5 regions per category.
If developing this for a live dashboard I would remove/hide the header of the Index field.
How to show the bottom N records
Showing the bottom N records is very similar to the techniques described above. Depending on the specifics of the task, use either a filter, set or table calculation.
If using a Filter or Set, instead of selecting Top, choose Bottom.
An Index would work for the bottom n, however, there’s a simpler table calculation. Use LAST instead of INDEX. The LAST function numbers the records from the bottom to the top. The last record will have a LAST value of 0, the 2nd last will have the value 1, the next 2, etc.
To show the bottom N first create the calculation:
Take this field to the filter shelf and, to keep the bottom 5, select all records with a max of 4.
How to show the top AND bottom N
To keep only the top 5 AND bottom 5 use table calculations. Earlier in the post I mentioned FIRST as a table calculation option to keep the top N. FIRST works the same way as LAST, just in reverse. Using FIRST, the first record in the data has the value 0, the 2nd record has the value -1, the 3rd has the value -2, etc. FIRST counts down from zero.
Combining FIRST and LAST in a calculated field means we can keep the top and bottom n records.
The only difference this time, we’ll hard code the top 5 in a calculated field. A parameter could replace the 5. In fact using parameters enables the user to select the top N and the bottom Y records.
For this simple example the top and bottom 5 is sufficient.
Create a calculated field:
[FilterTopAndBottom5]: LAST() < 5 OR FIRST() > -5
Put this field to the filter shelf and set it to True.
To finish I have published a workbook to Tableau Public with all of the above examples. You are able to download and better understand how it works.