What is Context Filter in Tableau?
A context filter is an independent filter in Tableau and is one of the filters in Tableau’s order of operations. A context filter can help you enforce a filter to be evaluated first and create a dependent numerical or Top N filter. By defining a context filter, you can instruct Tableau to include only the specific data population and then set up a numerical or Top N filter. Similarly, you can define conditions or create a Fixed level of detail expression using the context filters.
For example, A large manufacturing company deals with the manufacturing of different products across the globe. You may be tasked with identifying the top 10 products by Sales per region. For large datasets, consider setting up a context filter to include, say, the South region and then create a dependent filter, i.e., top 10 products by Sales, to narrow down the filter to the top 10 products by Sales in the South region. The Tableau order of operations involving Context Filters is below.
Table of contents
Key Takeaways
- A context filter in Tableau can help you with enforcing a filter to be evaluated first and also creating a dependent numerical or Top N filter.
- Context filters have multiple features, such as independent execution control and prioritized order of operation compared to standard filters, and are static.
- Using context filters, you can improve your query performance and enforce the order of operation in Tableau.
- You can create single or multiple context filters in the Tableau view.
- The context filters can be used in combination with the Dimensions filter, Measures filter, or Table Calc filters.
Features
Some of the key features of the context filters in Tableau include:
- Execution control – Context filters are independent filters, and hence, if there are any dependent filters, the data first need to go through the context filter before they get processed by these dependent filters
- Order of Operation – Context filters are evaluated at the top compared to other filters such as dimension filters, measure filters, table clamp filters, etc.
- Static Nature – Context filters are primarily static and hence can’t be altered dynamically.
Types
Tableau doesn’t provide any specific type of context filter. There is only 1 context filter option available for the user that can be used for setting up a context filter for your visualization purpose.
You can define a context filter by choosing the Add to Context option for a field in the Filters pane.
How to create a Context Filter in Tableau?
In this example, we will demonstrate how to create and use a context filter in Tableau. To create a context filter in Tableau, follow the instructions outlined below:
Step 1: Connect to your dataset from the Tableau interface.
Step 2: Drag the fields to the view and sort the data to create a visualization.
Step 3: Drag a field to the Filters pane. In the Filter screen, create a Top 10 filter to display the top 10 states by total sales. Click on OK to save the changes.
Tableau view will display the Top 10 states by total sales.
Step 4: Drag the Product field to the Filters pane and choose a filter condition from the list in the Filters screen.
Tableau will display the Top 10 states by sales for Men’s Apparel products.
Step 5: Right-click on the Product and choose Add to Context. It will create a context filter in Tableau.
You will see the updated Tableau view showing the Top 10 states by sales for Men’s Apparel products.
Now, if you notice, at the bottom of the result, there are two values, i.e., Ohio and Michigan, which were missing in the previous visualization and are now visible as a result of the use of a context filter in Tableau. The Top 10 filter is now evaluated based on the context of the Product field on which the context filter is created.
In the next section, we will look at a few examples to understand more about the context filters in Tableau.
Examples
In this section, we will demonstrate different use cases where you can use context filters in Tableau.
Example #1
In this example, we will demonstrate creating a context filter in Tableau using the UEFA EURO 2024 – Players dataset. The UEFA EURO 2024 – Players contains the dataset of all the players that are in the squad of the teams participating in the UEFA EURO 2024, including the info about clubs, age, height, market value, etc., which can be very good for EDA and Data Visualizations.
To create a context filter in Tableau, follow the instructions outlined below:
Step 1: Import your dataset into the Tableau interface using the File Navigator in Tableau Desktop.
Step 2: Drag the Name and Market Values fields to the Rows shelf and the Columns shelf, respectively.
Step 3: Sort the results in descending order by clicking on the Sort descending icon.
Step 4: Drag the players’ name to the Filters pane. In the Filter dialog box, under the Top tab, specify the filter condition to evaluate the Top 10 players by Market Value. Click on OK to save the changes.
Tableau will display the top 10 players by their market value as shown below. You can also drag the Market Value field to Color and Label to your visual.
Step 5: Change the view from Standard to Entire View.
Step 6: Drag the Country field to the Filters pane and choose values under the General tab. Click on OK to save your changes.
Tableau will display the Top 10 players based on Market Value and choice of Country values. You can also add Country to the Color on the Marks pane.
Step 7: Right-click on the Country field in the Filters pane and choose Add to Context to create a context filter.
Now you can see the list of the top 10 players has changed in Tableau view. Harry Kane has now been added to the list. It is due to the inclusion of a context filter in the view.
Example #2
In this example, we will demo another use case of creating and using a context filter in Tableau using the University Students Data. The University Students Data contains various university metrics and characteristics such as the number of applications received, number of applications accepted, number of students enrolled, new students from the top 10%/25% of H.S. class, cost associated with books, outstations, personal expenses, etc.
To create a context filter in Tableau public, follow the instructions outlined below:
Step 1: Import the University Students Data dataset in Tableau public desktop using File – Open.
Step 2: In a new worksheet, drag the University Name and Apps, i.e., Applications received, to the Rows shelf and the Columns shelf, respectively.
Step 3: Drag the University Name to the Filters pane. In the Filter screen, define the filter condition for the Top 10 University Name under the Top tab.
Step 4: Change the view from Standard to the Entire View. Drag the Apps field to the Label on the Marks card.
Step 5: Drag the Private field to the Filters pane. In the Filter screen, provide the choice of filter in the General tab. Click on OK to save the changes.
Tableau will display the total number of applications received from the top 10 universities in view.
Step 6: Right-click on the Private field in the Filters pane and select Add to Context. It will create a context filter in the Tableau view.
Tableau will display the data filter values as seen below. Similar to the previous example, a new university has been added to the list. It is a result of the context filter that you have added to this view.
Context Filter vs Standard Filter in Tableau
The key differences between a context filter and a standard filter in Tableau are highlighted below:
Parameters | Context Filter | Standard Filter |
---|---|---|
Objective | These are independent filters that narrow down the data population that other standard filters can process. | These filters narrow down the data population to be processed based on the standard conditions defined. |
Order of Operation | Context filters are executed higher in priority as compared to other filters. | These filters are also known as dependent filters, as they process the dataset that passes through the context filters (if defined). |
Dynamic Nature | These filters are fixed in nature. | Standard filters can be both static and dynamic, depending on the setup. |
Performance | For large datasets, these filters can be useful due to query performance. | The query performance may or may not be efficient, depending on the complexity involved. |
Advantages
The key advantages of using a context filter are outlined below.
- Query Performance – Context filters can help you optimize the query performance, especially for large datasets by defining data modelling and applying applicable filters
- Tableau order of operation – Context filters rank on top in terms of order of execution as compared to other filters in the Tableau order of operation. Hence by defining a context filter, you can enforce other filters to process the dataset passed through the context filters
Limitations
Some of the limitations associated with context filters are highlighted below:
- Static Filter – Context filters are static, and once specified, they remain static throughout unless they’re redefined in Tableau.
- Query performance – Context filters may adversely affect the query performance if they are not properly defined, especially for large datasets.
Importance Things to Note
- It is recommended that to improve the query performance of context filters for large datasets, following best practices may be followed.
- Use a single context filter instead of multiple context filters to reduce data size.
- Apply data modeling before defining the context filter.
- Add the fields after creating and setting up the context.
- Use continuous date to set a context filter on a date.
- Using the Assume Referential Integrity option in the data source’s data menu can significantly impact query performance.
Frequently Asked Questions (FAQs)
Yes, you can remove or modify the context filters in Tableau.
To remove the context filters, right-click on a context filter and choose Remove from Context. Repeat this step for all the remaining context filters you want to remove.
Similarly to modify the context filters, remove the existing context filters and create a new one by right-clicking the field and choosing Add to Context. If there are multiple context filters, then a new context is calculated when you remove a context filter.
Whether the context filters in Tableau affect all worksheets in a dashboard or not, depends on the choice of selection. If you choose to apply the context filter to all worksheets, then they will affect the other worksheets as well.
Yes, you can create multiple context filters in Tableau. To create a context filter, right-click on the field and choose Add to Context. You can repeat this step for different fields to create multiple context filters in Tableau.
Yes, you can use of context filter in Tableau in combination with table calculations or level of detail (LOD) expressions. In this case, Tableau will follow the order of operations where context filters shall be executed first followed by Table calculations or level of details expressions.
Download Template
This article must help understand the Context Filter in Tableau formula and examples. You can download the template here to use it instantly.
Recommended Articles
This has been a guide to Context Filter in Tableau. Here we learn how to create context filter with filters, types, examples, advantages and limitations. You can learn more from the following articles –
Leave a Reply