I will use the same data in pie chart example which makes things easier. The statement of the problem is how to group the lower % bars into a single one after a sorted measure. In the example, we need to group those lower % sales countries into a single label: "Others".
Create the Bar Chart
1.Create a parameter [N% Chooser] which allows us to choose any % as threshold. Show the parameter control. One can skip this if the percentage is a given number.
2.Create a new field [Top % + Others Label] which labels all low % slices as "Others". Drag it to the Row shelf. Note that the percentage is dynamically controlled by the parameter 0.01*[N% Chooser]. One can replace it by a given number if necessary.
3.Drag the [Country/Region] to the Detail shelf.
Now we got a barebone bar chart with an "Others" bar. Dressup is in order and will be done next.
Dress Up
- Click the descending sort button to sort the bars.
- Click the "42 nulls" at the down-right corner and you will see a pop up. Click Filter Data to get rid of the Nulls. A green pill will appear in the filter shelf.
- Create [Percent] field and drag it to the Label shelf. Right click on it and select Format to make the numbers in %.- [Top % + Others Sales]/Total(Sum([Sales]))
- Create a [Is Top %] field as follows. Drag it to the Row shelf and place it before [Top % + Others Label]. In the table, drag True to above False. Then right click it to uncheck Show Header.
- [Top % + Others Labels] !="Others"
- Right click the horizontal axis and uncheck "Show Header".
- Right click the canvas and select Format. Go the grid menu and turn both Row and Column Divider to None. This will remove some unnecessary horizontal and vertical lines.
And it's done! The workbook can be found here.
Calculating [Top % + Others Sales] and Filtering
To help understand the calculation, we attached a table in the above workbook. The table values are sorted descendingly according to sales. We see that all the lower % countries are labeled with "Others". And only the last country in "Others" has values. The rest of countries have Nulls.
The calculation is as follows:
if [Top % + Others Labels]="Others"
then if Last()=0
then Window_Sum(if [Top % + Others Labels]="Others" then Sum([Sales]) end)
end
else SUM( [Sales] )
end
if [Top % + Others Labels]="Others"
then if Last()=0
then Window_Sum(if [Top % + Others Labels]="Others" then Sum([Sales]) end)
end
else SUM( [Sales] )
end
It shows that, for the new label "Others", we need a different calculation. Otherwise it's just Sum([Sales]).
Widow_Sum() will only sum up the sales of those countries labeled with "Others".
We only need one value which sums up all the "Others". Last()=0 will do that. The rest is filtered out.
Widow_Sum() will only sum up the sales of those countries labeled with "Others".
We only need one value which sums up all the "Others". Last()=0 will do that. The rest is filtered out.
Voila!
Top N & Grouping Others Series
I love this post! It's helped me numerous times the past 2 weeks! I was wondering if you could explain why you need "if [Top % + Other Labels] = "Others" twice? Thanks!
ReplyDeleteWindow_Sum() is not affected by the first condition. That's why we need to specify the same condition within Window_Sum().
DeleteIf we don't set the condition within Window_Sum(), it will add up all the slices.
A related post is published recently
Deletehttp://vizdiff.blogspot.com/2015/04/binning-aggregates-as-dimension.html