Dynamic Sorting with Tableau

How to add a dynamic, interactive sorting feature to your Tableau Dashboard. The first article of a guest post series on “Expand your reach in Tableau with Parameters”

Sorting Oranges / Photographer: bighornplateau1 (flickr.com)To me, one of the most interesting sessions of this year’s Tableau Customer Conference was “Tips & Tricks from the Wild”, where five Tableau champions presented some of their best techniques in Tableau. One of these exceptional Tableau experts is Sheel Bhatiani, Lead Software Developer at Cheyne Capital. I liked his tip so much that I asked if he would be so kind to share some of his tricks here on Clearly and Simply. I am very happy to inform you that Sheel not only agreed to write one guest article here, but rather to publish a whole series of posts on how to “Expand your reach in Tableau with Parameters”. Sheel kicks off his series with today’s article. Enjoy.

Parameters – introduced with Tableau version 6 – are dynamic values that replace constants in calculations. They can be changed by the user of a dashboard or worksheet with an interactive control. This opens a lot of opportunities. Parameters in combination with Calculated Fields enable you to add a whole variety of additional interactivity to your Tableau workbook and dashboard. One of the most interesting things about Parameters is their ability to bring the existing Tableau built-features to the next level.

Today’s first article of my post series here shows the main concept of how to do this and includes a detailed tutorial how to use this for implementing a user-friendly interactive control to change the sort measure and the sort order of a view on a dashboard: Dynamic sorting with Tableau at your fingertips.

Are you ready to expand your reach in Tableau with Parameters? Here you go.

The Background and the Idea

Let’s take the Superstore Sales sample data coming with each Tableau installation and create a very simple view like this:

Superstore Sales View unsorted - click to enlargeA list of Customer States visualizing Net Profit and Sales with simple bar charts and Profit per Customer with a color coded tornado chart. You certainly know how to do this. Using the Multiple Marks feature, you put [Customer Name] on the Level of Detail Shelf and [Profit] on the Color Shelf for the tornado. No big deal.

Very often, analyzing data starts with sorting by different measures. And of course, Tableau provides different built-in ways of sorting your views: the one-click-sort using the sort buttons on a worksheet or dashboard or the persistent sort which allows you to sort by data sort order, alphabetic, by field or manually. However, sometimes the built-in features just ain’t enough.

The easiest way is selecting one measure by clicking on the axis (e.g. Net Profit) and then using the sort buttons on the Tableau toolbar. This works well for all three bar charts. However, sorting by Profit per Customer will sort the States by the total net profits. In other words, it will result in the same sort order as sorting by Net Profit. This works as designed, but what if you want to sort the view by profits only or losses only? Something like this:

Superstore Sales View sorted - click to enlargeOf course this is possible with Tableau in general.

First, you need two simple Calculated Fields:

[Profits only] : MAX([Profit],0)

and

[Losses only] : MIN([Profit],0)

Finally you set the sort order of [Customer State] to Sort by Field and select either [Profits only] or [Losses only].

However, this is only possible on the worksheet, not on the dashboard. And it is inconvenient to change the sort measure and sort order of the view this way. Wouldn’t it be nice, if you would be able to change the sort measure and the sort order with a convenient and easy to access interactive control directly on the dashboard?

Parameters and some more Calculated Fields allow you to easily implement such an additional interactive sorting control.

Here is the detailed how-to tutorial:

The basic approach – A step-by-step tutorial

The basic approach needs only one Parameter and one Calculated Field for an interactive dynamic sort feature.

Step 1: Create a Parameter

Right click somewhere on the data window and select Create Parameter. In the following dialogue window, give the new Parameter a meaningful name like “Sort States by”, select String as the data type of the Parameter and enter names for all measures in the list of values table. Since we have only one Parameter to select both the sort measure and the sort order, you have to add two entries to the list for each sort measure and an additional description to represent the sort order. You could simply add “ascending” and “descending” to the measure names or – as shown in the following screenshot – you may also use symbols like triangles (triangle up for ascending, triangle down for descending):

Parameter basic - click to enlargeStep 2: Create a Calculated Field for the Sort Measure

Next, right click again on the data window and create a new Calculated Field. The formula is a simple CASE WHEN statement. It evaluates the Parameter created in step 1 and assigns the according (i.e. user selected) measure to this Calculated Field. To switch from the default ascending to a descending sort order, we simply set the measure to its negative value.

The Calculated Field looks like this:

Calculated Field basic - click to enlargePlease be careful: the texts in this Calculated Field used after the WHEN statements have to be exactly the same as you defined them when creating the Parameter (see step 1).

Step 3: Set the Sort Order of the dimension on the Row Shelf

Click on the arrow of the [Customer State] pill on the Row Shelf and select Sort. In the following dialogue window, click on Sort by Field and select the Calculated Field created in step 2 ([State Sort Measure]):

Sort basic - click to enlarge

Step 4: Show Parameter Control

Finally, right click on the Parameter [Sort States By] in the data window and select Show Parameter Control.

That’s it.

Only 4 steps and you have an additional drop down list to change the sort measure and sort order of your view in one go.

Here is the interactive version on Tableau Public:

The enhanced version

The idea of the enhanced version is obvious: Instead of having two entries for each measure in the drop-down list of the Parameter, we add another Parameter to select the sort order.

Step 1: Create a Parameter for the Sort Measure

Pretty much the same step as in the basic approach, but only one entry per measure in the list of values:

Parameter Sort Measure enhanced - click to enlargeStep 2: Create a second Parameter for the Sort Order

This is a new step. Create another Parameter, again with data type String, but only with two entries in the list of values: ascending and descending. We call this Parameter [Sort Order]:

Parameter Sort Order enhanced - click to enlargeStep 3: Create a Calculated Field to transform Text Fields to Numbers

Another additional step is necessary. Since we want to sort the view ascending or descending not only by [Profit] and [Sales], but also by [Customer State Name], we have to transform the names of the state into a number.

Here is one possible way of doing this: A new Calculated Field ([State String Number]) converts the first 4 characters of the state names to a number using the function ASCII. Multiplying the characters by decimal powers and adding them results in a four-digit number which we can use for sorting the state names.

Calculated Field 1 enhanced - click to enlargeAgreed, this is a brute-force method. However, it works, at least in most cases. Please be advised that this technique will not work 100% correctly, if there are state names starting with the same 4 characters (e.g. North Carolina and North Dakota). But it is close enough for this example.

Step 4: Create a Calculated Field for the Sort Measure

This step is pretty much the same as step 3 of the basic approach, but the calculation is different. It is a CASE statement based on the Parameter [Sort States by] again, but please notice the additional IF-clauses within each WHEN statement. Furthermore we have to use aggregations (SUM and AVG) in order to make the formula work for sorting the state names:

Calculated Field 2 enhanced - click to enlargeThe CASE statement is pretty complex, including IF THEN statements within each WHEN. If you find this too hard to read and understand, there is another way to simplfy this Calculated Field.

Create another Calculated Field and call it – let’s say – [Sort Sign]:

IF [Sort Order] = “Ascending” THEN

1

ELSE

-1

END

You can now simplify your formula of [State Sort Measure]:

Calculated Field 2 enhanced (alternative) - click to enlargeThe result is exactly the same. However, you transferred the repeated IF THEN statements to another Calculated Field and simplified the CASE statement. This is probably easier to read and understand.

Step 5: Set the Sort Order of the Dimension on the Row Shelf

This one is exactly the same as step 3 of the basic approach.

Step 6: Show Parameter Controls

Again the same procedure as in step 4 of the basic approach, but this time you show both Parameter Controls: [Sort States by] as a compact list again and [Sort Order] as a single value list (radio buttons).

That’s it. Two simple steps more than the basic approach and you are good to go.

Here is the interactive enhanced version on Tableau Public:

The Drawbacks

The technique described above is a workaround. No more, no less.

Like any other workaround, it comes with some limitations and pitfalls. It takes some additional time to implement it (not too much in this case, though) and you have to define the sorting measures in advance. Those are only minor drawbacks from my point of view.

However, you should be aware of a more serious shortcoming of this technique. It only works as long as you or the user of your dashboard doesn’t change the sort order by using Tableau’s built-in sorting. After you selected a field and sorted it with the sort buttons on the toolbar, our dynamic sorting technique does not work anymore.

The root cause for this is the fact that Tableau sets the sort order to manual, if you are using the toolbar sort buttons. In this case, you have to go to the worksheet again and set the sort order of the dimension on the Row Shelf to Sort by Field again.

It goes without saying that the technique described above also stops working if you change the persistent sort on the worksheet to sort by data source order, alphabetic sort, another field or to manual sort.

As long as you are aware of this undesired behavior, you might be ok. However, this could be confusing for a user who does not know about this shortcoming. Even worse, if he has only access to the dashboard, but not to the worksheet (e.g. if he is using Tableau Reader or a dashboard on Server or Public), he can’t do anything about it.

There is one more drawback you should be aware of: using the Calculated Field as the sort criterion results in a more complex query and this may have negative impact on the database performance. If you encounter this problem in your implementation, you could grab the generated query from your Tableau Desktop log file and tune this with your DBA.

What’s next?

This was the first post of a series of articles on how to expand your reach in Tableau with parameters and to spice up your Tableau dashboards with even more dynamic, interactive features than Tableau already provides as a standard.

The next article will show a similar technique to create an interactive hierarchy.

Stay tuned.

Comments

40 responses to “Dynamic Sorting with Tableau”

  1. Dan Murray Avatar

    Great post Robert!

  2. Robert Avatar

    Dan,
    many thanks for your comment. The credits belong to Sheel. It is his idea and article, I only did the formatting.

  3. Nuno Avatar

    Dear Sheel,
    Thanks for this great article…
    Dear Robert,
    Thank you for publishing…
    Regards,
    Nuno

  4. Marko Avatar
    Marko

    Great post Robert. I was waiting for an update for quite some time; it was definitely a great read.
    I hope this can be accomplished in excel as well!
    Keep up the great work 🙂

  5. Robert Avatar

    Marko,
    thanks for your comment. You ought to thank Sheel. It’s his work, not mine. I am just hosting the article here.
    If you want to implement a formula based dynamic sorting in Excel, you may be interested in the following posts:
    You can’t start a fire without a spark
    here on Clearly and Simply or the second post of my guest article series over at Chandoo.org:
    Creating KPI Dashboards in Microsoft Excel
    You can’t change the sort order with a drop down list, but rather using radio buttons. However, the technique would be pretty much the same with a combo box.

  6. Moises Avatar

    Robert,
    I’ve read you’ve been kindda busy lately with your paid job but every post you make just amazes me.
    Thank you for such an excellent article. It has been very helpful on my way to learn more about Tableu.

  7. Robert Avatar

    Moises,
    many thanks for your comment and your very kind words. However, this article is Sheel’s brainchild. I am just featuring it here.
    As I said in my recent article, Sheel’s guest post series will continue soon.
    Please stay tuned.

  8. Brian Avatar
    Brian

    When creating the [State String Number] field, multiply each ASCII value by 26^x as follows. This ensures ‘ba’ is after ‘ar’ for example. You may also need to convert the text field to lowercase and account for strings shorter than the number of characters you’re checking.
    IFNULL(ASCII(MID([Customer State Name],1,1)), 1) * 26^5 +
    IFNULL(ASCII(MID([Customer State Name],2,1)), 1) * 26^4 +
    IFNULL(ASCII(MID([Customer State Name],3,1)), 1) * 26^3 +
    IFNULL(ASCII(MID([Customer State Name],4,1)), 1) * 26^2 +
    IFNULL(ASCII(MID([Customer State Name],5,1)), 1) * 26 +
    IFNULL(ASCII(MID([Customer State Name],6,1)), 1)

  9. Robert Avatar

    Brian,
    many thanks for your message. Very good idea and solution. There is still the limitation to the first n characters, but your Calculated Fields avoids at least the pitfall you mentioned.
    Thanks for sharing.

  10. Johncmunoz Avatar

    Thanks for the post Robert! Keep up the good work. I appreciate you taking the time to share your work and insights.

  11. Lars Nordin Avatar
    Lars Nordin

    How can I modify this so that it works with SETs, i.e. in my case [Customer State] is not a column of STRINGs but it is a SET of columns. I tried doing ASCII(LEFT(STR([Customer State]),1)) etc. but it didn’t work (and I’ve found out that ASCII(LEFT(STR([Customer State]),1)) always returns 116 regardless of the value of the SET [Customer State])
    Background info: Why I want to do this is because I have more than one Dimension and I try to work-around the “Nested Sorting”-dilemma (discussed here: http://community.tableausoftware.com/message/184181#184181) which means that I cannot sort according to my measures “outside” of the parent-column.

  12. Robert Avatar

    Lars,
    I am not 100% sure I understand your question, but here are my 2 cents:
    A set in Tableau is a user defined subset of a dimension. It either returns true (value is in the subset) or false (value is out of the subset). Your Calculated Field will return the ASCII code of the first character of the set: either 116 (ASCII code of “t” for true for all values in the subset) or 102 (ASCII code of “f” for false for all values out of the subset).
    To get around the “Nested Sorting”-dilemma, a set won’t help. What Ross is using in his answer on the Tableau forum you linked to isn’t a set, it is a combined field.
    You can use this in the dynamic sorting approach, too. Simply right click on 2 dimensions in the data window and select Combine Fields. Drag this to the Rows Shelf and set the sort of this combined field to Sort By field [State Sort Measure] (in the example posted in the article).
    Or am I completely misunderstanding you?

  13. Lars Nordin Avatar
    Lars Nordin

    Hi again! (I managed to solve this using a work-around, read at the end of this message)
    Well a slight misunderstanding =)
    Where I wrote SET I meant is a combined field (why I wrote SET is that the datatype of combined field is callet SET in Tableau).
    When you sorted [Customer State] (String) you used an ASCII sorting. Now I want to do the same with [Customer State] (Combined Field). Which I can’t because the ASCII-function will return 116=t=first letter in True.
    However, I managed to solve all this by creating [Customer State] as a Calculated Field with a concatenated string (STR([State]+’, ‘+STR([City]) instead of a Combined Field! So now it is a string that I can use ASCII-functions on.
    (It would however be useful to learn how to use a Calculated Field to extract values from a Combined Field, because I guess this is the root of my issue, that the only value I get in return is TRUE.)

  14. Robert Avatar

    Lars,
    I agree, the concept of Combined Fields is a bit confusing in Tableau. The symbol of a Combined Field in the data window is “Abc”, indicating it would be a string. If you drag it to a shelf or a mark, it shows the combination of the 2 dimensions separated by a comma (or another user defined separator), but if you use it in a Calculated Field, the value of the Combined Field is always “true”.
    I think the only way to get what you want is – as you described – a Calculated Field concatenating the 2 dimensions instead of using a Combined Field.
    Having said that, I do not see why you would need to extract values from a Combined Field. Since it is per definition a combination of dimensions, you can always use those dimensions in your Calculated Fields to get what you want. If you have a Combined Field of State, City and want to e.g. get the first character, you just perform a LEFT on [State].

  15. SM Avatar
    SM

    Hello,
    Thanks for the post; what if we want to sort date instead of a measure such as profit; would negative sign works as well?
    Thanks
    SM

  16. Robert Avatar

    SM,
    there is no such thing as a negative date, but you could use INT() or FLOAT() to convert the dates into numbers and the technique should work for dates as well.

  17. Anonymous Avatar
    Anonymous

    Quick comment on what I know is by now an old (though still interesting) article. One extra trick for getting around the “sort by name or number” problem is to use a calculated field of the following form:
    IF [Sort By] = “Name” THEN 0, ELSE Value END
    By assigning 0 to your sort key calculated field all lines with end up with the same value, which makes Tableau fall back on a second level sort which you can use the name for.

  18. Sandip Pandey Avatar

    How did you label with traiangle in parameter?

  19. Robert Avatar

    Sandip,
    I created the texts with the triangles in a text editor (e.g. Word), copied the text and inserted it into the parameter list in Tableau. It is important to use a symbol from one of the standard font types (like Arial), not from a special font like Symbol or Wingdings 3.

  20. manish mishra Avatar
    manish mishra

    @sandip have you used
    Insert-Shaped-triangle shape
    in MS-word to do that
    Not getting.

  21. Ankamma Chowdary Avatar
    Ankamma Chowdary

    Hi Can you plz help me out in Sorting Multiple Dimensions and a Measure in a view ?

  22. Robert Avatar

    Ankamma,
    you can combine measures and dimensions in dynamic sorting by following the steps 3 and 4 in the section “the enhanced version” of the article above.

  23. Pradeep Avatar
    Pradeep

    Awesome explanation with Screenshots.. Very very helpful.. Please keep posting.. 🙂

  24. Hema Avatar
    Hema

    Hello,
    Thank you for the post. I am trying to use the same.. I have numbers to sort on. And the sort that I use on one worksheet, the same has to be applied on another worksheet. In that case, parameters won’t quite work. Any other solution to this.
    Thanks,
    Hema

  25. Robert Avatar

    Hema,
    sorry, I do not understand the problem. Why should the parameter based custom sorting not work across several worksheets? A parameter is global, i.e. it has one defined value throughout all sheets in the workbook. As long as you have set the Sort by Field correctly on all sheets, the sort order will change on all sheets after you changed the parameter.

  26. Naveen N Avatar

    Hi team, I have created the bar chart using measure names and measure values for x,y,z,… variable but i am unable to sort it based on the measure values and i want to mark on the chart top three variable based the highest values after sorting.
    please give me some solution for this..

  27. Robert Avatar

    Naveen,
    sorry for the late reply. Can you elaborate a little bit about why you are not able to sort the data based on the measure values? Or even better, can you post an example workbook somewhere so that I can have a look? Thanks.

  28. vimal Avatar
    vimal

    how to sort the aggregated value. my aggregated value is actually a percent difference(table calc)
    i tried many methods and failed.
    clicking manual sort is not helping if value changes next day.

  29. vimal Avatar
    vimal

    sort by list is not showing the aggregated value as dimension. and am not able to move that measure to dimension too.

  30. Robert Avatar

    Vimal,
    can you post your workbook somewhere for download? It is easier to understand your issue / question with an example workbook.

  31. Marta Avatar
    Marta

    Hi 🙂 I have a small problem – I followed the tutorial closely (basic version) but when I click “Sort by Field” in a Sort window I can’t select the Calculated Field created in step 2 because it is not on the list. I am using Tableau 9.0 – how to solve that? I am sure every other steps are done correctly.

  32. Marta Avatar
    Marta

    It is the same case vimal had. When the value is AGG it is impossible to chose it from the Sort by Field list. How can I repair it?

  33. Robert Avatar

    Marta,
    I am sorry for this very simple question, but it is hard to tell what is going on without seeing the workbook: are you sure the calculation is valid? Maybe you are mixing aggregated and non-aggregated measures and the Calculated Field is invalid?

  34. prolay banik Avatar
    prolay banik

    Hello
    Thanks for the information shared here..
    I’ve one question..
    Can I manually sort of a variable used?

  35. Prolay Banik Avatar
    Prolay Banik

    Its me again..
    I’ve values like BSX, GMED, Key Comp1, Key Comp2, SNN, SYK, ZBH..
    Its sorted alphabetically, but I want to sort it in my way like Key Comp1, Key Comp2, BSX, GMED, SNN, SYK, ZBH

  36. Robert Avatar

    Prolay,
    in the data tab right click on the dimension, select Default Properties and Sort, activate Manual and use the Up and Down buttons to define your prefered sort order.

  37. diji Avatar
    diji

    This article solved my issue, and the instructions were easy to follow. In case it helps another newbie: I had both SUM and AGG measure values. In the CASE statement, the SUM measures were straightforward:
    …THEN SUM(measure1)
    but the AGG measure values gave me an error until “AGG” was removed in the CASE statement:
    …THEN (measure2)
    instead of THEN AGG(measure2)
    Also, you can create the up and down arrows using Alt-30 and Alt-31.

  38. cakesnake Avatar
    cakesnake

    I am running into an issue where I have a string that starts with a number e.g 1st anniversary if I use ASCII formula for a string starting with 10th it is actually a lower value than 1st and putting the string with 10th on top of the string starting with 1st any workaround would be appreciated

  39. Robert Avatar

    cakesnake,
    I would create a Calculated Field which extracts the numbers from the strings, e.g.:
    REGEXP_EXTRACT([Value], ‘(\d+)’)
    If you then sort the view by this Calculated Field, you should get what you want.

  40. Abhilash P S Avatar

    Thank you for the post, I was working on this, this worked out well for me.

Leave a Reply to Lars Nordin Cancel reply

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