Power Query Text.Start Function: Guide and Examples
In this article
The Text.Start function in Power Query is used to extract a specific number of characters from the beginning of a text string. It is the Power Query equivalent of the LEFT function in Excel. To use it, you provide the text column or string as the first argument and the number of characters you wish to retrieve as the second argument. This is essential for cleaning data where codes, prefixes, or IDs are stored at the start of a cell and need to be isolated for analysis.
What You'll Need
- Microsoft Excel (with Power Query installed) or Power BI Desktop.
- A dataset containing a text column that requires character extraction.
- Access to the Power Query Editor window.
Step-by-Step
- Open Power Query Editor: In Excel, go to the Data tab and select Get Data or launch an existing query. In Power BI, click Transform Data on the Home ribbon.
- Select Your Column: Locate the column containing the text you want to truncate. Ensure the data type is set to Text. If the column is set to Number, the
Text.Startfunction will return an error because it only operates on text strings. To change this, right-click the column header, select Change Type, and choose Text. - Create a Custom Column: Navigate to the Add Column tab on the top ribbon. Click on Custom Column. This opens the Custom Column dialog box where you can write your formula.
- Enter the Formula: In the Custom column formula box, start by typing
= Text.Start([ColumnName], n). Replace[ColumnName]by double-clicking the desired column from the list on the right, and replacenwith the number of characters you want to extract. For example, to get the first 3 characters of a column named "ProductCode", the formula would be= Text.Start([ProductCode], 3). - Verify the Preview: Power Query will show a preview of the result at the bottom of the dialog box. Ensure that the characters being extracted are correct. If you see "Error", check that your column data type is indeed Text.
- Name the Column: In the New column name field at the top of the dialog, give your new column a descriptive name, such as "Prefix" or "Area Code".
- Confirm and Apply: Click OK. The new column will now appear at the end of your table. You can now right-click the original column and select Remove if it is no longer needed, or simply keep both for verification.
- Finalize the Query: Once satisfied, go to the Home tab and click Close & Apply to load the transformed data back into your Excel sheet or Power BI model.
Common Mistakes
One of the most frequent errors is attempting to use Text.Start on a column that is formatted as a Whole Number or Decimal. Power Query is strictly typed; unlike Excel, it will not automatically convert a number to text to perform a string operation. If your column contains numeric IDs that you want to treat as text, you must explicitly change the type to Text before adding the custom column.
Another common mistake is case sensitivity. While the function Text.Start is case-sensitive in terms of how it is written (you cannot write text.start or TEXT.START), the actual data it processes is handled as a string. However, users often confuse Text.Start with functions that search for a delimiter. Text.Start only takes a fixed number of characters. If your strings have varying lengths before a separator (like a hyphen or space), using a fixed number in Text.Start will result in inconsistent data. In those cases, Text.BeforeDelimiter is the correct choice.
Users also occasionally forget to handle null values. If a cell in the source column is null, Text.Start will return a null value. While this doesn't usually crash the query, it can cause issues in downstream calculations or merges if you expect a blank string instead of a null.
Tips
To make your queries more dynamic, you can combine Text.Start with other functions. For example, if you need to extract characters up to a certain point but that point varies, you can use Text.PositionOf to find the index of a specific character and use that result as the second argument in Text.Start. This allows the function to adapt to the length of the data in each row.
If you find yourself writing many custom columns, consider using the "Column From Examples" feature. By typing a few examples of the first few characters you want, Power Query will often automatically generate the Text.Start (or a similar) formula for you in the background, saving you from typing the syntax manually.
For better performance in very large datasets, perform your text transformations as early as possible in the query steps. This ensures that subsequent steps, such as filtering or grouping, are working with the refined data rather than the bulky original strings.
Troubleshooting
Go back to the step immediately preceding your custom column and ensure the column is set to Text. You can do this by clicking the icon to the left of the column header and selecting Text.
If the result is an empty string or doesn't seem to capture the right characters, check for leading spaces in your data. A cell that looks like it starts with "A" might actually start with a space (" A"). In this case, Text.Start([Column], 1) would return a space. To fix this, apply a "Trim" transformation to the column (Transform tab > Format > Trim) before applying the Text.Start function.
If you receive a syntax error, double-check your commas and brackets. The formula must follow the exact pattern Text.Start(text, count). Ensure the column name is enclosed in square brackets [] and that there is a comma separating the column reference from the number of characters.
Frequently Asked Questions
Can Text.Start handle negative numbers?
No, the second argument (the count) must be a non-negative integer. If you provide a negative number, Power Query will return an error. If you need to extract characters from the end of a string, use the Text.End function instead.
What happens if the requested number of characters is longer than the actual text?
If you ask for 10 characters but the cell only contains 5, Power Query will simply return all 5 characters. It will not add padding or throw an error; it just returns as much of the string as is available starting from the left.
Is there a way to use Text.Start without creating a new column?
Yes. Instead of using "Add Column," you can select the column and go to the Transform tab. While there isn't a direct "Start" button, you can select Extract > First Characters. This performs the Text.Start operation on the existing column, replacing the original values rather than creating a new column.
Does Text.Start work with dates?
Not directly. Dates are stored as a specific date type, not text. To use Text.Start on a date, you must first convert the date to a string using Date.ToText or by changing the column type to Text. However, it is usually better to use Date.Year or Date.Month to extract date parts.
Can I use a variable instead of a hard-coded number for the character count?
Yes. You can reference another column in the table as the second argument. For example, Text.Start([Description], [LengthColumn]) will extract a different number of characters for each row based on the value in the "LengthColumn".