Power Query List.FindText: How to Search Lists
In this article
Power Query’s List.FindText function is a specialized tool designed to locate the first occurrence of a specific text string within a list of values. Unlike standard search functions that might require you to iterate through every item manually or use complex filtering, List.FindText provides a direct way to return the position (index) of the matching text. This is particularly useful when you need to identify where specific data points appear in a dataset before performing further transformations, such as splitting columns or validating entries against a known set of keywords.
What You'll Need
- Microsoft Excel or Power BI: Ensure you have a recent version installed, as Power Query features are updated regularly.
- A Data Source: A table or list containing text values that you want to search.
- Power Query Editor Access: The ability to open the Power Query Editor via the "Data" tab in Excel or the "Transform Data" option in Power BI.
- Basic M Language Knowledge: While not strictly required for simple usage, understanding how to edit formulas will help you customize the search parameters.
Step-by-Step
- Load Your Data into Power Query: Open your Excel workbook or Power BI report. Navigate to the Data tab and select Get Data (or From Table/Range if your data is already in a sheet). If you are using Power BI, click Transform Data on any loaded table to open the Power Query Editor.
- Identify the Column to Search: In the Power Query Editor, locate the column that contains the list of text values you wish to scan. For this example, let's assume the column is named "Description." Note that
List.FindTextoperates on lists, so if your data is in a table, you may need to convert the column values into a list or apply the function row-by-row depending on your goal. - Add a Custom Column: Go to the Add Column tab and click Custom Column. Name this new column something descriptive, like "MatchIndex." This is where the result of the search will be stored.
- Write the Formula: In the formula box, enter the following syntax:
=List.FindText(YourList, "SearchTerm"). ReplaceYourListwith the actual list variable. If you are searching within a single cell's text content converted to a list, you might first need to split the text. However, a more common scenario is searching across a column. To do this effectively in a custom column context, you often useList.PositionOfcombined withText.Contains, butList.FindTextspecifically looks for exact substring matches in a list structure. A typical usage involves converting a column to a list first:=let myList = List.Buffer(Table.Column(Source, "Description")) in List.FindText(myList, "Error"). - Handle Nulls and Errors: If the text is not found,
List.FindTextreturns null. Wrap your formula in anifstatement to handle this gracefully:=if List.FindText(myList, "Error") <> null then List.FindText(myList, "Error") else -1. This ensures you get a numeric index rather than a blank value. - Apply and Close: Click OK to add the column. Review the results to ensure the indices match your expectations. Finally, click Close & Load to bring the transformed data back into your worksheet or data model.
Common Mistakes
One frequent error is confusing List.FindText with Text.Find. Text.Find searches for a substring within a single text value, while List.FindText searches for a specific element within a list of elements. Using them interchangeably will lead to type errors. Another mistake is failing to buffer the list using List.Buffer when working with large datasets. Without buffering, Power Query may re-evaluate the list for every row, causing significant performance degradation. Additionally, users often forget that the function is case-sensitive by default unless specified otherwise in newer updates or via workarounds, leading to missed matches if the casing differs between the search term and the data.
Tips
For better performance, always define your search term as a variable at the top of your query if it is constant throughout the transformation. This allows Power Query to optimize the execution plan. If you need to find all occurrences rather than just the first, consider using List.Select with Text.Contains instead, as List.FindText only returns the index of the first match. When dealing with messy data, pre-clean your list using Text.Trim and Text.Clean before applying the search function to avoid false negatives caused by hidden whitespace or special characters. Lastly, remember that indices in Power Query start at zero, not one, which can be counterintuitive if you are used to Excel’s row numbering.
Troubleshooting
If you receive an error stating "Expression.Error: We cannot apply operator ... to types," check that your first argument is indeed a list and your second is a text value. Converting a table column to a list requires explicit steps like Table.Column(). If the function returns null unexpectedly, verify that the search term exists exactly as typed, including capitalization and spacing. Use the "View Native Query" feature (if enabled) to see the underlying SQL or M code being generated, which can help debug logic issues. If performance is slow, ensure you are not applying the function inside a loop unnecessarily; try to aggregate or filter data earlier in the process to reduce the size of the list being searched.
Frequently Asked Questions
Does List.FindText support regular expressions?
No, List.FindText performs literal string matching. It does not interpret regular expressions. If you need regex capabilities, you must use external tools or preprocess the data outside of Power Query.
Can I make the search case-insensitive?
The function itself is case-sensitive. To achieve case-insensitive searching, you should convert both the list items and the search term to lowercase using List.Transform and Text.Lower before applying List.FindText.
What happens if the text is not found?
The function returns null. It does not throw an error, which makes it safe to use in conditional statements without needing try/catch blocks for missing values.
Is there a limit to the list size?
There is no hard-coded limit, but performance degrades significantly with very large lists (millions of items). Always filter your data to relevant subsets before performing list operations to maintain speed.
How do I find multiple occurrences?
List.FindText only finds the first occurrence. To find all indices, you would typically use List.PositionOf with the Occurrence.All parameter, though this checks for equality rather than substring containment. For substring containment across all items, combine List.Select with Text.Contains.