Sub topic 13
Combine trim, case, dedupe, and split into one repeatable pipeline
This chapter assumes you have already used TRIM, CLEAN, SUBSTITUTE, PROPER, LOWER, UNIQUE, SPLIT, and REGEXREPLACE on their own. You also need to be comfortable with ARRAYFORMULA and with ranges that spill into neighboring cells. Here you chain those steps into one formula. When new rows land in the raw sheet, the cleaned table updates without any manual work.
Why build a pipeline
The menu tools under Data cleanup change cells in place. They work well for a one-off job, but you have to repeat every click when next week's export arrives. A formula pipeline reads from a raw tab and writes to a separate clean tab, so two things follow:
- The raw data is never touched, which keeps the "protect the original" rule from earlier chapters.
- Re-running the cleanup costs nothing. Paste the new export into the raw tab and the clean tab recalculates.
The sample data
Assume a tab named Raw with a header in row 1 and data from row 2:
| Name (A) | Email (B) | Phone (C) |
|---|---|---|
| doe, jANE | [email protected] | (555) 010-1234 |
| Doe, Jane | [email protected] | 555-010-1234 |
| SMITH, Raj | [email protected] | 555 010 9876 |
| (blank row) |
The first two rows describe the same person. The third name contains a non-breaking space (character code 160) that looks like a normal space. The last row is empty, which happens often when a range is longer than the data.
All phone numbers are stored as text. If your export stores them as numbers, leading zeros are already lost before any formula runs. Fix that at import time by formatting the column as plain text.
Decide the order of the steps
The order of steps decides whether the pipeline works. Each step depends on the one before it.
| Order | Step | Why it goes here |
|---|---|---|
| 1 | `SUBSTITUTE`, `CLEAN`, `TRIM` | Hidden characters and extra spaces make equal values look different, so they must go first. |
| 2 | `FILTER` out blank rows | `UNIQUE` would otherwise keep one empty row. |
| 3 | `SPLIT` | Split after trimming, then trim the pieces again. |
| 4 | `PROPER`, `LOWER`, `REGEXREPLACE` | Make each column's format consistent before comparing rows. |
| 5 | `UNIQUE` | Rows can only match once every value is in its final form. |
Running UNIQUE earlier is the most common mistake. "[email protected] " and "[email protected]" are different strings until they are trimmed and lowercased, so UNIQUE keeps both.
Build it in stages
Build the pipeline one step at a time and look at each result before adding the next. On a scratch tab, start with step 1. It handles the whole table in one formula:
=ARRAYFORMULA(TRIM(CLEAN(SUBSTITUTE(Raw!A2:C, CHAR(160), " "))))This returns a 3-column array the same size as Raw!A2:C. SUBSTITUTE swaps non-breaking spaces for normal ones. CLEAN removes non-printing characters. TRIM removes the leading and trailing spaces and collapses repeated ones. Check that row 3 now reads SMITH, Raj with a single normal space.
Step 2 keeps only rows that have an email:
=FILTER(ARRAYFORMULA(TRIM(CLEAN(SUBSTITUTE(Raw!A2:C, CHAR(160), " ")))), Raw!B2:B <> "")The condition range Raw!B2:B has the same number of rows as Raw!A2:C, which FILTER requires. This tests the raw email. A cell holding only spaces would pass the test, so the final version below tests the trimmed email instead.
The single-formula version
LET lets you name each intermediate result, which keeps a long pipeline readable. Put this in Clean!A2 and type the headers Last, First, Email, and Phone in Clean!A1:D1:
=LET(
txt, ARRAYFORMULA(TRIM(CLEAN(SUBSTITUTE(Raw!A2:C, CHAR(160), " ")))),
kept, FILTER(txt, INDEX(txt, , 2) <> ""),
names, ARRAYFORMULA(PROPER(TRIM(SPLIT(INDEX(kept, , 1), ",")))),
email, ARRAYFORMULA(LOWER(INDEX(kept, , 2))),
phone, ARRAYFORMULA(REGEXREPLACE(INDEX(kept, , 3), "[^0-9]", "")),
UNIQUE({names, email, phone})
)Read it from top to bottom:
txtis the fully trimmed table.keptdrops rows whose trimmed email is empty.namessplits the name on commas, trims each piece, and applies proper case. The secondTRIMis needed because splitting on","leaves the space after the comma attached to the first name.emaillowercases the email column.phonestrips every character that is not a digit.- The last line joins the columns with
{ ... }and removes repeated rows.
The result on the sample data is:
| Last | First | Phone | |
|---|---|---|---|
| Doe | Jane | [email protected] | 5550101234 |
| Smith | Raj | [email protected] | 5550109876 |
The two Jane Doe rows became identical after cleaning, so UNIQUE kept one.
Making the pipeline repeatable
- Open-ended ranges.
Raw!A2:Chas no row limit, so rows pasted below the current data are picked up automatically. - Room to spill. The result expands downward and to the right. If any cell in that area is not empty, you get a
#REF!error saying the array result was not expanded. Keep the area below and beside the formula empty, and put nothing else on theCleantab under it. - Replacing the export. Paste new data over the old data in
Raw. Do not delete rows or columns there, because that rewrites the range in the formula to#REF!. - Documenting the logic. Add a note on
Clean!A1that says which tab feeds it and why the order matters. The next person will not guess it.
Failure modes
- Empty input. If no row has an email,
FILTERreturns#N/Abecause there is nothing to return. Wrap the pipeline inIFERROR(..., "")if an empty export is normal for you. Be aware that this also hides real errors. - Names without a comma.
SPLITgives one piece, soFirstis empty for that row. The column count stays the same only because other rows contain commas. If no name has a comma, the array has one name column fewer, and the headers shift. Check the headers after unusual imports. - Near-duplicates survive.
UNIQUEcompares whole rows. Two rows with the same email but different phone formats are only merged afterREGEXREPLACEmakes the phones identical. Two rows with the same email and genuinely different phones stay separate. If email alone defines a duplicate, deduplicate on that column instead and look the other fields up from the result. - Over-aggressive patterns.
[^0-9]also removes a leading+and any extension such asx45. That is acceptable for a plain phone key, but not if you need the country code. - Slow sheets. Whole-column ranges with several nested array functions recalculate on every edit to
Raw. If the sheet becomes slow, limit the range, for exampleRaw!A2:C5000.
Check the result before trusting it
A formula that runs without errors is not necessarily correct. Add a few checks next to the output, for example in F1:F3:
=COUNTA(Raw!B2:B)
=COUNTA(Clean!C2:C)
=COUNTA(Raw!B2:B) - COUNTA(Clean!C2:C)The third number is the count of rows removed as blank or duplicate. For the sample data it is 1: the repeated Jane Doe row. The blank row is already excluded by COUNTA. If that number surprises you, flag the duplicates with a helper column on the raw tab before you decide anything is wrong:
=COUNTIF($B$2:B2, B2) > 1Also scan the output for leftovers: any Phone value with fewer than 10 digits, any Email without @, and any name still in capitals. Sorting each column once and reading the top and bottom rows catches most surprises.
Exercise
In the pipeline, UNIQUE is the last step. A teammate moves it to run on Raw!A2:C before any trimming or case changes, then cleans the result. Rows 1 and 2 of the sample data (the two Jane Doe rows) now both appear in the output. Why?
Show answer
Before cleaning, the two rows are different strings: extra spaces, different capitalization in the email, and different phone formatting. UNIQUE compares exact values, so it sees two distinct rows. After the later steps make them identical, nothing removes the repeat. Deduplicating must come last, after trimming, case, and REGEXREPLACE.
Exercise
Change the pipeline so that it keeps only the first row for each email address, even when the phone numbers differ, while still returning all four columns. Describe the approach in one or two sentences.
Show answer
Build the cleaned columns as before, then apply UNIQUE to the email column alone, for example to get the distinct emails. Use XLOOKUP or VLOOKUP (or FILTER with INDEX) to pull the first name, last name, and phone for each distinct email from the cleaned array. UNIQUE on the full row cannot do this, because it only merges rows that are identical in every column.
Exercise
The Clean tab shows #REF! with the message that the array result was not expanded. What do you check first?
Show answer
Look for any non-empty cell in the range the result would fill, below or to the right of the formula cell. Clear or move that content. Typing in the area under a spilled formula is the usual cause.