Ravindra BagaleCourses & study guides मराठी Track your progress

7. Data Cleaning A–Z in Power Query

7.6 Extra Spaces, Non-printable Characters and Text Case

Start with a problem

A Store chart shows four bars: "Kothrud ", Kothrud in capitals, baner and Baner. There are only two stores.

Explain simply

Trim removes spaces at the start and end. Clean removes non-printable characters. Format changes case (lowercase, UPPERCASE, Capitalize Each Word).

Show the data

Before:

Store Distinct?
"Kothrud " 1
baner 2
KOTHRUD 3
Baner 4

About this data: This table uses columns: Store, Distinct?. This table uses columns: Store, Rows (5 order rows). This table uses columns: City, Area, Customer. Values shown are the lesson examples already on this page (not a new dataset).

Fictional sample data. Practice file: pq_raw_pune_export.csv (source: src/practice/).

Demonstrate

  1. Tick View › Show whitespace to see the trailing space.
  2. Select Store › Transform › Format › Trim, then Format › Clean.

    Your result should look like this:

    Store
    Kothrud
    baner
    KOTHRUD
    Baner
  3. Transform › Format › Capitalize Each Word.

    Your result should look like this:

    Store
    Kothrud
    baner, then Baner
    KOTHRUD, then Kothrud
    Baner
  4. Check Column distribution: distinct count.

Show the result

Your result should look like this (fictional sample):

Store Rows (5 order rows)
Kothrud 3 (401, 403, 403)
Baner 2 (402, 404)
Distinct values 2

Explain what happened

Power BI treats text as different if spaces or case differ (for grouping in Power Query; DAX is case-insensitive but keeps the first spelling). Trim + Capitalize makes one spelling per store.

Explore further

  • Use UPPERCASE for codes (Order ID) and Capitalize Each Word for names.

Detailed reference for this lesson:

Before

City Area Customer
·Pune kothrud ruhi bagale
PUNE·· KOTHRUD RUHI BAGALE
pune Kothrud↵ Ruhi Bagale
Pune baner shahrukh

After

City Area Customer
Pune Kothrud Ruhi Bagale
Pune Kothrud Ruhi Bagale
Pune Kothrud Ruhi Bagale
Pune Baner Shahrukh

(· = an extra space, ↵ = a hidden line-break character copied from another system.)

Command (Transform › Format) What it does Example
Trim Removes spaces at the start and end only "·Pune··", then "Pune"
Clean Removes non-printable control characters (line feeds, tabs etc.) "Kothrud↵", then "Kothrud"
lowercase all small letters "PUNE", then "pune"
UPPERCASE ALL CAPITALS (good for codes) "blk-001", then "BLK-001"
Capitalize Each Word First letter of each word capital "ruhi bagale", then "Ruhi Bagale"
Add Prefix / Add Suffix Adds fixed text "10001", then "BLK-10001"

Steps in Power BI

  1. Select the text columns (Ctrl + click City, Area, Customer).
  2. Transform › Format › Clean, then Transform › Format › Trim.
  3. Transform › Format › Capitalize Each Word (or UPPERCASE for code columns such as Store ID, Product ID).
  4. To fix double spaces inside text ("RUHI··BAGALE"): Transform › Replace Values › find two spaces › replace with one space. Repeat until none are left, or use the M code below.
  5. To remove non-breaking spaces (common in web copy-paste): Replace Values › Advanced options › Replace using special characters › Insert special character › Non-breaking space → replace with a normal space.
Cleaned = Table.TransformColumns(Source, {
    {"City", each Text.Proper(Text.Trim(Text.Clean(_))), type text},
    {"Store ID", each Text.Upper(Text.Trim(_)), type text},
    // collapse multiple inner spaces into one
    {"Customer", each Text.Combine(List.Select(Text.Split(Text.Trim(_), " "), each _ <> ""), " "), type text}
})

Power Query is case-sensitive, the data model is not

In Power Query "pune" and "Pune" are different values. After loading, the Power BI model (VertiPaq) treats text case-insensitively, so it may show fakt one spelling for both, whichever it meets first. Fix case in Power Query so that the value shown is the one you want.

Tip

Trim never removes spaces in the middle of text. That is why "Andheri··East" survives a Trim.

Practice task

Clean a Customer table where names come as "zoya", "ZOYA ", " Zoya" and e-mails in mixed case. Names, then Capitalize Each Word; e-mails, then lowercase; all columns trimmed and cleaned.

Ravindra Bagale's Tip

Friends, many students apply Trim and think the data is clean, but Trim does not remove non-breaking spaces or line breaks copied from web pages and apps. Use Clean as well, and if a value still doesn't match, replace the non-breaking space character (#(00A0)) explicitly. Fix the text case after trimming, not before. This matters for both exams and interviews.

Fix common mistakes

Wrong result Reason Correction
Still 3 values Clean/Trim skipped non-breaking spaces (char 160) Replace Values: non-breaking space, then normal space, then Trim
Order IDs became Blk-401 Capitalize Each Word on codes Use UPPERCASE for codes
Merge with stores table fails Keys cleaned on one side only Clean both sides

Practise independently

Practice task

How many distinct Store values are there before and after cleaning?

Hints
  • Count spellings.
Solution

Before: 4; after: 2 (Kothrud, Baner).

Think like a professional

Text hygiene before joins and grouping prevents most 'missing data' tickets.

Ravi Sir's Cards for this lesson

मराठी cards