2 Replies Latest reply on Jan 31, 2014 9:58 AM by Bradley Collins

# For a stacked bar chart, how do I display the Top N, but then change the sort order of the bars?

Hi all,

I am trying to create a stacked bar chart, but the dimension on the x-axis has too many values to reasonably show at once. I would like to show just the top (or bottom) N of those dimensions at once. But as this plot will be hosted on Tableau Server, the chart needs to also be sortable by the viz user (e.g., if the chart is in Descending order then it should show the *top* 10 customers by total sales, but switching to Ascending order should show the *bottom* 10 customers by total sales). Is there a way to create such a chart?

# Two approaches I've tried without success:

1) Create a Top N filter on Customer Name based on SUM(sales): This works for only one sort order at a time -- if I set the filter to give me the Top 10 customers in descending order, then the user switches the chart to sort in ascending order, the original filter still applies and the chart doesn't make sense. That is, the customers are first filtered to the 10 highest by sales, then sorted within that 10 so that the "lowest" customer is actually the 10th highest instead of the actual lowest.

Top 10 + descending order (good):

Top 10 + ascending order (bad):

2) Create a calculated field = index() and filter on that field: This approach works for a normal (non-stacked bar chart) but not with a stacked bar chart -- at least not without modification. I see that the segment of the bar (Item, in this case) is being taken into account in the Rank = Index() table calculation, which would seem to be a problem, but I can't figure out how to change this:

I've created a dummy workbook to explain this question (attached); one sheet shows method 1, the other sheet shows method 2.

Thank you for your help!

Bradley

• ###### 1. Re: For a stacked bar chart, how do I display the Top N, but then change the sort order of the bars?

Let me know if this helps.

1 of 1 people found this helpful
• ###### 2. Re: Re: For a stacked bar chart, how do I display the Top N, but then change the sort order of the bars?

Thanks, that did the trick!

Your solution actually worked better when I removed the Sort Parameter and simplified the Rank Filter to just "if index()<= [N Customers] then 'yes'". Adding additional sort controls based on a non-plotted calculated value doesn't play well with Tableau's built in sort button, unfortunately. In this case, the using the Rank Filter and Tableau's sort button appear to be sufficient.

For any future readers of this thread, my solution (based on Aaron's) is attached.

Thanks again, Aaron!