Sub topic 12
Putting it together: cleaning a column of messy data
This chapter assumes you can enter formulas, reference cells and ranges, and use basic functions such as TRIM and IFERROR. You will combine SPLIT, JOIN, and REGEXEXTRACT to turn inconsistent text into usable columns.
A cleaning workflow
Suppose column A contains imported customer notes. A cell might contain tags, an order ID, and an email address:
| A: Raw note |
|---|
| `urgent; billing ; follow-up ORD-10492 contact [email protected]` |
The values are mixed together, separated by inconsistent punctuation and spaces. A useful workflow is:
- Split lists into individual pieces.
- Trim whitespace from each piece.
- Remove empty pieces.
- Join cleaned pieces into one consistent string when needed.
- Extract IDs or email addresses with a regular expression.
- Convert extracted numbers from text to numeric values.
Clean a delimited list
If A2 contains urgent; billing ; follow-up, use SPLIT to create separate cells:
=SPLIT(A2, ";")Each result may contain extra spaces. To clean the pieces and put them back into one standard list, combine SPLIT, TRIM, and JOIN:
=IF(A2="", "", JOIN(", ", FILTER(ARRAYFORMULA(TRIM(SPLIT(A2, ";"))), ARRAYFORMULA(TRIM(SPLIT(A2, ";")))<>"")))This formula does the following:
SPLIT(A2, ";")separates the tags.TRIMremoves spaces around each tag.FILTER(..., ...<>"")removes blank pieces caused by repeated delimiters.JOIN(", ", ...)creates one comma-and-space-separated result.IFkeeps an empty input empty instead of producing an unnecessary result.
For example, the result is urgent, billing, follow-up.
FILTER is important when the input can contain values such as urgent;;billing. Without it, the empty item may remain in the joined output.
Extract fields from the same text
Use REGEXEXTRACT when the value you need follows a recognizable pattern. To extract the order number from A2, use a capture group:
=IFERROR(REGEXEXTRACT(A2, "ORD-(\d+)"), "")The pattern means:
ORD-matches those literal characters.(\d+)captures one or more digits.- Parentheses define the part returned by the formula.
To extract the email address instead:
=IFERROR(REGEXEXTRACT(A2, "[\w.+-]+@[\w-]+\.[\w.]+"), "")IFERROR returns an empty string when a row has no matching order ID or email. This keeps a clean output column instead of displaying #N/A.
Convert extracted values
REGEXEXTRACT always returns text, even when the match contains only digits. Wrap it in VALUE when you need arithmetic or numeric sorting:
=IFERROR(VALUE(REGEXEXTRACT(A2, "ORD-(\d+)")), "")The result from ORD-10492 is the number 10492, not the text "10492".
Apply formulas to many rows
You can copy the cleaning formulas down alongside the source data. For simple splitting, ARRAYFORMULA can process a range:
=ARRAYFORMULA(SPLIT(A2:A10, ","))This produces one result row for each input row. Make sure the cells where results will spill are empty. A value in any destination cell blocks the result and causes a spill error.
Common mistakes
- Use the correct delimiter. A comma and a comma followed by a space are different choices.
- Use
FALSEforsplit_by_eachwhen a multi-character delimiter must be treated as one unit. - Trim after splitting, not only before it. Each piece can have its own surrounding spaces.
- Escape special regular expression characters when matching literal punctuation.
- Check that the output area is empty before using an array formula.
Exercise
Write a formula that extracts the digits after ORD- from A2, converts them to a number, and returns a blank when the pattern is missing.
Show answer
=IFERROR(VALUE(REGEXEXTRACT(A2, "ORD-(\d+)")), "")