How to Clean Messy Data in Excel with Power Query
Comprehensive technical methodology to transform dirty ERP or CRM extracts into pristine structured tables. Eliminate duplicates, fix malformed dates, sanitize non-breaking spaces, and standardize data types without VBA.
Why Manual Spreadsheet Cleaning Fails at Scale
In every corporate organization, data exported from transactional ERP systems (SAP, Oracle, NetSuite) or CRM tools (Salesforce, HubSpot) arrives riddled with formatting defects:
- Invisible non-breaking spaces (CHAR(160)) that prevent lookup functions from matching.
- Numbers formatted as text strings with embedded currency symbols and thousand separators.
- Inconsistent date formats (01/05/2026, 2026-01-05, Jan 5, 2026) that fail chronology sorting.
- Trailing subtotal rows mixed directly into transactional records.
- Inconsistent text casing (ACME CORP, Acme Corp, acme corp) that fragments Pivot Table aggregations.
Relying on manual Find & Replace, helper columns, or Excel formulas every week is slow, non-auditable, and prone to silent failures.
Power Query transforms data hygiene into a deterministic pipeline: you specify transformation rules once, and they execute flawlessly on every subsequent refresh.
Real-World Case Study: Dirty Extract Audit
Consider a representative messy sales extract:
| Customer_ID | Customer_Name | Order_Date | Invoice_Amount | Status |
|---|---|---|---|---|
| " C001 " | ACME CORP | 01/04/2026 | "$1,250.50" | approved |
| C002 | Wayne Enterprises | 2026-01-08 | 840.00 | APPROVED |
| C001 | Acme Corp | 01/04/2026 | "$1,250.50" | approved |
| Total | $2,090.50 | |||
| C003 | Stark Industries | 01/12/2026 | Missing | Pending |
| C004 | Oscorp | 01/15/2026 | 3,100.00 | APPROVED |
Identified data defects:
1. Aggregated total row polluting transactional granularity (Total).
2. Duplicate order record for Acme Corp.
3. Leading/trailing whitespace in identifiers and company names.
4. Casing divergence (ACME CORP vs Acme Corp).
5. Text-formatted currency with dollar symbols and commas ("$1,250.50").
6. Business exception string (Missing) inside a numeric metric column.
7. Inconsistent status categorization (approved, APPROVED).
Step-by-Step Automated Cleaning Pipeline
Step 1: Filter Subtotals and Blank Rows
Never begin by setting column data types: eliminate structural noise first.
1. Click the filter dropdown on Customer_ID.
2. Uncheck Total and null/blank entries.
3. In M code:
Table.SelectRows(Source, each [Customer_ID] <> null and [Customer_ID] <> "" and not Text.StartsWith([Customer_ID], "Total"))
Step 2: Strip Whitespace and Non-Printable Characters
Standard Excel TRIM() does not remove ASCII 160 non-breaking spaces frequently injected by web applications. Power Query provides two targeted transforms:
1. Select text columns (Customer_ID, Customer_Name, Status).
2. Go to Transform tab > Format > Trim (strips leading and trailing spaces).
3. Click Format > Clean (purges line returns and control characters ASCII 0 to 31).
Step 3: Standardize Text Casing
To unify ACME CORP and Acme Corp:
1. Select Customer_Name.
2. Transform tab > Format > Capitalize Each Word (or Uppercase based on corporate conventions).
Step 4: Strict Deduplication
Differentiate two distinct business cases:
- Primary Key Deduplication: If an entity must be unique by ID, select Customer_ID > Right-click > Remove Duplicates.
- Full-Row Deduplication: If duplicate data transmissions occurred, select all columns (Ctrl + A) > Right-click > Remove Duplicates.
Step 5: Convert Dirty Text Currencies to Numeric Decimals
Invoice_Amount contains dollar signs, thousand separators, and non-numeric strings (Missing).
1. Select Invoice_Amount.
2. Transform tab > Replace Values:
- Value To Find: $
- Replace With: (leave empty)
3. Transform tab > Replace Values:
- Value To Find: ,
- Replace With: (leave empty)
4. Set column data type to Decimal Number.
5. The string "Missing" automatically resolves to an Error.
6. Right-click column header > Replace Errors > Enter 0 (or preserve as null depending on accounting requirements).
Step 6: Locale-Aware Date Parsing
When dates originate from international systems using month-first formatting:
1. Right-click Order_Date header.
2. Select Change Type > Using Locale....
3. Data Type: Date.
4. Locale: English (United States).
Step 7: Categorical Normalization via Conditional Column
To standardize approved and APPROVED:
1. Add Column tab > Conditional Column.
2. Rule: If Status contains APPROV, then Approved, otherwise Pending.
3. Drop the legacy column and rename the new attribute.
Complete Production M Script
Open the Advanced Editor in Power Query and paste the following verified M transformation block:
let
Source = Excel.CurrentWorkbook(){[Name="Raw_Data_Table"]}[Content],
FilteredRows = Table.SelectRows(Source, each ([Customer_ID] <> null and [Customer_ID] <> "" and not Text.StartsWith([Customer_ID], "Total"))),
CleanedWhitespace = Table.TransformColumns(FilteredRows, {
{"Customer_ID", Text.Trim, type text},
{"Customer_Name", each Text.Trim(Text.Clean(_)), type text},
{"Status", Text.Trim, type text}
}),
StandardizedCasing = Table.TransformColumns(CleanedWhitespace, {
{"Customer_Name", Text.Proper, type text}
}),
Deduplicated = Table.Distinct(StandardizedCasing),
StrippedCurrency = Table.ReplaceValue(Deduplicated, "$", "", Replacer.ReplaceText, {"Invoice_Amount"}),
StrippedCommas = Table.ReplaceValue(StrippedCurrency, ",", "", Replacer.ReplaceText, {"Invoice_Amount"}),
TypedAmounts = Table.TransformColumnTypes(StrippedCommas, {{"Invoice_Amount", type number}}, "en-US"),
HandledErrors = Table.ReplaceErrorValues(TypedAmounts, {{"Invoice_Amount", 0}}),
TypedDates = Table.TransformColumnTypes(HandledErrors, {{"Order_Date", type date}}, "en-US"),
StandardizedStatus = Table.AddColumn(TypedDates, "Status_Clean", each if Text.Contains(Text.Upper([Status]), "APPROV") then "Approved" else "Pending", type text),
FinalColumns = Table.RemoveColumns(StandardizedStatus, {"Status"}),
RenamedStatus = Table.RenameColumns(FinalColumns, {{"Status_Clean", "Status"}})
in
RenamedStatus
Data Quality Audit: Before vs. After
| Validation Check | Raw Extract | Power Query Pipeline |
|---|---|---|
| Total Rows | 6 rows | 4 valid records |
| Subtotal Ingestion | 1 corrupting row | 0 (systematically purged) |
| Duplicate Transactions | 1 duplicate | 0 (distinct records) |
| Amount Column Schema | Unusable string data | Clean decimal metric |
| Status Category Count | 3 divergent values | 2 standardized buckets |
Frequently Asked Questions (FAQ)
Q: Does refreshing Power Query overwrite raw files?
A: No. Power Query operates strictly in read-only mode against underlying source files. Your original extracts remain immutable for regulatory audit compliance.
Q: How does this scale to 500,000 rows?
A: Power Query streams and transforms data in high-performance memory blocks. A 500,000-row file with these 10 transformation steps typically executes in less than 3 seconds on standard business hardware.
Master Excel, Power Query & Power BI
Discover our practical courses designed to automate spreadsheets, clean data without code, and build audit-ready dashboards.
View Courses →