Sub topic 2
Cleaning and Reshaping Data in Power Query
Prerequisites: You should know Excel tables, rows, columns, and basic data types. You should also know how to open the Power Query Editor. The examples assume a query named SalesRaw.
Clean text values
Text cleanup makes matching, grouping, and merging more reliable. Text.Trim removes spaces at the beginning and end. Text.Clean removes non-printing characters. Case functions standardize capitalization.
let
Source = Excel.CurrentWorkbook(){[Name="SalesRaw"]}[Content],
Cleaned = Table.TransformColumns(
Source,
{
{"Region", each Text.Upper(Text.Trim(Text.Clean(_))), type text},
{"Product", each Text.Trim(Text.Clean(_)), type text}
}
),
Replaced = Table.ReplaceValue(
Cleaned,
"NORTH EAST",
"NORTHEAST",
Replacer.ReplaceText,
{"Region"}
)
in
ReplacedUse Text.Lower, Text.Upper, or Text.Proper when a consistent case matters. In the editor, these operations appear as Applied Steps. You can edit a step from its gear icon or inspect its M code in the formula bar.
> callout note > Empty text ("") and null are different. Empty text is a value with zero characters. null means no value. Convert empty text to null when later steps must treat it as missing.
Split and merge columns
Use Split Column when one field contains several values, such as Smith, Ana. Split by a delimiter, such as a comma, or by a fixed number of characters. Use Merge Columns to create a combined label, such as Region-Product. Choose a separator that cannot be confused with real data.
Fill down and handle nulls
Cross-tab exports often show a region once, followed by blank rows. Select the column and choose Fill > Down. Power Query copies the last non-null value into later null rows. Replace empty strings with null first if necessary. Do not fill an identifier column unless the source layout clearly indicates that repeated values were omitted.
Unpivot cross-tab data
Unpivoting converts month columns into rows. This creates a tidy table where each row represents one observation.
Select the identifier columns, then choose Unpivot Other Columns. Rename the generated Attribute and Value columns to meaningful names such as Month and Units.
Load and refresh
Choose Close & Load To to load results to an Excel table, the Data Model, or Only Create Connection. A connection-only query can feed another query without creating a visible worksheet table. Select Refresh to rerun the source and every transformation step.
Exercise
Why might a merge fail even when two Region values look identical?
Show answer
Hidden characters, extra spaces, or different capitalization can make the text differ. Clean and standardize both columns before merging.