Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Left Function: How to Extract Text

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Power Query Left function extracts a specific number of characters from the start of a text string. To use it, you navigate to the Add Column tab, select Custom Column, and enter the formula Text.Start([ColumnName], number_of_characters). This is essential for cleaning data where identifiers, area codes, or category prefixes are stored at the beginning of a cell and need to be isolated for analysis.

What You'll Need

  • Microsoft Excel or Power BI Desktop.
  • A dataset imported into the Power Query Editor.
  • A column containing text data (strings) from which you want to extract characters.
  • Knowledge of how many characters you need to extract from the left side of the string.

Step-by-Step

  1. 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.
  2. Select Your Table: In the Queries pane on the left, click on the table that contains the text you wish to modify.
  3. Access the Custom Column Menu: Navigate to the Add Column tab on the top ribbon. Click on Custom Column. This opens a dialog box where you can write a formula to create a new column based on existing data. Labels and menu locations may vary slightly depending on your software version or region.
  4. Name Your New Column: In the New column name field at the top of the dialog box, enter a descriptive name, such as "Extracted Prefix" or "Area Code."
  5. Enter the Text.Start Formula: In the Custom column formula box, type the following: Text.Start([YourColumnName], X). Replace [YourColumnName] by double-clicking the desired column from the Available columns list on the right, and replace X with the actual number of characters you want to keep. For example, to get the first three characters, use Text.Start([ProductCode], 3).
  6. Validate the Formula: Look at the bottom of the dialog box. Power Query will show a syntax check. If it says "No syntax errors have been detected," the formula is written correctly.
  7. Apply and Close: Click OK. You will now see a new column at the end of your table containing only the leftmost characters.
  8. Set the Data Type: Click the data type icon (e.g., ABC/123) in the header of your new column and select Text. This ensures the data is handled correctly in subsequent steps.
  9. Load the Data: Go to the Home tab and click Close & Apply to bring the modified data back into your Excel worksheet or Power BI model.

Common Mistakes

One of the most frequent errors is using the wrong function name. Users coming from Excel formulas often try to type LEFT() into the Power Query custom column box. Power Query uses the M language, and the equivalent of the Excel LEFT function is Text.Start(). Using LEFT() will result in a "Token Literal expected" or "The name 'LEFT' wasn't recognized" error.

Another common mistake is neglecting case sensitivity. The M language is strictly case-sensitive. Writing text.start() or Text.start() with a lowercase 's' will cause the formula to fail. It must be written exactly as Text.Start().

Users also frequently forget to handle null values. If a cell in the source column is empty (null), the Text.Start() function will return a null value for that row. While this isn't always an error, it can cause issues if you later try to combine that result with other text. If you need a blank string instead of a null, you must wrap the function in an if-statement or use the Replace Values feature before applying the extraction.

Finally, some users attempt to extract a number of characters that exceeds the length of the string. While Text.Start() is generally forgiving and will simply return the entire string if the number requested is larger than the available text, it can lead to inconsistent data lengths if you are expecting a fixed-width result for every row.

Tips

To make your extraction dynamic, avoid hard-coding the number of characters if the length of the prefix varies. For example, if you want everything before a specific character like a hyphen (-), do not use Text.Start() with a fixed number. Instead, use Text.BeforeDelimiter([ColumnName], "-"). This is far more robust than a left-function when dealing with variable-length strings.

If you need to extract characters from the right side instead of the left, use the Text.End() function. The syntax is identical to Text.Start(), but it counts from the end of the string backward. This is particularly useful for extracting file extensions or year suffixes from IDs.

For those who prefer a no-code approach, you can use the "Column From Examples" feature. Go to Add Column > Column From Examples > From Selection. Type the desired result for the first few rows, and Power Query will automatically detect that you are performing a "Left" extraction and write the Text.Start() formula for you in the background.

When working with large datasets, consider whether you need a new column or if you should transform the existing one. If you don't need the original full string, you can use the Transform tab > Extract > First Characters. This modifies the column in place, reducing the memory footprint of your query by avoiding the creation of duplicate data.

Troubleshooting

The Text.Start() function only works on text data types. To fix this, go to the step immediately before your custom column, click the data type icon in the column header, and change it to Text. Alternatively, wrap your column reference in the Text.From() function, like this: Text.Start(Text.From([YourColumnName]), 3) .

If the results are not what you expected—for example, you are getting trailing spaces—it is likely that your source data contains leading or trailing whitespace. This is a common issue with data imported from CSVs or old databases. To resolve this, right-click the source column, select Transform, and click Trim. This removes all leading and trailing spaces, ensuring that Text.Start() counts only the actual characters of the data.

If the Power Query editor becomes slow or freezes after adding the column, check if you have a very large number of steps in your Applied Steps pane. Each custom column creates a new step. If you have dozens of these, try combining them or using a single complex formula to handle multiple extractions. You can also try disabling "Background Data" in the Query Options to improve performance during the development phase.

Frequently Asked Questions

Can I use Text.Start with a variable instead of a fixed number?

Yes. Instead of typing a number like 3, you can reference another column that contains the number of characters to extract. The formula would look like Text.Start([TextColumn], [LengthColumn]).

What happens if the cell is empty?

If the cell contains a null value, Text.Start() will return null. If the cell is an empty string (blank but not null), it will return an empty string.

Is there a way to do this without writing any code?

Yes. Go to the Transform tab, click Extract, and select First Characters. You will then be prompted to enter the number of characters you want to keep.

Does Text.Start count spaces as characters?

Yes, every space, punctuation mark, and special character is counted as one character toward the total number you specify.

Can I use this to extract the first word of a sentence?

While you could use Text.Start() if the first word is always the same length, it is better to use Text.BeforeDelimiter([Column], " ") to extract everything before the first space.

How do I remove the first few characters instead of keeping them?

To remove the first X characters, use the Text.Middle() function or Text.RemoveRange(). For example, Text.Middle([Column], 3) will start at the 4th character and take everything remaining.