π§ͺ Exercise 05: Clean Messy Data With AI Assistance
Objective
Use AI to generate text cleaning and standardization formulas, inspect the generated functions (TRIM, CLEAN, PROPER), and rebuild the pipeline in Power Query.
Dataset
Raw_Customer_Names:
- Cell A2:
" john DOE "(Leading/trailing whitespace, non-breaking spaceCHAR(160), inconsistent casing) - Cell A3:
"sarah connor"(Tab characterCHAR(9)inside string) - Cell A4:
"MICHAEL O'CONNOR"(Consecutive internal spaces, uppercase with apostrophe)
Business Scenario
A marketing database export contains messy customer names with hidden spaces and improper capitalization. You need clean, standardized names formatted in proper title case for an email campaign.
Task
- Prompt AI for an Excel formula to strip extra whitespace, remove non-printable characters, and format to proper case.
- Verify how the formula handles Irish surnames like
O'Connor. - Translate the formula logic into an automated Power Query step.
AI Prompt
TEXT
Task: Clean messy customer names in Excel.
Input string in cell A2 has:
- Extra leading/trailing spaces
- Multiple internal spaces
- Possible non-breaking spaces (CHAR(160)) and non-printable control characters
- Mixed uppercase and lowercase letters
Write an Excel formula to sanitize this text to Proper Case.
Explain how Excel handles words with apostrophes like "O'Connor".
Provide the equivalent Power Query M code.
Expected Reasoning
- Standard Excel
TRIM()removes leading/trailing ASCII spaces (CHAR(32)) and collapses multiple consecutive spaces to one. CLEAN()removes non-printable ASCII characters (0to31).SUBSTITUTE(A2, CHAR(160), " ")replaces web non-breaking spaces with standard spaces before trimming.PROPER()capitalizes the first letter of each word, but capitalizes the letter following an apostrophe (O'ConnorbecomesO'ConnororO'connordepending on Excel localization).
Validation
Manual Solution
π Click to Reveal Verified Formula & Power Query Solution
Excel Formula:
EXCEL
=PROPER(TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " "))))
Power Query M Code:
POWERQUERY
= Table.TransformColumns(Source, {
{"CustomerName", each Text.Proper(Text.Trim(Text.Clean(_))), type text}
})
Reflection
Did the AI mention CHAR(160)? Web-scraped data frequently contains non-breaking spaces that standard TRIM() cannot remove. Prompting AI specifically about web data prompts it to include SUBSTITUTE(..., CHAR(160), " ").
Lessons Learned
TRIM+CLEAN+SUBSTITUTEis the classic Excel triple-defense formula for dirty text.- Power Query's
Text.TrimandText.Cleanprovide a more scalable, permanent solution for automated data ingestion.