Smart Systems for Home

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

Excel Power Query String Functions: A How-To Guide

· 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

Power Query string functions let you clean, split, trim, and reformat text columns without writing formulas in the worksheet. The quickest way to use them is to load your data into Power Query, add a custom column with a function such as Text.Trim or Text.Proper, and then close and load the result back to Excel. This guide walks through the practical steps, the functions you will actually use, and the mistakes that trip people up.

What You'll Need

  • Excel 2016 or later on Windows, Excel 2016 or later on Mac, or a Microsoft 365 subscription (Power Query is built in; older versions need the free Power Query add-in).
  • A table or range of text data to work with.
  • Basic familiarity with the Power Query Editor window (the ribbon, the Queries pane, and the Applied Steps list).

Step-by-Step

  1. Load your data into Power Query. Select any cell in your table, then go to Data > From Table/Range. If the data is in a workbook file, use Data > Get Data > From File > From Workbook instead. The Power Query Editor opens in a separate window. Menu labels can differ slightly by version, device, and region, so look for the closest match.
  2. Check the column type. In the Editor, click the small icon to the left of the column header. If the column shows ABC or Any, set it to Text so string functions behave predictably. Numbers stored as text will not trim or split the way you expect if the type is wrong.
  3. Add a custom column for your first function. Go to Add Column > Custom Column. In the formula box, type a function call such as Text.Trim([CustomerName]). Replace [CustomerName] with your actual column name. Give the new column a name and click OK. This leaves the original column intact so you can compare results.
  4. Use the most common string functions. The ones you will reach for most often are:
    • Text.Trim and Text.Clean to remove leading/trailing spaces and non-printing characters.
    • Text.Proper to capitalise each word, and Text.Upper or Text.Lower for case changes.
    • Text.Start and Text.End to grab characters from the start or end of a value.
    • Text.Middle to extract characters from the middle.
    • Text.Length to count characters.
    • Text.Contains to test whether a value includes a substring.
    • Text.Replace to swap one substring for another.
    • Text.Split to break a value into a list, often combined with Split Column on the ribbon.
    • Text.Combine to join a list of values into one string.
    • Text.BeforeDelimiter and Text.AfterDelimiter to pull text around a character such as a comma or hyphen.
  5. Use the ribbon for simple splits. Select the column, then go to Home > Split Column > By Delimiter or By Number of Characters. This writes the M code for you and is faster than typing Text.Split by hand. You can inspect the generated step in the Applied Steps list and adjust it if needed.
  6. Combine functions in one formula. Custom columns accept nested calls. For example, trims spaces and fixes capitalisation in a single step. Keep nesting to two or three levels; beyond that, split the work into separate columns for readability and easier debugging.
  7. Handle nulls and errors. Wrap risky calls with try ... otherwise. For example, try Text.Start([Code], 3) otherwise null returns null instead of an error when a value is missing. You can also use Replace Values or Remove Errors on the ribbon for quick clean-ups.
  8. Rename and reorder the new columns. Double-click a column header to rename it, and drag headers to reposition them. Clear names make the output table easier to use in Excel.
  9. Close and load. Go to Home > Close & Load to send the result to a new worksheet, or Close & Load To... to choose a table, pivot, or connection only. The query is saved in the workbook and can be refreshed later.
  10. Refresh after source changes. Right-click the output table and choose Refresh, or use Data > Refresh All. The string transformations re-run automatically on the updated data.

Common Mistakes

  • Wrong column type. Running Text.Trim on a column typed as Whole Number returns errors. Set the type to Text first.
  • Case-sensitive comparisons. Text.Contains is case-sensitive by default. Add a comparer argument such as Comparer.OrdinalIgnoreCase if you need case-insensitive matching.
  • Forgetting that M is case-sensitive. Function names must be capitalised exactly, e.g. Text.Proper, not text.proper.
  • Editing the output table directly. Typing over cells in the loaded table breaks the query connection. Make changes in the Power Query Editor instead.
  • Assuming Text.Split returns a single value. It returns a list. Use Split Column on the ribbon, or wrap it with an index such as Text.Split([Value], "-"){0} to get the first part.
  • Leaving stray spaces. Values copied from web pages often contain non-breaking spaces. Text.Trim does not remove those; use Text.Replace to swap them for normal spaces first.

Tips

  • Use Add Column > Custom Column rather than editing the original column, so you can compare before and after.
  • The Applied Steps list records every action. Click a step to preview its result, and delete or reorder steps if something goes wrong.
  • Use the formula bar in the Editor to inspect the M code behind ribbon actions; it is the fastest way to learn the function names.
  • For repeated clean-ups, save the query as a template or copy the M code into a blank query via Home > Advanced Editor.
  • Keep a raw copy of the source data in a separate query or sheet so you can always start over.

Troubleshooting

  • The custom column shows errors. Check the column type and confirm the column name in brackets matches exactly, including spaces. Column names with spaces need the same spelling inside the square brackets.
  • Refresh fails after moving the file. The query may point to an old file path. Open Data > Queries & Connections, right-click the query, and edit the source path.
  • Results look unchanged. Make sure you clicked Close & Load after editing, and refresh the output table. Also check that the step you added is not disabled or moved above the step it depends on.
  • Function name is not recognised. Confirm the spelling and capitalisation, and check that the function exists in your Excel version. Some newer functions are only available in Microsoft 365.
  • Split column produces too many columns. Delete the extra columns or use Split Column > By Delimiter with the Advanced options set to split into a limited number of columns.

Frequently Asked Questions

Do I need to enable Power Query in Excel?

In Excel 2016 and later, Power Query is built in under the Data tab. Older versions require the free Microsoft Power Query add-in. There is no separate toggle to switch on.

What is the difference between Power Query string functions and Excel text functions?

Excel functions such as TRIM and PROPER work on cells and recalculate in the sheet. Power Query functions run as part of a data transformation step and are saved with the query, so they reapply automatically on refresh.

Can I use Power Query string functions on Mac?

Yes. Power Query is available in Excel for Mac, though some ribbon labels and the exact location of options can differ from Windows. The M functions themselves behave the same.

How do I remove all spaces from a text column?

Use Text.Replace([Column], " ", "") in a custom column to remove every space, or Text.Trim to remove only leading and trailing spaces.

Why does Text.Contains return false when the text is clearly there?

It is case-sensitive by default. Add Comparer.OrdinalIgnoreCase as the third argument, or convert both sides to the same case with Text.Lower before comparing.

Can I undo a Power Query step?

Yes. In the Applied Steps list, click the X next to a step to delete it, or right-click and choose Delete. The source data is not changed, so you can always rebuild the step.