Sub topic 6
Cleaning text: trim, clean, case, and replace values
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set data types. Text columns often look fine in Excel but fail to match, sort, or group correctly. The cause is usually invisible: extra spaces, non-printing characters, or inconsistent capitalization.
Why text looks right but behaves wrong
"North", "North " and "north" are three different values to Power Query. A lookup, merge, or Group By treats them as separate keys. Cleaning text early prevents mismatches later.
| Problem | Symptom | Fix |
|---|---|---|
| Leading or trailing spaces | Duplicates that look identical | Trim |
| Line breaks, tabs, control characters | Odd boxes or broken cells | Clean |
| Mixed capitalization | Same value appears several times | Capitalize Each Word, lowercase, UPPERCASE |
| Typos or placeholder text | "N/A", "Nrth" | Replace Values |
Trim and Clean
Select the column, then choose Transform > Format > Trim or Clean.
- Trim removes leading and trailing spaces. It also reduces runs of repeated spaces inside the text to a single space.
- Clean removes non-printing characters, such as line breaks and tabs.
Trim does not remove a non-breaking space (character 160), which often arrives from web pages. If Trim seems to do nothing, replace that character first.
You can combine both operations in one step. This is what the Region step in the first example does:
let
Source = Excel.CurrentWorkbook(){[Name="SalesRaw"]}[Content],
Typed = Table.TransformColumnTypes(Source, {{"Date", type date}, {"Amount", type number}}),
Trimmed = Table.TransformColumns(Typed, {{"Region", each Text.Trim(Text.Clean(_)), type text}})
in
TrimmedText.Clean runs first, then Text.Trim. The _ stands for the current cell value, and each applies the function to every row.
Changing case
Under Transform > Format, choose lowercase, UPPERCASE, or Capitalize Each Word. Use lowercase for keys such as email addresses or product codes, since it makes later merges predictable. Capitalize Each Word suits names, but it turns "McDonald" into "Mcdonald", so check the results.
Replacing values
Select a column and choose Transform > Replace Values. Enter the value to find and the replacement. Under Advanced options, you can match the entire cell contents only.
- Without "Match entire cell contents", "N/A" inside "N/A - pending" is also replaced.
- Replace Values is case-sensitive. "na" and "NA" need separate steps.
- To turn a placeholder into a true blank, leave the replacement empty or use
null.
let
Source = Excel.CurrentWorkbook(){[Name="SalesRaw"]}[Content],
NoPlaceholder = Table.ReplaceValue(Source, "N/A", null, Replacer.ReplaceValue, {"Region"})
in
NoPlaceholderThis replaces text that contains "N/A" in the Region column. For whole-cell matching, use Replacer.ReplaceValue only when the entire value is the target, or add a conditional step.
Common errors
- Cleaning a column before its type is set to text can leave numbers untouched. Set the type first.
- Applying Trim to the wrong column produces no visible change, so check the preview.
- Replacing in several columns at once changes values you did not intend to touch.
Exercise
A Region column shows "East" and "East " as separate groups after Group By. Which step fixes this, and why does the problem occur?
Show answer
Apply Trim (Transform > Format > Trim) to the Region column before grouping. Power Query compares text exactly, so the trailing space makes "East " a different value from "East".
Exercise
Trim leaves a gap at the end of values copied from a web page. What is the likely cause, and how can you fix it?
Show answer
The text probably has non-breaking spaces (character 160), which Trim does not remove. Use Replace Values, or Text.Replace with Character.FromNumber(160), to replace them with a normal space or empty text, then apply Trim.