How to Use CONTAINS in Tableau Calculated Fields
Explore the power of the CONTAINS function in Tableau to identify and segment data based on text patterns. This guide includes an interactive calculator, detailed explanations, and practical examples.
Tableau CONTAINS() Calculator
This calculator helps visualize how the CONTAINS function works by checking if a specific substring exists within a larger string.
What is the CONTAINS Function in Tableau?
The CONTAINS function in Tableau is a powerful string manipulation tool that allows you to determine if a specific text string (substring) exists within another text string (main string). It’s a boolean function, meaning it returns either TRUE or FALSE. This capability is fundamental for filtering, categorizing, and segmenting your data based on textual content within your fields. Whether you’re analyzing product descriptions, customer feedback, geographical data, or any other text-based information, CONTAINS provides a direct way to identify records that meet specific text criteria.
Who should use it?
Data analysts, business intelligence professionals, and anyone working with Tableau who needs to perform text-based analysis or create calculated fields for conditional logic. This includes users who need to:
- Filter datasets to include only records containing specific keywords (e.g., product names, categories, locations).
- Create flags or indicators for certain text patterns (e.g., identify reviews mentioning “urgent”, flag orders from specific regions).
- Build more complex calculations based on the presence or absence of text.
- Clean and prepare text data for further analysis.
Common Misconceptions:
A frequent misunderstanding is confusing CONTAINS with similar string functions like STARTSWITH, ENDSWITH, or FIND. CONTAINS checks for the substring anywhere within the main string, regardless of position. Another misconception is assuming it’s case-sensitive by default; while it can be, the default behavior is case-insensitive, which is often desired but important to be aware of. It’s also sometimes mistakenly thought to return the position of the string, which is the role of the FIND function.
CONTAINS Function Formula and Mathematical Explanation
The CONTAINS function in Tableau is defined as follows:
CONTAINS(string1, string2, [compare_option])
Breakdown:
string1: This is the primary string in which you are searching. In Tableau, this typically refers to a field containing text data (e.g., `[Product Name]`, `[Customer Segment]`).string2: This is the substring you are looking for withinstring1. This can be a literal string (e.g., `’Electronics’`) or another field.[compare_option](Optional): This parameter specifies whether the comparison should be case-sensitive. It accepts two values:'Case Sensitive'orTRUE: The search will distinguish between uppercase and lowercase letters.'Case Insensitive'orFALSE(Default): The search will ignore case differences.
If this parameter is omitted, Tableau defaults to case-insensitive matching.
How it works:
The function iterates through string1, attempting to find an exact match for string2, respecting the compare_option. If any occurrence of string2 is found within string1, the function returns TRUE. If no match is found after checking the entire string1, it returns FALSE.
Mathematical Analogy:
Conceptually, you can think of this like set theory or pattern matching. Let S1 be the set of characters in string1 and S2 be the set of characters in string2. The function checks if the sequence of characters in S2 appears as a contiguous subsequence within S1. The `compare_option` acts as a modifier, similar to how different equality checks (e.g., strict equality vs. case-insensitive equality) operate in programming.
Variables Table
| Variable | Meaning | Unit | Typical Range |
|---|---|---|---|
string1 |
The main text field or literal string to search within. | Text / String | Any valid text string. Length varies. |
string2 |
The substring to search for inside string1. |
Text / String | Any valid text string. Length varies. |
compare_option |
Specifies case sensitivity for the search. | Boolean (TRUE/FALSE) or Literal (‘Case Sensitive’/’Case Insensitive’) | TRUE, FALSE, ‘Case Sensitive’, ‘Case Insensitive’ (Optional) |
| Result | Boolean output indicating if string2 is found in string1. |
Boolean | TRUE or FALSE |
Practical Examples (Real-World Use Cases)
Example 1: Filtering Product Categories
Scenario: You have a dataset of products, and you want to identify all products that belong to the ‘Electronics’ category, but the category names are inconsistent (e.g., ‘Consumer Electronics’, ‘Home Electronics’, ‘Electronics Gadgets’).
Tableau Data:
A field named [Product Category] contains values like:
- ‘Consumer Electronics’
- ‘Apparel’
- ‘Home Appliances’
- ‘Electronics Gadgets’
- ‘Office Supplies’
Calculated Field:
You create a calculated field named `Is Electronics Category`:
CONTAINS([Product Category], 'Electronics')
Explanation: This calculation checks if the string ‘Electronics’ appears anywhere within the [Product Category] field. Since the default is case-insensitive, it will match ‘Electronics’ in all variations.
Resulting Values:
- ‘Consumer Electronics’ -> TRUE
- ‘Apparel’ -> FALSE
- ‘Home Appliances’ -> FALSE
- ‘Electronics Gadgets’ -> TRUE
- ‘Office Supplies’ -> FALSE
Interpretation: You can now use the `Is Electronics Category` field (which contains TRUE/FALSE values) as a filter in Tableau to isolate only the electronic products, regardless of the specific naming convention. This simplifies analysis of the electronics segment.
Example 2: Identifying Customer Feedback Keywords
Scenario: You are analyzing customer support tickets or feedback comments, and you want to identify feedback that specifically mentions “refund” or “return”, regardless of capitalization.
Tableau Data:
A field named [Feedback Text] contains customer comments.
Calculated Field:
Create a field called `Mentions Refund/Return`:
CONTAINS([Feedback Text], 'refund') OR CONTAINS([Feedback Text], 'return')
Explanation: This uses two CONTAINS functions combined with the OR operator. It returns TRUE if the text contains ‘refund’ OR if it contains ‘return’. The search is case-insensitive by default.
Sample Feedback Texts & Results:
- ‘The item arrived damaged, I need a refund.’ -> TRUE
- ‘Please process my return for the wrong size.’ -> TRUE
- ‘Excellent service, no issues.’ -> FALSE
- ‘I am looking to RETURN the sweater.’ -> TRUE (Case-insensitive match)
- ‘What is the REFUND policy?’ -> TRUE
Interpretation: This calculated field allows you to easily filter and analyze customer feedback related to refunds or returns. You could then count how many tickets mention these terms, track trends, or analyze the sentiment associated with them.
Tableau CONTAINS() Data Visualization
This chart illustrates how many times a substring might appear across different main strings (simulated).
How to Use This CONTAINS Calculator
- Enter Main String: In the “Main String” field, type or paste the text you want to search within. This could be a sample of data from a Tableau field like a product name or description.
- Enter Substring: In the “Substring to Find” field, enter the specific text you are looking for inside the main string.
- Select Case Sensitivity: Choose “No” for a case-insensitive search (default, e.g., ‘apple’ matches ‘Apple’) or “Yes” for a case-sensitive search (e.g., ‘apple’ does NOT match ‘Apple’).
- Click ‘Check CONTAINS’: The calculator will process your inputs.
How to Read Results:
- Primary Result (TRUE/FALSE): This directly shows whether the Substring was found within the Main String based on your selected case sensitivity.
- Found Count: In this simplified calculator, this indicates if the substring was found (1 if TRUE, 0 if FALSE). In real Tableau usage, you’d often use this in conjunction with `SUM(IF CONTAINS(…) THEN 1 ELSE 0 END)` to count occurrences across many rows.
- Length (Main) / Length (Sub): These display the character counts of your input strings, which can be useful for context.
- Formula Explanation: Provides a clear description of the CONTAINS function’s syntax and purpose.
Decision-Making Guidance: Use the TRUE/FALSE output to understand if your specific text pattern exists. In Tableau, a TRUE result means you can use this calculation in filters to keep or exclude rows, create conditional formatting, or build more complex analytical logic. For instance, if `CONTAINS([Customer Name], ‘Ltd.’)` returns TRUE, you know that record likely represents a business customer.
Key Factors That Affect CONTAINS Results
While the CONTAINS function in Tableau is straightforward, several factors can influence its outcome and how you interpret the results:
- Case Sensitivity: As discussed, this is the most critical factor. If you need to find ‘Apple’ but your data has ‘apple’, a case-sensitive search will fail. Always consider whether case matters for your specific analysis. Using the default (case-insensitive) is often safer for general text matching.
- Exact Substring Match: CONTAINS looks for the exact substring. If you search for ‘apple’ and have ‘applesauce’, it returns TRUE. However, if you search for ‘apple’ and have ‘pineapple’, it also returns TRUE. It doesn’t care about word boundaries unless you specifically craft logic for it (e.g., using spaces around the substring).
- Leading/Trailing Spaces: Spaces in your main string or substring can cause unexpected results. If your Main String is `’ Product A ‘` and you search for `’Product A’`, it will return FALSE because of the spaces. Data cleaning (using TRIM or REGEXP_REPLACE in Tableau) is often necessary before applying CONTAINS.
- Typos and Spelling Variations: CONTAINS requires an exact match. If your data has ‘colour’ and you search for ‘color’ (or vice versa), the default case-insensitive search won’t help. For handling minor spelling variations, more advanced functions like REGEXP_MATCH or fuzzy matching techniques might be needed.
- Empty Strings: Searching for an empty string (`”`) within any string (including another empty string) typically returns TRUE. Searching within an empty string for a non-empty substring will always return FALSE. Be mindful of empty fields in your data.
-
Null Values: If either
string1orstring2is NULL, theCONTAINSfunction generally returns NULL in Tableau. You might need to handle NULLs explicitly using `ZN()` or `ISNULL()` checks depending on your desired outcome. - Performance on Large Datasets: While powerful, applying complex string functions like CONTAINS across millions of rows can impact dashboard performance. Consider optimizing your data source, using extracts, or pre-processing data where possible. Using CONTAINS on a dimension field is generally more performant than on a row-level calculation applied across huge numbers of rows without aggregation.
Frequently Asked Questions (FAQ)
CONTAINS checks if the substring exists anywhere within the main string. STARTSWITH checks if the main string begins with the specified substring. For example, `CONTAINS(‘Pineapple’, ‘apple’)` is TRUE, but `STARTSWITH(‘Pineapple’, ‘apple’)` is FALSE.
Not directly in a single function call. You need to use the OR operator to combine multiple CONTAINS checks, like: CONTAINS([Text], 'term1') OR CONTAINS([Text], 'term2').
You need to explicitly specify the third parameter as ‘Case Sensitive’ or TRUE: CONTAINS([Field], 'SearchTerm', 'Case Sensitive') or CONTAINS([Field], 'SearchTerm', TRUE).
If either the main string or the substring is NULL, the CONTAINS function will return NULL. You should wrap your calculation in `IF ISNULL(CONTAINS(…)) THEN FALSE ELSE CONTAINS(…) END` or use `ZN()` if you want to treat NULL results as FALSE.
You can create a calculated field like: SUM(IF CONTAINS([Product Name], 'Chair') THEN 1 ELSE 0 END). This will count how many products contain the word ‘Chair’.
No, CONTAINS is strictly a string function. If you need to check for a numeric pattern within a number, you must first convert the number to a string using STR(), e.g., CONTAINS(STR([Order ID]), '123').
CONTAINS returns TRUE or FALSE. FIND returns the starting position (index) of the substring if found, or 0 if not found. You can use FIND to build conditional logic too, e.g., FIND([Text], 'Error') > 0 is equivalent to CONTAINS([Text], 'Error').
Use the NOT operator: NOT CONTAINS([Product Category], 'Electronics'). This calculated field will return TRUE for any category that does NOT contain the word ‘Electronics’.
Related Tools and Internal Resources
- Tableau CONTAINS() Calculator An interactive tool to test the CONTAINS function.
- Tableau Official Function Reference Access the complete list of Tableau’s calculation functions.
- Product Category Filtering Example See how CONTAINS simplifies data segmentation.
- Customer Feedback Analysis Example Learn to identify specific issues in text data.
- Guide to Tableau String Functions Explore other useful text manipulation functions.
- Advanced Tableau Filtering Techniques Discover methods beyond basic filters.
- Tableau Calculated Fields Basics Get started with creating your own calculations.