Power Query Text.StartsWith Function Guide
In this article
The Text.StartsWith function in Power Query is used to determine if a specific string of text begins with a designated set of characters. It returns a logical value—either TRUE or FALSE—making it an essential tool for creating conditional columns, filtering rows, or grouping data based on prefixes. To use it, you simply provide the text you want to check and the prefix you are looking for within the Power Query Formula Bar or a Custom Column dialog.
What You'll Need
- Microsoft Excel (with Power Query installed) or Power BI Desktop.
- A dataset containing at least one column of text data (strings).
- Basic familiarity with the Power Query Editor interface.
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
- Select Your Table: In the Queries pane on the left, select the table containing the text column you wish to analyze.
- Add a Custom Column: Navigate to the Add Column tab on the top ribbon and select Custom Column. This opens the Custom Column dialog box where you can enter your M code.
- Name Your Column: In the New column name field, enter a descriptive name, such as "Starts With Prefix" or "Category Check."
- Enter the Formula: In the Custom column formula box, type the function using the following syntax:
Text.StartsWith([ColumnName], "Prefix"). Replace[ColumnName]by double-clicking the column name from the available columns list on the right, and replace"Prefix"with the actual text you are searching for, ensuring it is enclosed in double quotation marks. - Verify Case Sensitivity: By default, Power Query is case-sensitive. If you need the function to ignore whether letters are uppercase or lowercase, add a third optional argument:
Text.StartsWith([ColumnName], "Prefix", Comparer.OrdinalIgnoreCase). - Click OK: Once the formula is entered, click OK. A new column will appear in your table showing TRUE for every row that begins with your specified text and FALSE for those that do not.
- Filter the Results: If your goal was to isolate specific rows, click the filter dropdown arrow on the header of your new custom column and uncheck FALSE.
- Remove the Helper Column: If the custom column was only used for filtering, right-click the column header and select Remove to clean up your dataset before clicking Close & Apply.
Common Mistakes
One of the most frequent errors is forgetting that Power Query is strictly case-sensitive. If you search for "North" but the data contains "north", the function will return FALSE. This often leads users to believe the function is broken when it is actually performing exactly as programmed. Always verify the casing of your source data before writing the formula.
Another common mistake is omitting the double quotation marks around the prefix string. In the M language, text must be wrapped in quotes. Writing without quotes will cause Power Query to look for a variable or another column named "North," resulting in an "Expression.Error: The name 'North' wasn't recognized" message.
Users also frequently confuse Text.StartsWith with Text.Contains. While Text.StartsWith only looks at the very beginning of the string, Text.Contains looks for the sequence anywhere in the text. If your prefix appears in the middle of the cell, Text.StartsWith will return FALSE, which may be unexpected if you aren't mindful of the function's specific purpose.
Lastly, failing to handle null values can lead to errors. If a cell in your text column is empty (null), the function cannot perform a text operation on it and may return an error for that specific row. This can break subsequent steps in your data transformation pipeline if not addressed via a null-replacement step.
Tips
To make your workflow more dynamic, instead of hard-coding the prefix into the formula, you can refer to another column in your table. For example, Text.StartsWith([ProductCode], [CategoryPrefix]) allows the prefix to change row by row based on a secondary reference column.
For complex categorization, combine Text.StartsWith with an if...then...else statement. Instead of a TRUE/FALSE result, you can assign a meaningful label. For example: if Text.StartsWith([ID], "INV") then "Invoice" else if Text.StartsWith([ID], "PO") then "Purchase Order" else "Other". This transforms a technical check into a business-ready category column.
If you have a long list of possible prefixes, avoid nesting dozens of "if" statements. Instead, create a separate mapping table with two columns: "Prefix" and "Category." You can then use a custom function or a merge operation to check the start of the text against the entire list of prefixes in your mapping table.
To improve performance on very large datasets, perform your Text.StartsWith operations as early as possible in your query steps. Filtering out unnecessary rows early reduces the amount of data Power Query has to hold in memory for subsequent transformations, which can significantly speed up the refresh time.
Troubleshooting
If the function is returning FALSE when you expect TRUE, first check for leading spaces. A cell that looks like it starts with "Apple" but actually contains " Apple" (with a leading space) will fail the Text.StartsWith check. To fix this, right-click the column, select Transform, and then Trim. This removes all leading and trailing whitespace.
If you encounter an "Expression.Error" stating that the value cannot be converted to text, it means your column is not set to the Text data type. Power Query cannot perform text functions on numbers or dates. Go to the Transform tab or click the data type icon next to the column header and change the type to Text. Once the column is explicitly typed as text, the function will execute correctly.
If the formula returns an error for rows containing nulls, you can wrap the function in a try-otherwise block or use a conditional check. For example: if [ColumnName] <> null then Text.StartsWith([ColumnName], "Prefix") else false. This ensures that null values are treated as FALSE rather than triggering a system error.
If the results are inconsistent across different regions or devices, double-check the Comparer argument. Different systems may handle special characters or accents differently. Using Comparer.OrdinalIgnoreCase is generally the safest way to ensure consistency across different locales when case sensitivity is not a requirement.
Frequently Asked Questions
Can Text.StartsWith be used for numbers?
No, it only works on text data types. If you have a numeric column, you must first change the data type to Text using the Transform menu before applying the function.
Is there a way to check if text ends with a certain string?
Yes, Power Query provides a companion function called Text.EndsWith, which follows the exact same syntax and logic as Text.StartsWith but checks the end of the string instead.
Does the function support wildcards like asterisks?
No, Text.StartsWith does not support wildcards. It looks for an exact match of the characters provided. For wildcard-style searches, you may need to use Text.Contains or a more complex Regular Expression via a custom R or Python script.
How do I make the search case-insensitive?
Add Comparer.OrdinalIgnoreCase as the third argument in the function: Text.StartsWith([Column], "text", Comparer.OrdinalIgnoreCase).
Can I check for multiple different prefixes at once?
Not within a single Text.StartsWith call. You must either use multiple or operators (e.g., Text.StartsWith([Col], "A") or Text.StartsWith([Col], "B")) or use a custom list and a function like List.AnyTrue.
Will this function slow down my report?
On small to medium datasets, the impact is negligible. On millions of rows, adding many custom columns with text functions can increase refresh time. It is best to filter the data as early as possible in the query steps.