<< Back

Understanding Sorting in Tableau

Have you ever noticed that using the quick sort in Tableau sometimes produces unexpected results? Particularly when you have more than one Dimension in play?

Tableau does not comply with ‘traditional’ sorting methods – the kind you might find in Microsoft Excel for example. This post is going to explore why Tableau sorts the way it does, and how we can get around some of the more complex sorting issues.

Quick Sort

The quick sort function in Tableau can be useful in most simple scenarios. For all my examples I’m going to be using Superstore Sales. In the image below I have a bar chart showing profit by state.

1

We can use a quick sort on either our State field, or our Profit field.

2

A quick sort on state sorts our bars ascending or descending on the state name.

3

Whereas a sort on profit will order our bars by the amount of profit.

At any time we can bring up the Sort dialogue box (right-click on the Dimension field in the view and select Sort) to see what Tableau has applied to create the quick sort.

4

Above is how Tableau applied a quick sort by Profit. Note – you can go ahead and select any Measure field to sort by, even if it’s not in play on the View.

Quick sorts are great when we are creating quick explorations of our data and our charts are fairly simple. However, challenges arise when we start adding in additional fields to the view.

Why is my sort broken?

Say I want to find out across regions and container types the amount of profit I make. I want to be able to sort my bars by profit within the different regions. So I use a quick sort on profit, here is what I get:

5

Hmm. Something’s not right. In the Central region the Small Pack container is making less profit than Large Box, but the bar for Small Pack is above the bar for Large Box? Why does Tableau do this?

Tableau is actually taking in to account the data across all Regions when applying this sort. Across all regions Small Pack accounts for $67,161 of profit, whereas Large Box accounts for less: $65,490. So Tableau is sorting the bars within our Regions with Small Pack above Large Box. While this type of sorting may be useful in certain scenarios, it’s not what I’m looking for today.

So – how do we get around this? In version 8 we can create a combined field. In version 7 we would have to create a set. This post will follow using the version 8 method, but for more on the version 7 method you can click here.

Combined Fields

To create a combined field multi-select (click while holding Ctrl) the Dimensions required in the view. In this case that’s Container and Region. Then right-click one of the selected Dimensions and select Combine Fields.

6

The combined field will then be found in the Dimensions pane. You can edit the field at any time by right-clicking. You can change the order of the fields, rename the field and set what character the members are separated by.

7

 

8

You can add however many fields to the combined field as needed. So if you need to sort based on three Dimensions, just multi-select all three before you create the combined field.

Now we have our combined field, we can re-create the view using the combined field, rather than the Region and Container fields.

9

The last step is to re-create the sort on the new combined field.

10

Here’s our final view:

11

Success!

You can find out more about how Tableau sorts from these links:
Jonathan Drummey’s great Tableau Wiki
Nested Sorting from Tableau’s Knowledge Base

 

 

Emma Whyte

London, UK

6 thoughts on “Understanding Sorting in Tableau

  1. @Manoj: Place the combined field in between the other two fields and then hide it.
    In that case, you would have on the rows shelf: Region, Region & Containers, Containers. Do the sort on the combined field, and voilà!

  2. An alternative is to use RANK then sort it based on that.

    Create a calculated field that says RANK(SUM([Profit]),’desc’). Make it Discrete

    Add this between the 2 Dimensions (e.g. Region and Sub-Category) then modify the Table Calculation to Compute Using either Pane (Down) or the 2nd dimension (e.g. Sub-Category)

    Format to remove the lines between each Sub-Category.

    Hope this helps

Leave a Reply

Your email address will not be published. Required fields are marked *