Quick Tip - Context Filters in Tableau

by Carlo Sanzeri

When beginning my training at the Data School, there was one aspect that jumped out to me when thinking about showing the correct data in your viz. This aspect is context filters.

So what is a context filter?

Simply put, a context filter is just a normal filter that overrides Tableau's normal Order of Operations. When you use a regular filter in Tableau, you are in essence telling tableau to apply that filter to each row of your dataset. This works most of the time - however the issue comes when you are trying to use different types of filters at once, as Tableau will assess these independently of each other, leading to confusing results.

From Tableau's Order of Operations, we can see that context filters are quite high up the chain. 

Using a context filter over a regular one

Let's set up an example where your boss is going into a meeting and needs the top 10 best selling technology products at your company. You take your SUM(Sales) and your product dimension, set the product dimension to only show the top 10, set the category to Technology and build a viz.

You see the results and your heart sinks. You swore this Tableau thing was easy, and that you could get the task done quickly.

So - why hasn't it worked?

Well, let's refer back to the order of operations. As we can see, Top N Filters come before the computation of dimension filters. This means that Tableau is working out the top 10 first, and then applying your category filter. This leads to inaccurate or incomplete results as your data is being filtered out after your calculation. Luckily, the fix is simple. Simply right click your category filter and click 'Add to Context', and voilà! Your context filter has now applied the category filter before your Top N Filter, leading to a fully built viz.

10 Products - job done!

All done, your viz is fixed!