Text.PositionOfAny returns the zero-based position of the first occurrence of any character from a given list inside a text value, or -1 if none are found. It is a Power Query M function, so you use it in the Power Query Editor (not in a worksheet cell). You need a text column or a text literal, and a list of characters to search for. The function is case-sensitive by default; add the optional third argument Comparer.OrdinalIgnoreCase to make it case-insensitive.
What You'll Need
- Power BI Desktop, Excel with Power Query, or another host that runs Power Query M (the steps below use Power BI Desktop and Excel; labels can differ slightly by version, device or region).
- A table loaded into Power Query, or a blank query where you can type M code directly.
- Basic familiarity with the Power Query Editor and the formula bar or Advanced Editor.
Step-by-Step
- Open the Power Query Editor. In Excel, select a cell in your table and go to Data > From Table/Range. In Power BI Desktop, use Home > Transform data. This opens the Power Query Editor window.
- Decide where the result should go. You can add a custom column, replace an existing column, or test the function in a blank query first. For a quick test, use Home > New Source > Blank Query, then open the Advanced Editor and replace the default code with a simple expression such as
= Text.PositionOfAny("Hello world", {"a","e","i","o","u"}). Close and apply to see the result. This returns 1 because the first vowel is "e" at zero-based position 1. - Add a custom column for real data. With your table selected, go to Add Column > Custom Column. In the formula box, write an expression that references your text column, for example . This returns the position of the first @, dot, or space in each row, or -1 if none appear.
- Use the correct list syntax. The second argument must be a list of single characters, written with curly braces and quoted strings:
{"a","b","c"}. A plain text string such as "abc" is not the same as a list and will cause an error. - Handle case sensitivity. By default the search is case-sensitive. To ignore case, add the comparer as the third argument: . Without it, searching for "a" will not match "A".
- Check the result type. The function returns a number (zero-based position) or -1. If you need a true/false flag instead, wrap it: . If you need the character itself, use Text.At with the returned position, but guard against -1 first.
- Apply and load. Select Close & Load (Excel) or Close & Apply (Power BI) to bring the new column into your model or worksheet.
Common Mistakes
- Passing a string instead of a list. The second argument must be a list, not a single text value. Use
{"x","y"}, not"xy". - Forgetting zero-based indexing. A result of 0 means the character is the first character, not the second. Many people expect 1-based positions.
- Assuming case-insensitive search. Without the comparer argument, "A" and "a" are different. Add Comparer.OrdinalIgnoreCase when case should not matter.
- Ignoring -1. If no character is found, the function returns -1. Using that value directly in Text.At or Text.Range will cause an error or unexpected output.
- Using it on non-text values. The first argument must be text. If the column contains numbers, convert them with Text.From first, or the function will fail.
Tips
- Use Text.PositionOfAny to find the first delimiter in a string, then Text.Range or Text.Split to extract the part before it.
- Combine with List.Transform to search for several characters at once, or with Text.Contains if you only need a yes/no answer.
- When testing, use a blank query and the Advanced Editor so you can see the raw result without adding a column.
- Remember that the function finds the first occurrence of any character in the list, not the first occurrence of the whole list as a sequence.
Troubleshooting
If you get an error saying the argument is not a list, check that the second argument uses curly braces and quoted characters. If the result is always -1, confirm the characters you are searching for actually appear in the text and that case matches, or add the comparer argument. If the function is not recognized, make sure you are in the Power Query Editor and not typing into a worksheet cell; M functions do not work in Excel formulas. If a column returns errors, check for null values: Text.PositionOfAny returns null when the first argument is null, so filter or replace nulls before applying the function. If the custom column step fails, verify the column name in square brackets matches exactly, including spaces.
Frequently Asked Questions
Is Text.PositionOfAny zero-based or one-based?
It is zero-based: the first character is at position 0. A result of -1 means no character from the list was found.
Can I search for a whole word instead of single characters?
No. The second argument is a list of characters, so each item is matched individually. To find a substring, use Text.PositionOf instead.
How do I make the search case-insensitive?
Add Comparer.OrdinalIgnoreCase as the third argument, for example .
What happens if the text is null?
The function returns null. Handle nulls with a conditional or by replacing them before calling the function.
Does it work in Excel Power Query and Power BI?
Yes, it is part of the standard M library available in both, as well as in other hosts that run Power Query.