How to Use CONTAINS in Tableau

Cody Schneider8 min read

Finding a specific word or phrase within your text data is a common but crucial task in data analysis. Tableau's CONTAINS function is the go-to tool for exactly this, allowing you to filter, group, or flag rows based on whether a text field includes a certain substring. This article walks you through the CONTAINS syntax, practical examples for filtering and new calculations, and how to handle common scenarios like case sensitivity.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

What is the CONTAINS Function?

The CONTAINS() function in Tableau is a logical function that checks if a specified string of text (a "substring") exists anywhere within another string. It's like using "Ctrl-F" or "Cmd-F" to find text, but within a powerful Tableau calculation.

The result of the function is always a boolean value: it returns TRUE if the substring is found and FALSE if it is not. A key detail to remember is that the CONTAINS function is case-sensitive by default, but we'll cover how to work around that later.

CONTAINS Syntax

The syntax for the function is simple and straightforward:

CONTAINS([String], [Substring])

Let's break that down:

  • [String]: This is the field containing the text you want to search through. For instance, this could be a [Product Name], [Customer Feedback], or [Description] field.
  • [Substring]: This is the specific word, phrase, or character you're looking for. You can either type this directly into the formula inside quotation marks (e.g., "Office Chair") or reference another field from your data source.

How to Use CONTAINS in a Calculated Field

The most direct way to use CONTAINS is by creating a calculated field. This gives you a new, reusable field in your data pane that you can use for filtering, colors, or further calculations.

Let's use a common example with Tableau's Sample - Superstore data. Imagine our goal is to identify all products that have the word "Table" in their name.

1. Open the Calculated Field Editor

First, you need to create a new calculated field. You can do this by right-clicking anywhere in the Data pane (the left sidebar) and selecting "Create Calculated Field." Alternatively, you can go to the "Analysis" menu at the top and select "Create Calculated Field."

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

2. Name Your Calculation

Give your new field a descriptive name that tells you what it does. Something like "Is a Table?" or "Product Contains Table" is much clearer than the default "Calculation1." A good name makes your workbook easier to understand later.

3. Write the CONTAINS Formula

In the formula editor, enter the CONTAINS function. For our example, we want to search within the [Product Name] field for the substring "Table".

The formula would be:

CONTAINS([Product Name], "Table")

At the bottom of the editor, you should see a message saying "The calculation is valid." Click "OK."

4. Apply the Calculation to Your Viz

You now have a new dimension in your Data pane called "Is a Table?". This field will have a TRUE or FALSE value for every row in your data. To see it in action, you can drag [Product Name] onto the Rows shelf, and then drag your new "Is a Table?" calculation right next to it.

You'll immediately see which products returned TRUE (because their name includes "Table") and which returned FALSE.

Practical Use Cases for the CONTAINS Function

Now that you have a basic CONTAINS calculation, you can use it in several powerful ways to enhance your analysis.

Use Case 1: Filtering for Keywords

The most common application is to filter your dashboard or worksheet to show only data that matches your criteria. Building on our example, let's say you want a bar chart showing Sales, but only for products that are tables.

  1. Create your CONTAINS calculated field as described above ("Is a Table?").
  2. Drag this new field ("Is a Table?") directly onto the Filters card.
  3. A filter dialog box will appear. Check the box for "TRUE" and click "OK."

Your view will now be filtered to show only the data for products whose names contain the word "Table." Anything else will be excluded from the viz.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Use Case 2: Grouping Data with CONTAINS and IF Statements

Sometimes, a simple True/False isn't enough. You might want to create broader categories for your analysis. You can do this by nesting CONTAINS within an IF statement. For instance, maybe you want to classify products into three groups: "Chairs", "Tables", and "Other".

Create a new calculated field named "Product Group" with the following formula:

IF CONTAINS([Product Name], "Chair") THEN "Chairs" ELSEIF CONTAINS([Product Name], "Table") THEN "Tables" ELSE "Other" END

This formula checks each product name in order:

  • If it finds "Chair," it assigns that product to the "Chairs" group.
  • If it doesn't find "Chair" but finds "Table," it assigns it to the "Tables" group.
  • If it finds neither, it assigns the product to the "Other" group.

Now you have a powerful new dimension for your visualizations. You could, for example, build a pie chart showing Sales by this new "Product Group" to see how sales break down across your custom categories.

Use Case 3: Creating a Dynamic Parameter Search

You can give your users more control by allowing them to search for keywords themselves. To do this, you can combine the CONTAINS function with a parameter.

  1. Create a Parameter: Right-click in the Data pane and select "Create Parameter". Name it something like "Search Keyword," set the Data Type to "String," and in "Allowable values," select "All."
  2. Show the Parameter: Right-click on your new parameter in the Data pane and select "Show Parameter." This will add a search box to your view.
  3. Create a Calculated Field: Create a new calculated field that links the parameter to the CONTAINS function. Name it "Search Filter."
  4. Filter the View: Drag this "Search Filter" to the Filters shelf and select TRUE, just like you did in the first use case.

Now, your worksheet will be filtered based on whatever text a user types into the "Search Keyword" parameter box. It's a simple way to add an interactive search bar to your dashboards.

Advanced Tips and Tricks

Here are a few ways to level up your CONTAINS expertise.

Dealing with Case Sensitivity: Using UPPER() or LOWER()

A frequent problem is dealing with text case. By default, CONTAINS([Product Name], "table") is not the same as CONTAINS([Product Name], "Table"). The search is case-sensitive.

To make your search case-insensitive, you can convert both the field and your search term to the same case using either the UPPER() or LOWER() function.

Simply wrap the string inside one of these functions:

CONTAINS(LOWER([Product Name]), "table")

This formula first converts the [Product Name] to all lowercase letters and then checks for the lowercase substring "table." This ensures you find every match, regardless of whether the original text was "Table," "table," or "TABLE." It is best practice to always write your substring in the corresponding case (e.g., all lowercase if using LOWER()).

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Searching for Multiple Substrings

What if you want to find products that are either a "Chair" OR a "Desk"? You can use the OR logical operator to combine multiple CONTAINS statements in one calculation.

CONTAINS([Product Name], "Chair") OR CONTAINS([Product Name], "Desk")

This calculation will return TRUE if a product name includes "Chair" or if it includes "Desk." You can link as many conditions as you need with OR.

Alternatives: STARTSWITH() and ENDSWITH()

While CONTAINS is great for finding text anywhere in a string, sometimes the position matters. Tableau offers two other useful functions for these cases:

  • STARTSWITH([String], [Substring]): Returns TRUE only if the string begins with the specified substring.
  • ENDSWITH([String], [Substring]): Returns TRUE only if the string ends with the specified substring.

These functions are just as easy to use and give you more specific control over your text analysis.

Final Thoughts

The CONTAINS function is one of the most practical and frequently used tools in any Tableau developer's toolset. It serves as a simple yet effective way to find specific keywords in your data, allowing you to quickly filter, categorize, and segment your information. By pairing it with other functions like IF, UPPER(), and logical operators like OR, you can solve a wide variety of analytical challenges.

We know that even with helpful functions, building reports in tools like Tableau can feel slow, especially when you have to repeatedly create new calculated fields to answer new questions. At Graphed , we handle this differently. Instead of having to write formulas and configure filters manually, you can simply ask for what you need in plain English. A query like, "Show me sales for all products containing the word 'table'" would instantly generate the chart you need, with no formula writing necessary. We connect directly to your data sources, providing live-updating answers and saving you the time you’d otherwise spend on report building.

Related Articles