This took me FOREVER to finally figure out, so I wanted to share a method to avoid a common mistake when trying to use Tableau’s Top N or Bottom N filter. The issue is that often times when the Top filter is applied, it applies the filter against the entire, unfiltered source data, while the user is likely expecting the Top N (or Bottom N) to be select only after all the other filters have already been applied. Here are the steps I’ve taken in some sample data with the ultimate goal of selecting the “Top 3 Markets in Texas.”
Step 1: our original data.
Here, I’ve taken a list of customers by state.
Step 2: Filter the top 3 markets.
Right-click on LocationMetro > Filter > Top tab. Then select “By Field” and enter 3.
Step 3: Results – top 3 markets overall (still need to filter on Texas).
Step 4: Filter on Texas.
Wait! Our results have only 1 Market? I wanted 3 markets!
Step 5: Apply Context Filter on State
In order to preserve our “Top 3” filter, we must add a Context Filter. A Context Filter will apply the filter FIRST, prior to any other filters on the page.
What was happening in Step 4, was that the worksheet was choosing the “Top 3” markets out of all of the states first, and then applied the Texas filter.
Step 6: Make Sure your Context Filter didn’t reset. In this example, make sure Texas is the only state selected.
In my experience, Tableau often resets all of the filters in the context filter, which requires the user to go back a re-select the filters. In this case, all the states were selected again, so I had to go back and unselect them all and then choose Texas.
We’re done! Our chart now shows the Top 3 Markets in Texas!
3 thoughts on “Context Filters with Tableau, Important for Top N Filters”
I am learning tableau and this is interesting to know.
Is it true that the context filter means that Tableau creates a temp table storing only those records matching the context and returns that dataset ? If that’s the case then a datasource with context=TX is only good if everything you are reporting on from that datasource is TX data.
Chris, I’d tend to agree with you, but the context filter would only apply to that particular worksheet. If there are other worksheets in the same Tableau workbook that share the datasource, those other worksheets would not be limited to where context=TX. If we wanted all worksheets to share the filter, then we could either apply that filter at the datasource level, or make the click “Apply to all worksheets” in the filter menu.