Power Query Intermediate Lesson 5: Custom Columns in M – Clean Text, Digits and try … otherwise

Power Query Intermediate Lesson 5: Custom Columns in M – Clean Text, Digits and try … otherwise featured illustration

πŸ“Ž This article includes 3 downloadable practice files ↓

⏱ 1 min read

πŸ“˜ Power Query Intermediate Course Β· Lesson 5 of 8

Advertisement

The ribbon covers common clean-ups. For anything else, Add Column > Custom Column with a little M.

Need M
Proper Case, trimmed Text.Proper(Text.Trim([Name]))
Digits only Text.Select([Phone], {"0".."9"})
Last 10 digits Text.End(d, 10)
Remove spaces Text.Remove([Pincode], {" "})
Number or blank try Number.From(x) otherwise null
Mobile = Table.AddColumn(Name, "Mobile", each
    let d = Text.Select([Phone], {"0".."9"})
    in if Text.Length(d) >= 10 and List.Contains({"6","7","8","9"}, Text.Start(Text.End(d, 10), 1))
       then Text.End(d, 10) else null, type text)

Indian mobile numbers start with 6-9, so the landline (0484) 2345678 correctly gives null.

⚠️ Our first version read “Rs. 2,250” as 0.225: keeping digits and dots kept the dot from “Rs.”, which became a decimal point. The fix removes “Rs.” first: Text.Select(Text.Replace([Amount Text], "Rs.", ""), {"0".."9", "."}). Always check a few converted values against the source.

Practice

Unzip the source files (e.g. to D:\PQ\). Try to build the query with the ribbon first, then compare with the M solution and the expected result. Every solution was run by Excel’s own Power Query engine before publishing.

Advertisement

πŸ“Ž Practice files for this article

⬇ Download all 3 files (ZIP Β· 8 KB)

  • πŸ—‚οΈ
    Practice source files (zip)All messy CSVs for the course: invoices, payments, typed customer names, credit history, contacts, branch files, a two-row-header report and an ERP register.
    ⬇ ZIP Β· 3 KB
  • πŸ“„
    Lesson 5 M solutionThe full query. Paste into Advanced Editor and change the Folder line.
    ⬇ PQ Β· 1 KB
  • πŸ“—
    Expected resultWhat the query returned when Excel ran it, to compare with yours.
    ⬇ XLSX Β· 5 KB

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong

Leave a Comment