If you’ve ever used Tableau you’re probably familiar with the sort buttons. Clicking them will instantly sort your data in ascending or descending order. And while this is very straightforward in simple usecases, you might come across more complex problems where they don’t work the way you expect them to.

The Problem

You’re interested in your top-selling product categories across regions, so your worksheet looks like this. Note that I’ve already applied the sort function.

Hold on, that’s not sorted at all! It actually is, but Tableau sorts it at the total Region level. If I remove Category from the y-axis, Tableau sorts my Sales perfectly by Region.

The Solution

But how do we keep both measures and sort our sales correctly? You can achieve that by using a Ranked Sort. Just follow these 3 simple steps:

1) Create the following calculation in the Row shelf:


The Rank function returns a rank for the current row compared to all other rows. The table calculation should be computed using Pane (Down).

2) Change your calculated field to discrete in order to create a separate column.

3) Drag your Rank field between Category and Region and your worksheet should look like the one below. Lastly, remove the Rank coloumn from the view by unticking the Show Header option.