Sub topic 6
Cleaning text: trim, clean, case, and replace
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set a column's data type. Text columns are where most visible dirt lives: stray spaces, invisible characters, and inconsistent capitalization. If you don't fix these, Power BI treats "Alice Smith" and " alice smith " as different values. Slicers, relationships, and duplicate removal then give wrong results.
The four basic transformations
You find these under Transform > Format (for the first three) and Transform > Replace Values (for the last). Each one adds an applied step.
| Transformation | M function | What it does |
|---|---|---|
| Trim | `Text.Trim` | Removes leading and trailing spaces. Spaces inside the text stay. |
| Clean | `Text.Clean` | Removes non-printing characters, such as line breaks and tabs. |
| Capitalize Each Word | `Text.Proper` | Makes the first letter of each word uppercase and the rest lowercase. |
| Replace Values | `Table.ReplaceValue` | Swaps one value or substring for another. |
The Format menu also offers lowercase (Text.Lower) and UPPERCASE (Text.Upper). Pick one case style per column and use it everywhere.
Worked example
The M code below cleans a Customer column in one step. It also fills a null region and fixes the types.
let
Source = #table(
{"Customer", "Region", "Amount"},
{
{" alice smith ", "West", "120"},
{"BOB JONES", null, "abc"},
{"BOB JONES", null, "abc"}
}
),
CleanedText = Table.TransformColumns(Source, {{"Customer", each Text.Proper(Text.Trim(Text.Clean(_))), type text}}),
FilledRegion = Table.ReplaceValue(CleanedText, null, "Unknown", Replacer.ReplaceValue, {"Region"}),
RemovedDuplicates = Table.Distinct(FilledRegion),
ChangedType = Table.TransformColumnTypes(RemovedDuplicates, {{"Amount", type number}}, "en-US"),
ReplacedErrors = Table.ReplaceErrorValues(ChangedType, {{"Amount", 0}})
in
ReplacedErrorsLook at CleanedText. The expression each Text.Proper(Text.Trim(Text.Clean(_))) runs from the inside out. _ is the current cell value. Text.Clean runs first, then Text.Trim, then Text.Proper. The result is "Alice Smith" and "Bob Jones".
When you use the ribbon, each button adds a separate step: Clean, Trim, then Capitalize Each Word. Three steps and one combined step give the same result. Separate steps are easier to debug. A combined step is shorter.
Replace Values
Replace Values works on whole cell values by default. It replaces a cell only if the entire cell matches. To replace part of a cell, open Advanced options in the dialog and leave Match entire cell contents unchecked. Without that setting, replacing "St." with "Street" leaves "12 Main St." unchanged.
Replace Values is case-sensitive. Replacing "west" does not affect "West". Apply a case transformation first so you only need one search value.
Common errors
- Trim does not remove all spaces.
Text.Trimonly removes standard spaces at the ends. A non-breaking space (character 160) from web or PDF data survives. Use Replace Values withCharacter.FromNumber(160)in the M code, or replace it by editing the step in the formula bar. - Replacing null fails. In the dialog, type
nullin the Value To Find box. The editor generates the correct null value. - Case change on a non-text column. The Format commands appear only on text columns. Set the type to text first.
- Case changes break acronyms.
Text.Properturns"USA"into"Usa". Apply it only to name-like columns.
Exercise
A City column contains " new york" and "NEW YORK ". After you apply Trim and Capitalize Each Word, how many distinct values remain, and what is the value?
Show answer
One distinct value, "New York". Trim removes the leading and trailing spaces. Capitalize Each Word makes both entries "New York", so they match exactly.
Exercise
You replace "St." with "Street" using Replace Values with default settings, but "12 Main St." does not change. What is the cause and the fix?
Show answer
By default, Replace Values matches the entire cell contents. Open Advanced options and clear "Match entire cell contents" so it replaces the substring.