How to Use CONTAINS in Tableau Calculated Field
Nothing stalls a data analysis faster than messy text fields. The Tableau CONTAINS function is your go-to tool for cutting through that noise, letting you search for specific keywords or phrases within your data. This article will walk you through exactly how to use CONTAINS in a calculated field, complete with practical examples for marketing, sales, and product analysis.
What Exactly is the CONTAINS Function in Tableau?
The CONTAINS function is a simple but powerful logical function in Tableau. Its job is to check if a specific sequence of characters (a "substring") exists anywhere inside a larger piece of text (a "string").
If it finds the substring, the function returns a TRUE. If it doesn't, it returns a FALSE.
Think of it like using "Ctrl + F" or "Cmd + F" to find a word on a webpage, but for every single row in your dataset. It’s perfect for situations like:
- Checking if a product name includes a specific keyword like "Organic" or "Sale".
- Finding all customer feedback comments that mention the word "shipping".
- Segmenting marketing campaigns by channel, like isolating all campaigns with "Facebook" in the name.
The function is not case-sensitive by default, which is usually helpful. This means CONTAINS("Hello World", "world") will return TRUE, even though the case doesn't match perfectly.
Understanding the CONTAINS Syntax
The syntax for the CONTAINS function is straightforward. It takes just two arguments:
CONTAINS(string, substring)
- string: This is the text field you want to search within. In Tableau, this will typically be a field from your data source, like
[Product Name]or[Customer Comment]. - substring: This is the specific text you're looking for. It must be enclosed in double quotes, like
"keyword".
Let's look at a simple example. Imagine you have a data source with a field called [Item Description].
If you create a calculated field with the formula:
CONTAINS([Item Description], "Shirt")
The calculated field will produce the following results for each row:
How to Create Your First CONTAINS Calculated Field
Creating the calculated field is a simple process. Let's walk through it step-by-step.
- Open the Calculation Editor: In your Tableau worksheet, go to the Data pane on the left. Right-click on a blank space within the pane and select Create Calculated Field.... Alternatively, you can go to the top menu and select Analysis > Create Calculated Field...
- Name Your Field: A dialog box will appear. The first thing you should do is give your new field a descriptive name. For our example, let's call it "Is a Shirt?"
- Write the Formula: In the large text box, type your CONTAINS formula. Using our example from before, you would type:
CONTAINS([Item Description], "Shirt")
As you type, Tableau will offer suggestions for functions and fields, which can help prevent typos. A small message at the bottom will say "The calculation is valid." if everything is correct.
- Save the Calculation: Click OK.
Your new calculated field, "Is a Shirt?", will now appear in the Data pane. You can drag it onto the Rows or Columns shelf, drop it on the Color mark, or use it as a Filter to start segmenting your data visualizations based on its TRUE/FALSE output.
Practical Examples of Using CONTAINS
The real power of CONTAINS comes when you use it to solve actual business problems. A simple TRUE/FALSE is a good start, but combining it with an IF statement turns it into a powerful categorization tool.
Example 1: Grouping Marketing Campaign Data
Imagine your company runs ad campaigns across multiple channels and your [Campaign Name] field looks like this: "Summer_Sale_23_Facebook", "Q4_Holiday_Google", "Summer_Brand_23_Instagram". Analyzing performance is messy. You can use CONTAINS to group them logically.
Create a calculated field called "Campaign Theme" with this formula:
IF CONTAINS([Campaign Name], "Summer") THEN "Summer Campaigns" ELSEIF CONTAINS([Campaign Name], "Holiday") THEN "Holiday Campaigns" ELSE "Other Campaigns" END
Now, instead of dozens of individual campaign names, you have three clean categories — "Summer Campaigns," "Holiday Campaigns," and "Other Campaigns" — that you can use to easily compare performance.
Example 2: Analyzing Open-Ended Survey Feedback
You have a field called [User Feedback] containing hundreds of comments. You want to quickly find all the feedback related to product pricing or costs.
Create a calculated field named "Mentions Pricing":
CONTAINS([User Feedback], "price") OR CONTAINS([User Feedback], "cost") OR CONTAINS([User Feedback], "expensive")
This formula returns TRUE if a comment contains any of those keywords. You can then drag this new field to the Filters card and select "True" to instantly see only the comments relevant to pricing.
Example 3: Segmenting Website Traffic by Page Type
Let's say your Google Analytics data has a [Page Path] dimension. You want to separate your blog traffic from the rest of your site traffic, and all your blog post URLs contain "/blog/".
Create a calculated field called "Page Type":
IF CONTAINS([Page Path], "/blog/") THEN "Blog" ELSEIF [Page Path] = "/" THEN "Homepage" ELSE "Other Pages" END
This allows you to build visualizations that compare key metrics like sessions, bounce rate, and conversions between your blog, homepage, and other site pages.
Advanced Techniques: Combining CONTAINS with other Functions
To take your analysis to the next level, you can nest the CONTAINS function inside other logical statements.
- With AND: Use this to find rows that meet multiple criteria. For example, to find all red shirts, you can use:
CONTAINS([Item Description], "Red") AND CONTAINS([Item Description], "Shirt")
This will only return TRUE if a single item description includes both "Red" and "Shirt".
- With OR: Use this to find rows that meet at least one of several criteria. To categorize social media campaigns, you could use:
IF CONTAINS([Campaign Name], "Facebook") OR CONTAINS([Campaign Name], "Instagram") THEN "Social" ELSE "Non-Social" END
This will label a campaign as "Social" if its name contains either "Facebook" or "Instagram".
- With NOT: Use this to exclude certain data. To find all campaigns that are not branded campaigns:
NOT CONTAINS([Campaign Name], "Brand")
This is extremely useful when you want to filter out a specific subset of data from your view.
Common Mistakes to Avoid
The CONTAINS function is generally forgiving, but a few common errors can trip you up.
- Forgetting Quotes: The substring you're searching for must be in double quotes.
CONTAINS([Field], Shirt)will result in an error, it must beCONTAINS([Field], "Shirt"). - Hidden Spaces: The function is literal.
" Tshirt"(with a leading space) is different from"Tshirt". If your calculation isn't working, copy and paste the substring directly from your data source to ensure there are no unwanted spaces. - When You Need Case-Sensitivity: By default, CONTAINS is not case-sensitive. In the rare case you need to differentiate between "apple" and "Apple", you should use the FIND() function instead. The formula
FIND([Field], "Apple") > 0would only return TRUE if it finds "Apple" with a capital 'A'.
Final Thoughts
The CONTAINS function is a fundamental tool for anyone working with text data in Tableau. It turns messy, inconsistent strings into clean, structured categories that you can use to filter, segment, and visualize your data, ultimately leading to clearer insights.
Mastering functions like this is a huge part of building powerful reports, but getting data from different sources, cleaning it, and creating all the necessary calculations takes time. That's why we built Graphed to automate the entire process. Instead of manually creating calculated fields, you can connect your data sources and simply ask in plain English, "Show me a chart of blog traffic vs. other site traffic," and the dashboard is created for you in seconds, automatically handling the necessary logic in the background.
Related Articles
What SEO Tools Work with Google Analytics?
Discover which SEO tools integrate seamlessly with Google Analytics to provide a comprehensive view of your site's performance. Optimize your SEO strategy now!
Looker Studio vs Metabase: Which BI Tool Actually Fits Your Team?
Looker Studio and Metabase both help you turn raw data into dashboards, but they take completely different approaches. This guide breaks down where each tool fits, what they are good at, and which one matches your actual workflow.
How to Create a Photo Album in Meta Business Suite
How to create a photo album in Meta Business Suite — step-by-step guide to organizing Facebook and Instagram photos into albums for your business page.