Text.PositionOfAny

Text

Returns the position of the first character in a text value that matches any character in a given list.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Text.PositionOfAny(text as text, characters as list, optional occurrence as nullable number) as any

Parameters

NameTypeRequiredDescription
texttextYesThe text value to search within.
characterslistYesA list of single-character text values to search for.
occurrencenumberNoSpecifies which occurrence to find. Use Occurrence.First (default), Occurrence.Last, or Occurrence.All.

Return Value

anyThe zero-based position of the first matching character, -1 if none found, or a list of positions when Occurrence.All is used.

Remarks

Text.PositionOfAny searches text for any character that appears in the characters list and returns the position of the first such match. Returns -1 if no character from the list is found — not an error. Like Text.PositionOf, always check for -1 before using the result as an offset.

The characters parameter is a list of single-character text values. Each element is matched independently as a single character, not as a substring. This is the key difference from Text.PositionOf, which searches for a multi-character substring. Use Text.PositionOfAny when you want to find the first occurrence of any one of a set of characters — for example, the first digit, the first punctuation character, or the first whitespace.

The optional occurrence parameter accepts: - Occurrence.First (default) — position of the first matching character - Occurrence.Last — position of the last matching character - Occurrence.All — a list of all positions where any matching character appears

Note that Text.PositionOfAny does not accept a comparer parameter — the character matching is always case-sensitive. To perform case-insensitive matching, normalize the text with Text.Lower or Text.Upper before calling this function, and also lowercase or uppercase your characters list accordingly.

Examples

Example 1: Find the position of the first digit in a string

Text.PositionOfAny("E001", {"0","1","2","3","4","5","6","7","8","9"})
Result
Result
11

Example 2: Find the first separator character in a phone number

Text.PositionOfAny("(555) 123-4567", {"(",")"," ","-"})
Result
Result
10

Example 3: Get all positions of punctuation in a string

Text.PositionOfAny("alice.smith@example.com", {".", "@"}, Occurrence.All)
Result
Result
15,11,19

Example 4: No match returns -1

Text.PositionOfAny("AliceSmith", {"0","1","2","3","4","5","6","7","8","9"})
Result
Result
1-1

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks