Sub topic 13
Flash Fill and Power Query as Alternatives
This chapter assumes you can already build the nested PROPER(TRIM(CLEAN(...))) formula from earlier chapters and know why SUBSTITUTE is needed for non-breaking spaces. It also assumes you know how to convert formula results to values with Paste Special. Here you will compare that formula approach with two other built-in tools, Flash Fill and Power Query, and learn when each one is the better choice.
Why look at alternatives
Formulas in a helper column are transparent and work in every version of Excel. They have costs, though:
- You need one formula per cleaning rule, and nested formulas become hard to read.
- You must copy the formulas down, then paste values if you want to remove the helper column.
- Each new batch of imported data means repeating the same steps.
Flash Fill and Power Query reduce different parts of that work. Flash Fill is fast for a one-off job. Power Query is better for data that arrives again and again.
Flash Fill: cleaning by example
Flash Fill is a feature that watches you type an example result next to your data, guesses the pattern, and fills the rest of the column. It produces static values, not formulas.
A worked example
Suppose column A has these entries, with irregular spacing and capitalization:
| Cell | Raw value |
|---|---|
| A2 | ` jOHN smith ` |
| A3 | `MARIA gARCIA` |
| A4 | ` li WEI` |
- In B2, type the result you want:
John Smith. - Select B3.
- Press Ctrl+E, or choose Data > Flash Fill.
Excel compares your example with A2, infers a rule, and fills B3 and B4 with Maria Garcia and Li Wei. Flash Fill can also run automatically while you type, if Automatically Flash Fill is turned on under File > Options > Advanced.
What Flash Fill does well
- It handles tasks that would need several functions, such as splitting a full name and reformatting it.
- It needs no formula syntax.
- It is quick to try, and you can undo it with Ctrl+Z.
Where Flash Fill fails
- Results do not update. If A2 changes, B2 stays the same. Flash Fill output is a snapshot.
- It guesses. One example may fit several patterns, and Excel picks one. Entries that differ from your example can come out wrong without any warning.
- It can copy hidden characters. A non-breaking space (character 160) may survive, because Flash Fill works on what it sees as the pattern, not on a guaranteed removal rule. Check the output with
=LEN(B2)or=UNICODE(RIGHT(B2,1))as you did earlier. - It stops when it cannot find a pattern. It may fill nothing, or fill only part of the column.
Power Query: a repeatable cleaning pipeline
Power Query is Excel's data-import and transformation engine. You load a table into the Power Query Editor, apply a list of steps, and load the result to a worksheet. Excel records each step. When the source data changes, you click Refresh and every step runs again.
Doing the three basic cleanups from the ribbon
- Select any cell in your data and press Ctrl+T to make it an Excel table. Name it
RawNameson the Table Design tab. - Choose Data > From Table/Range. The Power Query Editor opens.
- Select the column, then use the Transform tab:
- Format > Trim removes leading and trailing whitespace.
- Format > Clean removes non-printing characters.
- Format > Capitalize Each Word applies proper-case capitalization.
- Choose Home > Close & Load. The cleaned table appears on a new worksheet, and your original data is untouched.
The ribbon commands do not behave exactly like the worksheet functions, as the table below shows.
| Task | Worksheet function | Power Query function | Difference |
|---|---|---|---|
| Remove extra spaces | `TRIM` | `Text.Trim` | `TRIM` also collapses runs of spaces inside the text. `Text.Trim` removes only leading and trailing whitespace. |
| Non-breaking spaces | Needs `SUBSTITUTE(A2,CHAR(160)," ")` | `Text.Trim` treats them as whitespace at the ends | Non-breaking spaces between words still need a replacement step. |
| Control characters | `CLEAN` | `Text.Clean` | Both remove line feeds and tabs without adding a space, so words on separate lines can join. |
| Capitalization | `PROPER` | `Text.Proper` | Both can mishandle names and abbreviations, so check the results. |
The first row matters most. If your data has double spaces between words, the Trim button alone will not fix them.
Writing the full cleanup as a query
You can reproduce the whole formula chain in one query. In the Power Query Editor, choose Home > Advanced Editor and replace the contents with this code. It assumes a table named RawNames with a text column named Name.
let
Source = Excel.CurrentWorkbook(){[Name = "RawNames"]}[Content],
CleanText = (t as nullable text) as nullable text =>
if t = null then null
else
let
Spaced = Text.Replace(
Text.Replace(
Text.Replace(
Text.Replace(t, "#(00A0)", " "),
"#(lf)", " "),
"#(cr)", " "),
"#(tab)", " "),
Words = List.Select(
Text.Split(Text.Clean(Spaced), " "),
each _ <> ""),
Joined = Text.Proper(Text.Combine(Words, " "))
in
Joined,
Result = Table.TransformColumns(Source, {{"Name", CleanText, type text}})
in
ResultHere is what each step does:
Text.Replacecalls turn non-breaking spaces, line feeds, carriage returns, and tabs into normal spaces. This is the same idea as theSUBSTITUTEformulas from the earlier chapters.#(00A0)is how M writes character 160.Text.Splitcuts the text at each space.List.Selectdrops the empty pieces that extra spaces create.Text.Combinerejoins the words with a single space. Together these steps do the job ofTRIM.Text.Cleanremoves any remaining control characters.Text.Propercapitalizes each word.
Close & Load, and the cleaned names appear in a new table. The first time a new batch of data arrives, paste it into the RawNames table and choose Data > Refresh All. No formulas need to be copied and nothing needs to be pasted as values.
A formula alternative in Microsoft 365
If you have dynamic arrays, a single formula can clean a whole range and spill the results:
=PROPER(TRIM(CLEAN(SUBSTITUTE(A2:A101,CHAR(160)," "))))This removes the copy-down step, but the results are still formulas that depend on column A. Older versions of Excel do not support spilling.
Choosing between the three approaches
Use these criteria to decide:
| Criterion | Formulas | Flash Fill | Power Query |
|---|---|---|---|
| Result updates when the source changes | Yes, until you paste values | No | Yes, on refresh |
| Predictable behavior | High, rules are explicit | Lower, pattern is guessed | High, steps are explicit |
| Setup effort for a one-off job | Low to medium | Lowest | Highest |
| Effort for repeated jobs | Medium, copy and paste each time | High, redo each time | Lowest after setup |
| Works in older Excel versions | Yes | Excel 2013 and later | Built in from Excel 2016, add-in for 2010 and 2013 |
| Leaves original data unchanged | Yes, if you use a helper column | Yes, if you use a new column | Yes, output goes to a separate table |
Edge cases to remember
- Numbers that stay text. Flash Fill and
Text.*functions return text. If cleaned values such as"1234"should be numbers, convert them afterward. In Power Query, change the column type with the data-type button in the column header. In a worksheet, useVALUEor multiply by 1, as in the earlier chapter. - Proper-case mistakes.
Text.ProperandPROPERfollow similar capitalization rules, so names like McDonald or abbreviations like USA still need a manual or targeted fix. Add aText.Replacestep in the query, or aSUBSTITUTEwrapper in the worksheet, but check what else it changes. - Unknown hidden characters. If
TRIMandCLEANdo not fix a value, diagnose it with=UNICODE(MID(A2,n,1))for each suspect position, then add the right replacement. This applies to every method.
Summary
- Flash Fill infers a pattern from your example. It is quick, but its results are static and it can guess wrong.
- Power Query records cleaning steps and reapplies them on refresh. The ribbon commands differ slightly from
TRIMandCLEAN, so a custom query is needed to match them fully. - Formulas stay the clearest choice when the rules are exact and the job is a one-off.
- Whichever method you choose, check the output for hidden characters and wrong capitalization before you use it in lookups or comparisons.
Exercise
Your sales team sends a new customer list every Monday. Names arrive with double spaces, non-breaking spaces, and mixed capitalization, and a lookup against your master list must work every week. Which method fits best, and why would Flash Fill be a poor choice? Name one step you must include in the Power Query version that the ribbon's Trim button alone does not do.
Show answer
Power Query fits best because you build the steps once and click Refresh each Monday. Flash Fill output is static, guesses the pattern from examples, and must be redone for every new list. The query must collapse runs of spaces between words, which Text.Trim does not do, for example by splitting on spaces, dropping empty items, and recombining with single spaces. It must also replace character 160 with a normal space before the lookup.
Exercise
In B2 you typed the example Anna Lee for the raw value aNNA lee . After Flash Fill, you suspect some rows still contain a hidden trailing character. Write one formula that returns the character code of the last character in B3.
Show answer
=UNICODE(RIGHT(B3,1)). A result of 160 indicates a non-breaking space that Flash Fill left in place.