Text.StartsWith Function in Power Query
In Power Query (M language), the Text.StartsWith function is used to determine whether a given text string starts with a specified substring.
Syntax
Text.StartsWith( text as nullable text, substring as text, optional comparer as nullable function ) as nullable logical
The function has the following parameters:
- text: The full text string we're testing.
- start: The substring to check for at the beginning of text.
- comparer: It is an optional parameter. It is used to specify case-sensitivity and culture (e.g.,
Comparer.OrdinalIgnoreCase
for case-insensitive comparison).
Returns:
- true if the text starts with the start value.
- false if it doesn't.
- null if text is null.
Example: Basic Case-Sensitive Check
Power Query M
let result = Text.StartsWith("PowerQuery", "Power") in result
The above code returns the result true.
Example: Case-Sensitive Mismatch
Power Query M
let Result = Text.StartsWith("PowerQuery", "power") in Result
The above code returns the result false.
Example: Case-Insensitive Check
Power Query M
let Result = Text.StartsWith("PowerQuery", "power", Comparer.OrdinalIgnoreCase) in Result
The above code returns the result true.
Example: Using in a Table Column (Add Column Example)
Power Query M
let // Example table Source = Table.FromRecords({ [Column1="PowerQuery"], [Column1="PQTools"], [Column1="QueryPower"] }), // Add column to check if Column1 starts with "PQ" AddedColumn = Table.AddColumn(Source, "StartsWithPQ", each Text.StartsWith([Column1], "PQ")) in AddedColumn
The output of the above code is shown below:
Column1 | StartsWithPQ |
---|---|
PowerQuery | false |
PQTools | true |
QueryPower | false |