Text.Contains in Power Query: A Complete Guide
In this article
To check if a cell contains a specific piece of text in Power Query, use the Text.Contains function within a Custom Column. The basic syntax is Text.Contains([ColumnName], "search text"), which returns a TRUE or FALSE value based on whether the specified string is found within the target column. This is the primary method for filtering, categorizing, or flagging data based on partial text matches rather than exact matches.
What You'll Need
- Microsoft Excel or Power BI Desktop with Power Query (Get & Transform) enabled.
- A dataset loaded into the Power Query Editor.
- A column containing text data (String/Text data type) that you wish to analyze.
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
- Select Your Table: In the Queries pane on the left, select the table where you want to apply the text search.
- Add a Custom Column: Navigate to the Add Column tab on the top ribbon and click on Custom Column. This opens a dialog box where you can write your M code formula. (Note: Menu labels may vary slightly depending on your version of Excel or Power BI).
- Name Your Column: In the New column name field, enter a descriptive name, such as "Contains Keyword" or "Is Priority".
- Enter the Formula: In the Custom column formula box, type the following:
Text.Contains([YourColumnName], "your text"). Replace[YourColumnName]by double-clicking the column name from the available columns list on the right, and replace"your text"with the specific word or phrase you are looking for, ensuring it is enclosed in double quotation marks. - Verify the Result: Click OK. Power Query will create a new column filled with TRUE (if the text was found) and FALSE (if it was not).
- Change Data Type: Click the data type icon (usually "Any") next to the new column header and select Logical. This ensures the column is treated as a Boolean value for further filtering or reporting.
- Filter the Data (Optional): If you only want to keep rows that contain the text, click the filter arrow on the new column header and uncheck FALSE.
- Remove the Helper Column (Optional): If you only needed the column to filter the rows, right-click the header of the custom column and select Remove. The filter step remains in the Applied Steps list, keeping your final dataset clean.
Common Mistakes
The most frequent error when using Text.Contains is ignoring case sensitivity. By default, Power Query is case-sensitive. If you search for "Apple" but the data contains "apple", the function will return FALSE. To solve this, you must add a third argument to the function: Comparer.OrdinalIgnoreCase. The corrected formula looks like this: Text.Contains([ColumnName], "apple", Comparer.OrdinalIgnoreCase).
Another common mistake is attempting to use the function on a column that is not set to the Text data type. If your column is set to "Any" or "Number", the function may throw an error. Always ensure the source column is explicitly set to Text by clicking the type icon in the column header before adding the custom column.
Users also frequently forget the double quotation marks around the search string. In M code, any literal text must be wrapped in quotes. Writing without quotes will cause Power Query to look for a column or variable named "London" rather than the text string "London", resulting in a "Token Literal expected" error.
Lastly, some users try to search for multiple keywords within a single Text.Contains function. This function only accepts one search string. If you need to check for "Red" OR "Blue", you cannot put them in one string; you must use the or operator to combine two separate functions: Text.Contains([Color], "Red") or Text.Contains([Color], "Blue").
Tips
To make your search more dynamic, instead of hard-coding the search text into the formula, you can create a Parameter. Go to the Home tab, select Manage Parameters > New Parameter, and define your search term there. Then, in your custom column formula, use the parameter name: Text.Contains([ColumnName], ParameterName). This allows you to change the search term without editing the M code every time.
If you are dealing with very large datasets, consider filtering the data before adding custom columns. While adding a column is useful for visualization, if your goal is simply to remove rows, using the built-in filter interface (Text Filters > Contains) is often more performant because Power Query can sometimes "fold" this operation back to the original data source (like a SQL database), reducing the amount of data loaded into memory.
For complex pattern matching that goes beyond simple "contains" logic (such as finding any string that starts with a digit followed by three letters), Text.Contains is insufficient. In those cases, you may need to explore Text.StartsWith, Text.EndsWith, or use a custom R or Python script integration within Power Query for Regular Expressions (Regex).
When cleaning data, combine Text.Contains with Text.Trim. Often, hidden spaces at the beginning or end of a cell can cause issues with other functions, although Text.Contains is generally resilient to this since it looks for the string anywhere in the cell. However, trimming your data as a standard first step in your query pipeline prevents errors in subsequent steps like Merging or Grouping.
Troubleshooting
If you encounter an "Expression.Error: We cannot convert the value null to type Text", it means your column contains null values. Text.Contains cannot process a null. To fix this, you have two options. First, you can replace nulls by right-clicking the column and selecting Replace Values, replacing null with an empty string. Second, you can wrap your formula in an if statement: if [ColumnName] <> null then Text.Contains([ColumnName], "text") else false.
If the function is returning FALSE when you know the text exists, check for non-printing characters or different encoding. Sometimes data imported from the web contains non-breaking spaces (char 160) instead of standard spaces (char 32). To troubleshoot this, create a temporary column using Text.Length([ColumnName]) to see if the character count matches what you expect. If there are hidden characters, use the Transform > Replace Values tool to swap the invisible character with a standard space.
If the Custom Column dialog box shows a syntax error (red underline), check your brackets and commas. Every column reference must be in square brackets [] and every argument in the function must be separated by a comma. Ensure you are using standard straight quotes (") and not "curly" or "smart" quotes that may have been introduced if you copied the formula from a word processor.
If your query is running extremely slowly after adding several Text.Contains columns, check if you have "Buffer"ed your table. Using Table.Buffer on your source table can sometimes speed up repeated text searches by loading the data into memory once, rather than re-reading the source for every single row calculation.
Frequently Asked Questions
Can I use Text.Contains to find multiple different words?
Not within a single function call. You must use the or operator to link multiple Text.Contains statements, such as Text.Contains([Col], "A") or Text.Contains([Col], "B").
Is there a way to make Text.Contains case-insensitive?
Yes, add Comparer.OrdinalIgnoreCase as the third argument in the function: Text.Contains([Column], "text", Comparer.OrdinalIgnoreCase).
What is the difference between Text.Contains and the filter menu?
The filter menu (Text Filters > Contains) removes rows from the dataset. Text.Contains in a custom column creates a new piece of data (TRUE/FALSE) that you can use for calculations, conditional formatting, or grouping without losing the original rows.
Does Text.Contains work with numbers?
No, it only works with text. If your data is numeric, you must first convert the column to Text using Transform > Data Type > Text, or wrap the column in the formula using Text.From([NumberColumn]).
Can I use wildcards like * or ? with Text.Contains?
No, Text.Contains does not support wildcards. It looks for the exact sequence of characters provided. For wildcard-style searching, you would need to use more advanced M functions or a custom script.