A small workshop receives a contact export. Names and region codes contain stray spaces, empty strings, and true nulls.
Your task
Produce one clean row per contact and a NameState column showing what was missing before cleaning.
The rules
- Keep all eight rows and keep ContactID as text, including leading zeros.
- Classify the original Name: null becomes Null; empty or whitespace-only text becomes Blank; other text becomes Present.
- Trim only the beginning and end of Name and Region. Preserve internal spaces, apostrophes, and spelling.
- Convert cleaned empty text to null. Uppercase the cleaned Region.
- Return ContactID, Name, Region, NameState sorted by ContactID.
Start the query
Open a blank query, open Advanced Editor, and replace its contents with the starter below. It includes the exact source data, so you need no file paths. Apply your transformations before opening the solution.
In the optional raw CSV, <NULL> represents a true null and an empty field represents empty text. The embedded starter preserves both exactly.
Show the starter M query
// Original synthetic practice data. Paste the entire query into Advanced Editor.
let
Source = #table(type table [ContactID = text, Name = nullable text, Region = nullable text], {
{"0001", " Mina Patel ", " NORTH "},
{"0002", "", "south"},
{"0003", null, " EAST"},
{"0004", " ", "west "},
{"0005", "#(tab)Ari Chen#(tab)", " north#(tab)"},
{"0006", "Sam Lee", "south"},
{"0007", " Jo O'Neil", ""},
{"0008", "Ren Diaz ", null}
})
in
Source
Need a nudge?
Use one hint, then try another step before revealing the next.
Hint 1
Start with the missing-value flag before changing the original Name.
Hint 2
Text.Trim removes surrounding whitespace; an empty string still differs from null.
Hint 3
Use a nullable-text helper: return null for null or a trimmed empty string; otherwise return the trimmed value.
Reveal the worked solution
Why these steps work
- Add NameState from the raw Name so cleaning does not erase whether a missing value was null or blank.
- Write a reusable nullable-text cleaning function so both columns follow the same blank policy.
- Apply the helper to Name and Region. Keep identifiers out of numeric conversion so 0001 remains 0001.
- Uppercase Region to remove case differences while leaving names unchanged.
- Sort on ContactID and check state counts; unchanged row count proves this is cleaning rather than filtering.
Common trap: Replacing every blank with null before adding NameState destroys the evidence needed to distinguish the two cases.
// Reference solution: documented M; not executed in a native Power Query engine.
let
Source = #table(type table [ContactID = text, Name = nullable text, Region = nullable text], {
{"0001", " Mina Patel ", " NORTH "},
{"0002", "", "south"},
{"0003", null, " EAST"},
{"0004", " ", "west "},
{"0005", "#(tab)Ari Chen#(tab)", " north#(tab)"},
{"0006", "Sam Lee", "south"},
{"0007", " Jo O'Neil", ""},
{"0008", "Ren Diaz ", null}
}),
Flagged = Table.AddColumn(Source, "NameState", each if [Name] = null then "Null" else if Text.Trim([Name]) = "" then "Blank" else "Present", type text),
Clean = (s as nullable text) as nullable text => if s = null then null else if Text.Trim(s) = "" then null else Text.Trim(s),
Trimmed = Table.TransformColumns(Flagged, {{"Name", Clean, type nullable text}, {"Region", Clean, type nullable text}}),
Upper = Table.TransformColumns(Trimmed, {{"Region", each if _ = null then null else Text.Upper(_), type nullable text}}),
Result = Table.Sort(Upper, {{"ContactID", Order.Ascending}})
in
Result
Expected output
Compare all values and counts. Here, null means a missing value. Internal spaces are preserved.
| ContactID | Name | Region | NameState |
|---|---|---|---|
| 0001 | Mina Patel | NORTH | Present |
| 0002 | null | SOUTH | Blank |
| 0003 | null | EAST | Null |
| 0004 | null | WEST | Blank |
| 0005 | Ari Chen | NORTH | Present |
| 0006 | Sam Lee | SOUTH | Present |
| 0007 | Jo O'Neil | null | Present |
| 0008 | Ren Diaz | null | Present |
- 8 output rows; NameState counts: Present 5, Blank 2, Null 1.
- 0001 becomes Mina Patel / NORTH; 0006 remains Sam Lee with two internal spaces.
- 3 cleaned names and 2 cleaned regions are null.
Try one change: Add a contact whose name is only a tab. Predict its NameState before you refresh. Then explain why it differs from a true null.
Reference outputs are independently calculated in Python; M syntax is checked with Microsoft’s parser. These queries have not been run in a native Excel or Power BI engine.
Microsoft documentation: Text.Trim · Table.TransformColumns

