Maintaining Percentage Of Total Calculations When Showing/Filtering Top N In Tableau

Perhaps you have encountered this challenge before in Tableau.

Imagine you are performing a percentage of total calculation on sales for each state, and are filtering for the top N states (for sales), as shown in the image below.

Screenshot (7)

 

Your percentage of total adds up to 100%, so at first glance this looks perfect.

Change your top N filter to show the top 5 instead of the top 10 however, and you may notice a problem.

Screenshot (8)

 

The percentage of totals have changed from last time.

What’s happening is that Tableau is calculating the percentage of total only for the states we have in the view.

Our percentage of total calculation is in fact incorrect – if we are filtering out some states, our percentage of total calculation should not add up to 100%!

How do we fix this? How do we filter for the top N and maintain our percentage of total calculation at the same time?

Researching on the internet, there are various methods, however I particularly like the solution Andy showed me.

First, let’s create a rank for our states, based on their sales.

Screenshot (9)

 

Let’s convert this calculated field from continuous to discrete, and put the discrete rank into the view.

Screenshot (10)

 

Here comes the clever part.

Create a calculated field that will tell you whether your state is in the Top N. In the screenshot below, “Show Top N” is the name of the parameter I made to allow the user to change the number N.

Screenshot (11)

This will give you a true/false field. Put the new field into the view. Take “State” off filter. Your view now looks like this.

Screenshot (12)

Now; right click on “False” under the “In Top N” header and select “Hide”.

Screenshot (13)

This will hide any states that aren’t in your top N selection. Crucially though, it’s not excluding them, thus the hidden states are still included in your percentage of total calculation!

Now you should be left with this view (I’ve hidden my column grand total)

Screenshot (14)

 

If I change my “Top N” from 10 to 5, my percentage of total calculation is preserved.

Screenshot (15)

 

Credit to Andy for the tip!

Author:
Jules Winceslaus
Powered by The Information Lab
1st Floor, 25 Watling Street, London, EC4M 9BR
Subscribe
to our Newsletter
Get the lastest news about The Data School and application tips
Subscribe now
© 2024 The Information Lab