Group by top N records using Tableau Desktop

Published: 06 Jan 2023
Last Modified Date: 06 Jan 2023


How to group by the top N positions using Tableau Desktop.  For example, when entering "5" in the parameter , sort the records by Sales and group by "5" records based on Sales ranking.

Default result:
User-added image

Desired result with 5 selected for the parameter:
User-added image


  • Tableau Desktop


As demonstrated in the attached packaged workbook, here are the solution steps:

1. Connect to the attached data source 'sample_data.xlsx' from Tableau Desktop.
2. Right-click on Customer ID and Convert to Dimension
3. Drag Customer ID to Rows and Sales to Text on Marks.
4. Create a new parameter with the following settings:
User-added image
5. Right-click on Group by N records and select Show Parameter.
6. Create a new calculated field with the following:
     Name : Sales_RANK
     Calculation : RANK(SUM([Sales]))
7. Create a new calculated field with the following:
     Name : GroupID
     Calculation : "Group"
                          STR(FLOAT(INT([Sales_RANK]/[Group by N records]-0.01))+1)
8. Drag GroupID next to the left of Customer ID on Rows.
User-added image
9. Right-click on Customer ID on Rows and Sort > Set as follows:
User-added image
Did this article resolve the issue?