Tableau CONTAINS() Function Calculator for String Matching
Understanding and effectively using the `CONTAINS()` function in Tableau is crucial for sophisticated text-based analysis. This calculator helps visualize its behavior and build your confidence.
CONTAINS() Function Visualizer
This is the main text field where you’ll look for a substring.
This is the smaller text you are searching for within the primary string.
Choose whether the search should match exact casing.
Result of CONTAINS()
The Tableau `CONTAINS(string, substring)` function checks if the ‘string’ contains the ‘substring’. It returns TRUE if the substring is found, and FALSE otherwise. The optional third argument (often implied as case-insensitive in default Tableau behavior unless specified otherwise) determines case sensitivity. This calculator simulates this check.
Intermediate Values:
CONTAINS() Behavior Visualization
What is the Tableau CONTAINS() Function?
The Tableau CONTAINS() function is a powerful string manipulation tool within Tableau’s calculated field capabilities. Its primary purpose is to determine if one string (a substring) exists within another, larger string (the primary string). This function is fundamental for filtering, segmenting, and analyzing textual data based on the presence or absence of specific keywords, patterns, or phrases. It’s a boolean function, meaning it returns either TRUE or FALSE, making it ideal for use in conditional logic within Tableau worksheets.
Who Should Use It: Data analysts, business intelligence professionals, and anyone working with datasets containing text fields in Tableau will find the CONTAINS() function invaluable. This includes scenarios like analyzing customer feedback, categorizing product descriptions, identifying specific log entries, or segmenting geographical data based on partial names.
Common Misunderstandings: A frequent point of confusion surrounds case sensitivity. By default, Tableau’s CONTAINS() function is often case-insensitive, but this behavior can be explicitly controlled. Another area of confusion is distinguishing it from similar functions like `FIND()` or `STARTSWITH()`. While `FIND()` returns the *position* of a substring, and `STARTSWITH()` checks if a string *begins* with a substring, `CONTAINS()` simply answers the yes/no question of whether the substring exists *anywhere* within the primary string.
{primary_keyword} Formula and Explanation
The core of the Tableau CONTAINS() function is its straightforward logic. It takes two primary arguments: the string to search within, and the substring to search for. An optional third argument can be used to enforce case sensitivity.
Tableau CONTAINS() Syntax:
CONTAINS(string, substring, [start], [occurrence])
While the syntax above shows optional arguments for `start` and `occurrence` in some contexts, the most common and fundamental usage in Tableau calculated fields is:
CONTAINS(string, substring)
Or for explicit case sensitivity:
CONTAINS(string, substring, true) (Case Sensitive)
CONTAINS(string, substring, false) (Case Insensitive – Default)
Variable Explanations:
| Variable | Meaning | Type | Default Behavior / Notes |
|---|---|---|---|
string |
The primary text field or string expression where the search is performed. | String | Can be a direct field name (e.g., `[Product Name]`) or a calculated string. |
substring |
The specific text pattern or string to look for within the primary string. | String | Can be a direct field name or a literal string (e.g., `”Electronics”`). |
true/false (Optional) |
Specifies whether the search should be case-sensitive (true) or case-insensitive (false). | Boolean | If omitted, Tableau typically defaults to case-insensitive matching. |
Practical Examples of Using CONTAINS() in Tableau
Let’s explore some real-world scenarios where the CONTAINS() function proves its utility:
Example 1: Filtering Products by Category Suffix
Imagine you have a dataset of products, and you want to identify all products that belong to the ‘Technology’ sub-category, which is consistently appended with ‘-Tech’ in the product name.
- Primary String Field: `[Product Name]`
- Substring to Find: `”-Tech”`
- Case Sensitivity: Case Insensitive (Default)
Tableau Calculated Field:
CONTAINS([Product Name], "-Tech")
Result: This calculation will return TRUE for products like “Smartphone -Tech”, “Laptop -Tech”, and FALSE for “Tablet”, “Smartwatch”.
Interpretation: Use this boolean result as a filter to include only those products ending with or containing ‘-Tech’.
Example 2: Identifying Specific Customer Regions
You have a customer dataset with a `[Customer Region]` field, and you want to find all customers located in regions containing the word “West”, regardless of whether it’s “West”, “Southwest”, or “Western”.
- Primary String Field: `[Customer Region]`
- Substring to Find: `”West”`
- Case Sensitivity: Case Insensitive (Default)
Tableau Calculated Field:
CONTAINS([Customer Region], "West")
Result: Returns TRUE for “Western Europe”, “West Coast”, “Southwest Asia”, and FALSE for “Central Africa”, “East Asia”.
Interpretation: This helps group customers from all “West”-related geographical areas for targeted marketing or analysis. Linking to Tableau CONTAINS() formula provides deeper context.
Example 3: Explicit Case-Sensitive Search for Usernames
Suppose you have a log file with usernames, and you need to specifically find entries made by the administrator ‘AdminUser’, differentiating it from ‘adminuser’.
- Primary String Field: `[Username]`
- Substring to Find: `”AdminUser”`
- Case Sensitivity: Case Sensitive
Tableau Calculated Field:
CONTAINS([Username], "AdminUser", true)
Result: Returns TRUE only if `[Username]` is exactly “AdminUser”. It would return FALSE for “adminuser” or “AdminUser_backup”.
Interpretation: Crucial for auditing or tracking actions by users with specific, case-sensitive identifiers. Explore other related Tableau functions for more options.
How to Use This Tableau CONTAINS() Calculator
This interactive calculator is designed to demystify the Tableau CONTAINS() function. Follow these simple steps:
- Enter the Primary String: In the “String to Search Within” field, type or paste the main text you want to analyze. This could be a Tableau field name like `[Product Name]` or any text string.
- Enter the Substring: In the “Substring to Find” field, enter the smaller piece of text you are looking for within the primary string. This could be a keyword, a part of a name, or a specific code.
- Select Case Sensitivity: Choose “Case Sensitive” if the exact capitalization matters for your search. Select “Case Insensitive (Default)” if ‘Apple’ should match ‘apple’.
- Calculate: Click the “Calculate CONTAINS()” button.
The calculator will immediately show you the boolean result (TRUE or FALSE) of the CONTAINS() operation, along with intermediate values like string lengths and the sensitivity setting used. The visualization chart will update to reflect the outcome.
How to Select Correct Units: For the CONTAINS() function, “units” refer to the interpretation of case sensitivity. Always consider whether your analysis requires exact matching (case-sensitive) or flexible matching (case-insensitive). The default in Tableau is typically case-insensitive, but explicitly setting it using the `true/false` parameter in your calculated field is best practice for clarity and predictability.
How to Interpret Results:
- TRUE: The substring was found within the primary string, respecting the chosen case sensitivity.
- FALSE: The substring was not found within the primary string.
This TRUE/FALSE output is invaluable for creating filters, conditional formatting, or further calculations in Tableau. For instance, you could use this result in an `IF` statement: `IF CONTAINS([Product Name], “Book”) THEN “Is Book” ELSE “Not Book” END`. This aligns with understanding the CONTAINS() formula deeply.
Key Factors That Affect CONTAINS() Results
While seemingly simple, several factors influence the outcome of the Tableau CONTAINS() function:
- Case Sensitivity Setting: This is the most direct factor. A case-sensitive search for “apple” will fail if the string contains only “Apple”. Conversely, a case-insensitive search might yield unintended matches if capitalization is critical.
- Exact Substring Match: `CONTAINS()` looks for the *exact* substring provided. If you search for “Color” and the text is “Colour”, it will return FALSE. You might need variations or more advanced functions like `REGEXP_MATCH()` for such cases.
- Whitespace Characters: Leading, trailing, or multiple internal spaces matter. Searching for ” New York” will not match “New York” unless the primary string has a leading space. Similarly, “New York” won’t match “New York” (double space). Consider using `TRIM()` or `REPLACE()` functions in Tableau beforehand if whitespace is inconsistent.
- Null Values: If either the primary string or the substring argument evaluates to NULL, the `CONTAINS()` function will typically return NULL, not TRUE or FALSE. Handle potential nulls using `ZN()` or `IFNULL()` functions.
- Data Type Consistency: Ensure both arguments are treated as strings. Tableau is usually good at implicit conversion, but in complex calculations, explicitly casting might be needed using `STR()`.
- Special Characters: The presence of symbols, punctuation, or non-standard characters in either string can affect the match. Understanding your data’s character set is important. For complex pattern matching involving special characters, `REGEXP_MATCH()` is often a better choice.
Frequently Asked Questions (FAQ) about Tableau CONTAINS()
Q: Is Tableau’s CONTAINS() function case-sensitive by default?
Typically, no. Tableau’s `CONTAINS()` function defaults to case-insensitive matching unless you explicitly specify `true` as the third argument.
Q: How is CONTAINS() different from FIND()?
CONTAINS() returns TRUE or FALSE based on whether the substring exists. FIND() returns the starting position (a number) of the substring within the string, or 0 if not found.
Q: Can I use CONTAINS() with numbers?
Yes, but both the primary string and substring must be treated as text (strings). You may need to convert numbers to strings using `STR()` before using `CONTAINS()`.
Q: What happens if the primary string is very long? Does performance suffer?
For extremely large datasets and very long strings, performance can be impacted. However, `CONTAINS()` is generally optimized. For critical performance needs, consider pre-processing data or using more efficient filtering methods if possible.
Q: How do I find strings that DO NOT contain a certain word?
You can use the `NOT` operator in Tableau: NOT CONTAINS([FieldName], "Keyword"). This will return TRUE when the keyword is absent.
Q: What if I need to match multiple possible substrings?
You can chain `OR` conditions with `CONTAINS()`: CONTAINS([FieldName], "Apple") OR CONTAINS([FieldName], "Banana"). For more complex scenarios, `REGEXP_MATCH()` is powerful.
Q: Can CONTAINS() handle partial matches like “Techno” matching “Technology”?
No, `CONTAINS()` requires an exact match of the substring. “Techno” would not match “Technology”. Use functions like `STARTSWITH()` if the match needs to be at the beginning, or `REGEXP_MATCH()` for pattern-based matching.
Q: My CONTAINS() calculation returns NULL. Why?
This usually happens if either the primary string field or the substring you are searching for contains a NULL value in the data. Use `IFNULL()` or `ZN()` to handle these cases before applying `CONTAINS()`.
Related Tools and Internal Resources
Enhance your Tableau data analysis toolkit by exploring these related functions and resources:
- Tableau FIND() Function Explained: Learn how to locate the position of a substring.
- Tableau STARTSWITH() Guide: Discover how to check if a string begins with a specific pattern.
- Tableau REGEXP_MATCH() Calculator: Master advanced pattern matching with regular expressions.
- Tableau LEFT() and RIGHT() Functions: Extracting characters from the beginning or end of strings.
- Tableau LEN() Function Guide: Understand how to get the length of a string.
- Tableau REPLACE() Function Tutorial: Learn to substitute parts of a string.