Power Query List.PositionOfAny Function Guide
In this article
The List.PositionOfAny function in Power Query returns the zero-based index of the first occurrence of any value from a provided list of candidates. For example, if you have a list of product IDs and you want to find where the first instance of either "ID_101" or "ID_105" appears, this function scans the list and returns the position of whichever one it hits first. If none of the specified values are found, the function returns -1.
What You'll Need
- Microsoft Excel (with Power Query/Get & Transform) or Power BI Desktop.
- A data source containing a list or a column that can be converted into a list.
- Access to the Advanced Editor or the Custom Column dialog box to enter M code.
Step-by-Step
- Prepare Your Primary List: Ensure you have a list of items to search through. In Power Query, a column in a table is not technically a list; it is a part of a table. To use
List.PositionOfAny, you must reference the column as a list. You do this by using the syntaxTableName[ColumnName]. - Define Your Search Criteria: Create a second list containing the values you are looking for. This can be a hard-coded list in the formula, such as
{"Red", "Blue", "Green"}, or a reference to another existing list in your query. - Open the Custom Column Dialog: In the Power Query Editor, go to the Add Column tab on the ribbon and select Custom Column. (Note: Labels may differ slightly depending on your version of Excel or Power BI).
- Enter the Function Syntax: In the Custom Column formula box, enter the function using the following structure:
List.PositionOfAny(list to search, list of values to find). For example, if your table is named "SalesData" and the column is "Status", and you want to find the first occurrence of "Cancelled" or "Returned", you would write:List.PositionOfAny(SalesData[Status], {"Cancelled", "Returned"}). - Handle the Zero-Based Index: Remember that Power Query lists are zero-based. This means the first item in the list is at position 0, the second at position 1, and so on. If the function returns 0, it means the match was found at the very start of the list.
- Validate the Result: Click OK. You will now see a new column containing numbers. A number indicates the position of the first match found; a -1 indicates that none of the values in your search list exist in the target list.
- Convert to Human-Readable Position (Optional): If you need the position to be 1-based (where the first item is 1), create another custom column or modify your formula to add 1 to the result:
List.PositionOfAny(...) + 1. Be careful to handle the -1 case first, otherwise, a non-match will be reported as position 0.
Common Mistakes
One of the most frequent errors is passing a Table instead of a List. List.PositionOfAny requires two list arguments. If you simply reference a table name without specifying the column in square brackets, the function will return an error because it cannot search a table object. Always ensure you are using TableName[ColumnName].
Another common mistake is ignoring case sensitivity. Power Query is case-sensitive by default. If your list contains "Apple" but your search criteria is "apple", List.PositionOfAny will not find a match and will return -1. To fix this, you must either ensure the casing matches exactly or transform both the source list and the search list to lowercase using List.Transform and Text.Lower before passing them into the function.
Users also often confuse List.PositionOfAny with List.PositionOf. While List.PositionOf looks for a single specific value, List.PositionOfAny is designed specifically for sets of values. Using the latter with a single value wrapped in curly braces {"Value"} works, but it is less efficient than using the single-value version of the function.
Tips
To make your queries more dynamic, avoid hard-coding the search list inside the formula. Instead, create a separate table in your data source (like an Excel table) that lists the "Keywords" or "Target Values". Load this table into Power Query and convert the column to a list. This allows you to update the values you are searching for without ever opening the Power Query Editor.
If you are searching for values based on a partial match (e.g., finding any cell that contains the word "Error" rather than exactly matching it), List.PositionOfAny will not work because it looks for exact matches. In those cases, you should use List.PositionOf in combination with List.Select or a custom filter function to identify the index of the first item that meets your criteria.
When working with very large datasets, be mindful that List.PositionOfAny must scan the list sequentially. If you find yourself using this function inside a custom column for every row of a million-row table, performance will degrade significantly. In such cases, consider if a Join or a Merge operation can achieve the same result more efficiently.
Troubleshooting
If the function returns -1 when you are certain the value exists, first check for trailing or leading spaces in your data. A value like "Completed " (with a space) will not match "Completed". Use the Transform > Trim feature on your columns before applying the function to remove invisible whitespace.
If you receive a "Expression.Error: We cannot convert the value... to type List" error, check your syntax. This almost always means you provided a Table, a Record, or a single Text value where a List was expected. Ensure both arguments are enclosed in curly braces {} if they are manual lists, or that you have correctly referenced a column as TableName[ColumnName].
If the result is not the position you expected, verify if the list is being sorted. Power Query processes data in the order it appears in the current step. If you have a sorting step applied to your table, List.PositionOfAny will return the position based on that sorted order, not the original order of the raw data source.
Frequently Asked Questions
Does List.PositionOfAny return all positions of the matches?
No, it only returns the index of the first occurrence it encounters. If you need every position where any of the target values appear, you will need to use a different approach, such as List.PositionsOf or a combination of List.Select and index columns.
Is there a way to make the search case-insensitive?
The function itself does not have a case-insensitive toggle. To achieve this, you must transform your source list and your search list to the same case (usually lowercase) using List.Transform(yourList, each Text.Lower(_)) before passing them into the function.
What happens if the search list is empty?
If the second argument (the list of values to find) is an empty list {}, the function will return -1 because no matches can be found from an empty set.
Can I use this function to find values across multiple columns?
Not directly. The function is designed for a single list. To search across multiple columns, you would first need to combine those columns into a single list (perhaps using List.Combine) or run the function separately for each column and then determine which result is the smallest (excluding -1).
How does the function handle null values?
If your search list contains null, the function will return the position of the first null it finds in the target list. If the target list contains null but your search list does not, the null values will simply be ignored during the scan.