Power Query Left Text: Extract Characters from the Start
In this article
To extract text from the left side of a cell in Power Query, select the column you want to modify, go to the Transform or Add Column tab, select Extract, and choose First Characters. You then enter the number of characters you wish to keep from the start of the string. This process allows you to isolate prefixes, area codes, or specific ID markers from a larger string of text without needing to write complex formulas manually.
What You'll Need
- Microsoft Excel (with Power Query installed) or Power BI Desktop.
- A dataset imported into the Power Query Editor.
- A column containing text strings from which you need to extract the leftmost characters.
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data or Launch Power Query Editor. In Power BI, click Transform Data from the Home ribbon.
- Select Your Target Column: Click on the header of the column that contains the text you want to trim. Ensure the data type is set to Text (indicated by the ABC icon next to the column header). If it is set to a number or date, the extraction tools may not appear or may behave unexpectedly.
- Choose Your Method (Transform vs. Add Column):
- If you want to overwrite the existing data in that column, navigate to the Transform tab.
- If you want to keep the original data and create a new column with the extracted text, navigate to the Add Column tab. (Note: Labels may differ slightly depending on whether you are using Excel for Windows, Mac, or Power BI).
- Access the Extract Tool: Look for the Text Column group in the ribbon. Click the Extract button. A dropdown menu will appear.
- Select First Characters: From the dropdown menu, click First Characters. This is the Power Query equivalent of the LEFT function in standard Excel.
- Define the Character Count: A dialog box will appear asking for the Count. Enter the exact number of characters you want to extract from the left side of each cell. For example, if your data looks like "USA-12345" and you only want the country code, enter
3. - Confirm and Apply: Click OK. Power Query will generate a new step in the Applied Steps pane on the right, and you will immediately see the results in your data preview.
- Finalize the Data: Once you are satisfied with the extraction, click Close & Apply (Power BI) or Close & Load (Excel) to bring the cleaned data back into your spreadsheet or model.
Common Mistakes
One of the most frequent errors is attempting to use the Extract tool on a column that is formatted as a Decimal Number or Whole Number. Power Query's text extraction tools are strictly for text data types. If you try to extract the first three digits of a number without first converting the column to text, the option may be greyed out or return an error. Always check the data type icon in the column header first.
Another common mistake is forgetting the difference between the Transform and Add Column tabs. Users often use the Transform tab, realizing too late that they have deleted the original full string which they actually needed for another part of their analysis. If this happens, you can delete the extraction step in the Applied Steps pane to restore the original data, but it is safer to use Add Column during the exploration phase.
Users also frequently confuse First Characters with Text Before Delimiter. If your strings have a variable length—for example, some are "NY-123" and some are "California-456"—using a fixed number of characters (like 2) will fail for the longer state names. In these cases, extracting by a delimiter (like the hyphen) is the correct approach rather than a fixed left-count.
Tips
For those who prefer writing code or need more flexibility, you can perform a "Left Text" operation using the Custom Column feature. By clicking Add Column > Custom Column, you can use the M language function Text.Start([ColumnName], count). This is the direct equivalent of the UI-based First Characters tool but allows you to use a dynamic count. For instance, if you have another column that specifies how many characters to extract for each row, you can reference that column name instead of a hard-coded number.
To keep your Power Query steps clean, rename your extraction step. Right-click the "Extracted First Characters" step in the Applied Steps pane and select Rename. Giving it a name like "Extract Country Code" makes it much easier for colleagues (or your future self) to understand the logic of the data transformation when auditing the query later.
If you find yourself performing the same left-text extraction across multiple tables, consider creating a Power Query Function. This allows you to wrap the Text.Start logic into a reusable tool that can be called across different queries, ensuring consistency in how your IDs or codes are handled across the entire project.
Troubleshooting
If the extraction results in null values, check your source data for empty cells. Power Query cannot extract characters from a null value. To fix this, right-click the column and select Replace Values to change null to an empty string or a placeholder before applying the extraction.
If you see an Expression.Error stating that the value cannot be converted to text, it means there is a data type mismatch. Ensure that the column is explicitly cast as Text by clicking the data type icon and selecting Text. If the error persists, check if there are hidden non-printable characters at the start of your strings; you may need to use Transform > Format > Trim to remove leading whitespace before extracting the first characters.
If the number of characters extracted seems incorrect (e.g., you asked for 5 but got 4), check for leading spaces. A space is counted as a character. If your data is " 12345" and you extract 5 characters, you will get " 1234", missing the final digit. Always use the Trim function before extracting to ensure you are capturing actual data.
Frequently Asked Questions
Can I extract a variable number of characters from the left?
Yes, but not through the standard "First Characters" menu. You must use a Custom Column and the Text.Start function, referencing another column that contains the desired number of characters for each specific row.
What is the difference between Text.Start and the LEFT function in Excel?
They achieve the same result, but Text.Start is written in M (Power Query's language), which is case-sensitive. While Excel's LEFT function works within a cell, Text.Start works on the entire column during the data loading process.
Does extracting text from the left change my original data source?
No. Power Query is a non-destructive transformation tool. It reads the data from your source and applies a series of steps to it in memory. Your original CSV, Excel file, or Database remains untouched.
Can I extract text from the left based on a specific character?
Yes. Instead of using "First Characters," use Extract > Text Before Delimiter. This allows you to tell Power Query to take everything from the left up until it hits a specific character, such as a comma, space, or dash.
Why is my extracted text appearing as a number?
Power Query sometimes tries to be helpful by automatically detecting the data type of a new column. If your extracted characters are all digits, it may change the type to Whole Number. You can manually change it back to Text using the data type icon in the column header.