Sub topic 2
Clean Text and Data Quality in Power Query
Assume you know Power BI tables, columns, and basic data types. Power Query applies transformations in order. Each transformation becomes an applied step written in M, so later steps use the results of earlier steps.
Cleaning text values
Text often contains leading spaces, hidden control characters, inconsistent capitalization, or unwanted symbols. Clean it before grouping, joining, or reporting.
Text.Trimremoves spaces at the beginning and end.Text.Cleanremoves nonprinting characters.Text.Upper,Text.Lower, andText.Properstandardize case.- Replace values to correct known variations.
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}}
),
StandardizedRegion = Table.TransformColumns(
CleanedText,
{{"Region", each if _ = null then null else Text.Proper(Text.Trim(_)), type text}}
)
in
StandardizedRegionUse the column menu for common operations. Use the formula bar or Advanced Editor when you need to inspect or make a small change to the generated M code.
Handling nulls and empty rows
A null value means that no value exists. It is different from an empty string such as "". Replace nulls when a meaningful default exists, such as "Unknown". Use Fill Down when a label appears only on the first row of a group. Remove rows only when they contain no useful data.
let
FilledRegion = Table.ReplaceValue(
StandardizedRegion,
null,
"Unknown",
Replacer.ReplaceValue,
{"Region"}
)
in
FilledRegionRemoving duplicates
Select the columns that define uniqueness, then choose Remove Rows > Remove Duplicates. Select all columns only when complete rows must be identical. Removing duplicates too early can hide source-system problems, so profile the data first.
Finding and replacing errors
A conversion can create an error when a value such as "abc" is changed to a number. Filter the column for errors, inspect the source values, and choose whether to remove the rows or replace errors with a documented fallback.
let
Typed = Table.TransformColumnTypes(FilledRegion, {{"Amount", type number}}, "en-US"),
SafeAmount = Table.ReplaceErrorValues(Typed, {{"Amount", 0}})
in
SafeAmountExercise
Why should you clean text before removing duplicates?
Show answer
Values such as " Alice " and "Alice" represent the same person but are different strings. Cleaning first lets Power Query identify them as duplicates.