Monday, April 29, 2019

Sorting Stacked Bars in Tableau

Someone came up with a question: how to sort stacked blocks within each stacked bar?
Each bar is a stack of small blocks of various sizes. Usually the blocks are all sorted by the same category, uniformly across all the bars. How to sort each bar individually according to the sizes?

I wondered about the same question before, but never actually solved it. So this is the time to give it a shot. It worked out pretty well. Based on the superstore data set, I created stacked bar chart showing Sales per Year and per Sub-category.

The solution is fairly simple: create a LOD expression of Sales by Year and Sub-category.

  • {Fixed Year(Order Date), Sub-Category: Sum(Sales)}

Put this new dimension in the Detail shelf. Drag Sub-Category into the Color shelf as an attribute ATTR(). The key of creating that LOD expression is being able to manipulate each block in every stacked bar.

Voila, we have each bar sorted by Sales.
Click the above image to view the interactive version.

Update 10/12/2020: Over at twitter https://twitter.com/IvettAlexa/status/1315579768734183428?s=20, I just learned there is another approach pre-LOD. Here it is https://kb.tableau.com/articles/howto/sorting-segments-within-stacked-bars-by-value This is a better solution I think.

Update 6/22/2023: A slightly change to the above solution at kb.tableau.com: No ATTR is necessary. Instead, place Region pill in Color under the Combined dimension pill in Detail. It works!


8 comments:

  1. I have a table calculation in my worksheet .So I am not able to sort my dimension according To that measure Could you pls provide a solution

    ReplyDelete
    Replies
    1. Try to replace that table calc by a LOD calc. Otherwise send me the workbook if you wish. Can't help without seeing your workbook.

      Delete
  2. Sorry I can't send you the worksheet.

    My calculated field contains total value

    Like count(requests)/total(requests)
    How do you want me to replace this to LOd?
    I really nt getting an idea

    ReplyDelete
    Replies
    1. Try {fixed dimension: count(request)} and put this in DETAIL. Note to use ATTR(category) to color.

      Delete
    2. The new dimension should be {fixed year, category: count(request)}. Drop this in the Detail shelf.

      Delete
  3. follow me on twitter @aleksoft, if you wish.

    ReplyDelete
  4. Could you explain why we need attribute ATTR(Sub-Category), not just Sub-Category?

    ReplyDelete
    Replies
    1. Since we are sorting the blocks in each bar independently of other bars, we need to operate at a finer level: sub-category+year. Sub-category alone is only used to color the blocks, without being a dimension. In other words, we do not need this sub-category as a standalone dimension. It could be there as a dimension. But it may affect the finer dimension in the sorting. For example, if we place the sub cat dimension in the Detail box above the sub-cat+year dimension, the sorting will be messed up. If we put the sub-cat dimension under that, the sorting is good. It is a redundant dimension which may interfere with the sorting. So we removed it.

      Delete