Saturday, May 30, 2015

Grouping Lower Ranked Bars in Bar Chart

In a recent post, C. Palo has asked for a "top N and others" solution. There is a KB article on the subject. But it's based on sets, which won't work for C. Palo because she has a few filters. So the top N will be dynamic while sets based approach only provides static top N, regardless of filters.

I would present here a tutorial on the topic. It is based on an approach by Tableau Zen Master Richard Leeke while using a different filter. Also would I explain some of the intricacies. This approach works on filtered results if any.

The example uses the superstore data set where we will show the top N countries with the highest sales, and group the others in a single bar.

Step 1. Create an integer parameter [Top N]
Step 2. Drag [Country / Region] to Details shelf and sort it by Sum(Sales) in descending order.
Note that all the 4 calculated fields below need to be set to compute using  [Country / Region].

Step 3. Create an Index: Index()
Turn it into discrete and drag it to Rows. Set it to compute using [Country / Region]. This index is mainly for the ordering of the results in final presentation.

Step 4. Create a [Top N+1 Rows] filter:
  • Index()<=[Top N] +1
Drag it to Filters and set it compute using [Country / Region]. Select True at pop up.

We only need N+1 rows/bars/marks in the chart. There comes the filter. The (N+1)th row will show "Others" with its aggregated value.

Step 5. Create [Top Countries & Others]. This will alias the country at the (N+1)th position to be "Others". Drag it to Rows and set it to compute using [Country / Region].
  • If Index()=[Top N] +1
  • Then "Others"
  • Else Attr([Country / Region])
  • End
Step 6. Create [Top Sales & Others]. At the (N+1)th position, the value is altered to be an aggregated result. Drag it to Columns and set it to compute using [Country / Region].
  • If Index()=[Top N] +1
  • Then Window_Sum(Sum(Sales),0,Last())
  • Else Sum(Sales)
  • End
Note that in the above formula, the If statement sets the index=0 for the window function which has its own index. The settings here instructs the window function to compute from (N+1)th row to the last row.

The aggregation at the (N+1)th row can be the average of all the others if you wish:
  • Window_Avg(Sum(Sales),0,Last())
We are done with a basic "Top N & Others" bar chart with grouping all others in one bar. Note that all table calculations have to compute using [Country / Region].
Dress up

Add a few extra steps to make the presentation better.

- Drag [Top Sales & Others] to Label/Text shelf and set it to compute using  [Country / Region].
- Create [Others] to color Top N and Others differently. Set it to compute using  [Country / Region].
  • Index()=[Top N]+1
- In Step 5, add a little calculation to show # of countries in the "Others".
  • If Index()=[Top N] +1
  • Then Str(Size()-[Top N])+" Others
Here is the final result. The workbook can be downloaded here.
If you want to have the option of expanding the countries in "Others", you can use a method proposed by another Zen Master Joe Mako.

Grouping Others per Category

The above approach can be applied to "Top N and Others" per category. Here is an example based on a question in the Tableau forum. It includes both a bar chart and a cross tab. This is an alternative solution to the one proposed by Zen Master Joe Mako.

An important change is using Rank_Unique(Sum(Sales)) for indexing, instead of Index(). This is based on an approach described in an early article.

Make sure all the table calculations in this example to be set to compute using Category.
Top N & Grouping Others Series

No comments:

Post a Comment