7. Data Cleaning A–Z in Power Query
7.12 Extract: Length, First/Last Characters, Range and Delimiters
Transform › Extract replaces the column; Add Column › Extract creates a new one. Examples on our data:
| Extract option | Input | Output | M function |
|---|---|---|---|
| Length | Kolhapuri Misal Masala | 22 | Text.Length |
| First Characters (3) | BLK-10001 | BLK | Text.Start([Order ID], 3) |
| Last Characters (5) | BLK-10001 | 10001 | Text.End([Order ID], 5) |
| Range (start 4, 3 chars) | BLK-PUN-KOT-01 | PUN | Text.Middle([Store ID], 4, 3) |
| Text Before Delimiter "@" | shraddha.bagale@example.com | shraddha.bagale | Text.BeforeDelimiter |
| Text After Delimiter "@" | shraddha.bagale@example.com | example.com | Text.AfterDelimiter |
| Text Between Delimiters "(" ")" | Blinkit Baner (Pune) | Pune | Text.BetweenDelimiters |
Steps in Power BI – extract City Code from Store ID
- Select Store ID.
- Add Column › Extract › Range.
- Starting Index:
4, Number of Characters:3› OK. (Power Query counts from 0, so index 4 is the 5th character.) - Rename the new column to City Code.
- For delimiters, open Advanced options to choose Scan for the delimiter From the start / From the end and how many delimiters to skip (for example the text after the last "-").
AddCode = Table.AddColumn(Source, "City Code", each Text.Middle([Store ID], 4, 3), type text),
LastPart = Table.AddColumn(AddCode, "Store No", each Text.AfterDelimiter([Store ID], "-", {0, RelativePosition.FromEnd}), type text)
Ravindra Bagale's Tip
Remember one thing: using Range or First Characters on codes whose length varies ("BLK-1" and "BLK-10001") gives wrong results on some rows. Delimiter-based extraction (Text Before/After Delimiter) is more robust. After extracting, always scroll through a sample of rows, not just the first five. Clear?
Ravindra Bagale's Tip – मराठी
एक गोष्ट लक्षात ठेवा: लांबी बदलणाऱ्या codes वर ("BLK-1" आणि "BLK-10001") Range किंवा First Characters वापरलं तर काही rows वर चुकीचे results येतात. Delimiter वर आधारित extraction (Text Before/After Delimiter) जास्त भरवशाचं आहे. Extract केल्यानंतर नेहमी फक्त पहिल्या पाच नाही, तर rows चा एक sample scroll करून बघा. समजलं का?
Ravindra Bagale's Tip – हिंदी
एक बात याद रखो: अलग-अलग लंबाई वाले codes ("BLK-1" और "BLK-10001") पर Range या First Characters लगाने से कुछ rows पर गलत results आते हैं. Delimiter पर आधारित extraction (Text Before/After Delimiter) ज़्यादा भरोसेमंद है. Extract करने के बाद हमेशा सिर्फ़ पहली पाँच नहीं, rows का एक sample scroll करके देखो. समझ आया?
Practice task
From Customer e-mails, extract the domain (Text After Delimiter "@"). From Product Name "Amul Butter 100 g", extract the text after the last space (the unit).