Power Query Text.Contains: Step-by-Step Guide
In this article
Text.Contains is a Power Query M function that checks whether a text value contains a given substring and returns true or false. You use it to filter rows, add conditional columns, or flag data that matches a keyword. It is case-sensitive by default, so "Apple" does not match "apple" unless you add a comparer argument. No add-ins are required; Power Query is built into Excel and Power BI, and the same function works in both.
What You'll Need
- Excel (Microsoft 365 or Excel 2019 and later) or Power BI Desktop with the Power Query Editor available.
- A table of data loaded into Power Query, or a blank query you can type M code into.
- Basic familiarity with the Power Query Editor ribbon and the formula bar.
Step-by-Step
- Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI, use Home > Transform data to open the Power Query Editor. Menu labels can differ slightly by version and platform, but the core commands are the same.
- Decide where you want the result. You can filter existing rows, add a custom column, or replace a column. Filtering is the most common use.
- Filter a column with Text.Contains. Click the filter arrow on the column header, choose Text Filters > Contains, type your search text, and click OK. Power Query writes a Text.Contains step for you. This is the quickest way to see the function in action.
- Add a custom column for a true/false flag. Go to Add Column > Custom Column. Name the column, for example ContainsKeyword, and enter a formula such as
Text.Contains([ProductName], "pro"). Click OK. Each row now returns true or false. - Make it case-insensitive. Add the comparer as a third argument:
Text.Contains([ProductName], "pro", Comparer.OrdinalIgnoreCase). Use Comparer.Ordinal for a strict, case-sensitive match. Without a comparer, the default is case-sensitive. - Search for multiple keywords. Combine calls with
or:Text.Contains([ProductName], "pro") or Text.Contains([ProductName], "lite"). For a list of terms, useList.AnyTrue(List.Transform({"pro","lite","max"}, each Text.Contains([ProductName], _))). - Handle nulls and errors. If the column can contain null, wrap the call:
try Text.Contains([ProductName], "pro") otherwise false. Alternatively, filter out nulls first with a null filter on the column. - Check the result and load. Review the preview, then choose Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to send the data back to your worksheet or model.
Common Mistakes
- Forgetting case sensitivity. The default is case-sensitive, so "Pro" will not match "pro". Add Comparer.OrdinalIgnoreCase when case should not matter.
- Passing a column instead of text. The second argument must be a text value, not a column reference. Use
Text.Contains([Column1], [Column2])only if Column2 is also text; otherwise you get an error. - Ignoring nulls. Text.Contains returns null when the input is null, which can break downstream filters. Use
try ... otherwise falseor filter nulls first. - Assuming wildcards work. Text.Contains looks for a literal substring. Characters like * and ? are not wildcards here; use Text.StartsWith or Text.EndsWith for position-based checks, or build a list of terms for multiple patterns.
- Using the wrong comparer. Comparer.OrdinalIgnoreCase is culture-aware in some contexts; for strict binary comparison use Comparer.Ordinal. Pick one and stay consistent.
Tips
- Use the formula bar to edit the generated step directly; it is often faster than re-running the filter dialog.
- Name your steps clearly in the Applied Steps pane so you can trace where Text.Contains was added.
- For large tables, filter early so Text.Contains runs on fewer rows.
- Test with a small sample query before applying to the full dataset.
- Remember that Text.Contains works on text; convert numbers or dates with Text.From first if needed.
Troubleshooting
If the filter returns no rows, check case sensitivity first, then confirm the search text has no leading or trailing spaces. If you see an error like "We cannot convert the value null to type Text", add the try/otherwise pattern or filter nulls. If the custom column shows errors for some rows, inspect those rows for non-text values and convert them with Text.From. If the step is slow, reduce the table size before the Text.Contains step. If the function name is not recognized, ensure you are in the Power Query Editor and not a regular Excel formula; Text.Contains is M, not a worksheet function.
Frequently Asked Questions
Is Text.Contains case-sensitive?
Yes, by default. Add Comparer.OrdinalIgnoreCase as the third argument to make it case-insensitive.
Can I search for multiple words at once?
Not directly. Combine calls with or, or use List.AnyTrue with List.Transform over a list of terms.
Does Text.Contains support wildcards?
No. It matches a literal substring. Use Text.StartsWith, Text.EndsWith, or a list of terms for pattern-like matching.
What happens if the column contains null?
Text.Contains returns null for null input. Wrap it in try ... otherwise false or filter nulls before the step.
Can I use Text.Contains in a calculated column in Power BI?
Yes, in Power Query (M). In DAX, use CONTAINSSTRING instead. The two are separate languages.
Why does my filter return nothing?
Check case, extra spaces, and whether the column is text type. Convert numbers or dates with Text.From first.