Fill One Cell, Guess the Column: How Excel's Flash Fill Works
From a column of Pinyin names, how Excel's Flash Fill turns examples into programs and picks a winner from thousands of candidates.
Converting a column of Pinyin names into "Given.Surname" format: Zhang San becomes San.Zhang, Li Si becomes Si.Li, and Wang Wu becomes Wu.Wang.
Next in line is Chen Xiao Ming.
A human filling in the sheet immediately recognizes the person as Chen Xiaoming, intending Xiao Ming.Chen. But if you type the first three rows in Excel and press Ctrl+E, the program will most likely produce Ming.Chen Xiao. Because every preceding name had exactly two words, extracting "the second word" versus "the last word", or keeping "the first word" versus "all prefix words before the last", yields identical results on the first three rows.
Excel's Flash Fill faces this situation every time. Type an edited result next to a name, start typing the next cell, and Excel offers a ghosted preview of the entire column—or you press Ctrl+E. It has no concept of what a "first name" or "last name" is; it only holds two strings: the input text and the answer you typed. Introduced in Excel 2013, the feature rests on program synthesis research led by Sumit Gulwani and his team at Microsoft Research.
Each Example Crosses Out Programs
Looking at just the first cell: from Zhang San to San.Zhang, there are at least three explanations:
- Copy the literal string
San.Zhang; - Take from the seventh character to the end, insert a dot, then append the first five characters;
- Take the last word, insert a dot, then append the first word.
On Zhang San, all three produce the exact same text.
Moving to Li Si, the literal rule still yields San.Zhang. The positional rule fails to find a seventh character (Li Si, including the space, is only five characters long). Only the word-based rule survives.
Inferring programs from input-output examples is called Programming by Example (PBE). Each example acts as a constraint. All programs that satisfy an example form a set; the intersection of these sets holds the programs consistent across all examples. Si.Li eliminates any program assuming the space sits at character six. Wu.Wang shares the same shape as Si.Li, so any program it could cross out was largely eliminated already.
Working Backward from the Output
Where do candidate programs come from? In his 2011 paper, Gulwani restricted programs to a handful of primitives. ConstStr inserts fixed text. SubStr extracts a slice from the input, located either by character offset (CPos) or by regular expression context (Pos, looking for "preceded by pattern A, followed by pattern B"). Concatenate strings these segments together.
Combined, they form a Domain-Specific Language (DSL) tailored specifically for string manipulation. Keeping the language narrow is intentional: the more expressive a language is, the larger the search space of conforming programs becomes, making it exponentially harder to isolate the intended one (Section 3).
In this DSL, "take the last word, append a dot, then take the first word" looks roughly like this:
Concatenate(
SubStr2(v1, AlphTok, -1),
ConstStr("."),
SubStr2(v1, AlphTok, 1)
)Here v1 represents the input cell, AlphTok matches a contiguous sequence of alphabetic letters, and SubStr2(v1, AlphTok, -1) picks the first word from the right—the last word. Replacing -1 with 2 yields "take the second word".
The algorithm works backward from the output string. The dot in San.Zhang is absent in the input, so it must stem from ConstStr("."). The substring San on the left has multiple sources: the last word, the second word, character seven to the end, everything following the first space, or a literal string constant. Zhang can likewise be sliced in several ways. The algorithm can also consider different segmentations, such as splitting S and an, or treating the whole output as a constant string.
The combinations multiply rapidly. In their 2015 paper, Rishabh Singh and Gulwani noted that from just one example—Roger to Mr. Roger—Flash Fill induces over 10 million conforming programs.
Storing them individually is infeasible. The 2011 paper models character boundaries of the output as nodes. San.Zhang has nine characters, giving ten nodes from start to end. An edge between two nodes holds all candidate expressions generating that slice of text. Every path from start to end represents a candidate program formed by concatenating expressions chosen along the edges. All derivations of San live on a single edge and are reused regardless of what follows (Section 4.1).
When the second example arrives, the algorithm computes the intersection of the two graphs, much like intersecting finite automata: pairing nodes and taking the intersection of expression sets on edges. Any derivation relying on "the space is at position six" vanishes during this step.
Maintaining all hypotheses consistent with observations and narrowing them with each new example is known in machine learning as version space learning. First introduced by Tom Mitchell for boolean concepts, it was later extended into Version Space Algebra by Tessa Lau and colleagues to learn programs from demonstrations. Gulwani adapted this framework to learning from input-output examples alone (Section 7).
Who Remains in the Intersection?
After three examples, the intersection has not collapsed into a single program. On Chen Xiao Ming, different programs diverge completely:
| Input | Rule A (2nd word + 1st word) | Rule B (Last word + 1st word) | Rule C (After space + 1st word) | Rule D (Last word + prefix) |
|---|---|---|---|---|
Zhang San | San.Zhang | San.Zhang | San.Zhang | San.Zhang |
Li Si | Si.Li | Si.Li | Si.Li | Si.Li |
Wang Wu | Wu.Wang | Wu.Wang | Wu.Wang | Wu.Wang |
Chen Xiao Ming | Xiao.Chen | Ming.Chen | Xiao Ming.Chen | Ming.Chen Xiao |
In a Microsoft Research article, Gulwani observed that the central challenge is that examples are inherently ambiguous. Taking the name of Rick Rashid, then Microsoft Research's chief research officer: if you convert Rick Rashid into Rashid, R., does that R come from Rick or Rashid?
Excel can only fill one string per cell, so it must pick a single winner. The 2011 paper handcrafted heuristics based on Occam's razor: prefer fewer concatenated parts, prefer extracting substrings over inserting constant strings, and prefer regular expression matches over fixed character offsets (Section 5.3). The paper acknowledged that some choices were subjective.
Handcrafted rules struggle when identical patterns demand opposite behaviors. The 2015 paper illustrated this with two cases.
First, adding a prefix title: converting Roger to Mr. Roger. M is not in the input, so it must be constant. But r matches the trailing character of Roger. If treated as an extracted substring, the next row Simon becomes Mn. Simon. Here, a single-character match should be treated as constant text.
Second, name initials: converting Mark Sipser to M.Sipser. M could also be treated as a constant string, but then Louis Johnson becomes M.Johnson instead of L.Johnson. Here, a single-character match must be extracted from the input.
The paper pointed out that these two scenarios are directly in conflict.
The clues to resolve them lie in subtler signals. In M.Sipser, M is capitalized, preserves the input casing, and "first capital letter" is a frequent extraction pattern in practice. In Mr., r is just one character, takes up only one-ninth of the output, and is flanked by constant characters. The 2015 paper formalized over thirty such features, scoring candidate programs via a learned linear ranking function (Section 2, Section 5.1).
The training corpus comprised 175 tasks from the Excel team and help forums, each with five examples. A program that fits one example but fails on others within the same task was labeled negative; one that fits all was labeled positive. The optimization goal was forgiving: it did not require ranking all positive programs ahead of negative ones, only that at least one positive program ranked above all negative ones, since any correct program produces the intended outputs (Section 4).
In a 30–70 train-test split, across 123 benchmark tasks, the learned ranking solved 91 tasks (74%) using just a single example—compared to zero for the baseline heuristic. Requiring all five examples dropped from 72 tasks under the baseline to just 4 tasks under the learned ranker (Section 5.2). The average number of examples needed fell from 4.17 to 1.48.
If you test this in Excel today, providing the first three rows and pressing Ctrl+E, the output for Chen Xiao Ming is typically Ming.Chen Xiao (Rule D). Under its ranking model, moving the last word to the front and preserving the remaining prefix after the dot scored highest. Lacking human common sense, the engine bets on the statistically favored pattern.
Which Row Breaks the Ambiguity?
Ranking bets on an answer when examples fall short. But version space can be used in another way: run the surviving candidate programs across all rows to see how many distinct outputs each row yields, without enumerating the programs individually. For the first three rows, every candidate produces identical outputs. On Chen Xiao Ming, the candidates branch into four possibilities.
In the 2011 paper, the prototype add-in for Excel 2010 (then called QuickCode) worked like this: if a cell had only one possible evaluation, it filled it in; if it had multiple, it populated the highest-ranked one and highlighted the cell for inspection; if it had none, it filled ?? (Section 5.1, Section 6.1). The official Microsoft Flash Fill help documentation mentions no such visual indicator in Excel.
Providing another example on a row like Wang Wu eliminates almost nothing. The row that matters is Chen Xiao Ming. The moment that cell is supplied as Xiao Ming.Chen, Rules A, B, and D are eliminated at once.
Excel still does not know what a given name is. It only learns that the segment before the dot starts from the second word and spans to the end, and the segment after the dot takes only the first word.
