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
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
- If Index()=[Top N] +1
- Then Window_Sum(Sum(Sales),0,Last())
- Else Sum(Sales)
- End
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())
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
- If Index()=[Top N] +1
- Then Str(Size()-[Top N])+" Others"
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.
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