Sub topic 12
Fuzzy matching for inconsistent keys
This chapter assumes you can run a standard merge, know the join kinds, and have cleaned key columns by trimming and fixing data types. Fuzzy matching handles the cases that cleaning cannot fix, such as typos and variant spellings.
When exact matching fails
A normal merge matches only identical values. If Orders holds "Contoso Ltd" and Customers holds "Contoso Limited" or "Contosso Ltd", those rows stay unmatched. Fuzzy matching compares text by similarity and treats close values as equal.
Try the cleaning steps from the previous chapter first. Trimming spaces and normalizing case fixes many failures at no risk. Use fuzzy matching only for the differences that remain.
Turning it on
In the Merge dialog, select the key column in each table, then tick Use fuzzy matching to perform the merge. Open Fuzzy matching options to change the settings. Fuzzy matching works on text columns only.
| Option | M name | What it does |
|---|---|---|
| Similarity threshold | `Threshold` | A number from 0 to 1. Higher values require closer matches. The default is 0.8. |
| Ignore case | `IgnoreCase` | Treats "ACME" and "acme" as equal. |
| Ignore spaces | `IgnoreSpace` | Ignores spaces when comparing. |
| Maximum matches | `NumberOfMatches` | Limits how many matching rows come back for each source row. |
| Transformation table | `TransformationTable` | A table of From and To pairs that you define, for example "Corp" to "Corporation". |
The M code
The dialog generates Table.FuzzyNestedJoin. The options go in a record at the end of the call.
let
Source = Table.FuzzyNestedJoin(Orders, {"CustomerName"}, Customers, {"CustomerName"}, "Matches", JoinKind.LeftOuter, [IgnoreCase = true, IgnoreSpace = true, Threshold = 0.8])
in
SourceThe result has the same shape as a normal merge. The Matches column holds nested tables, and you expand or aggregate it as usual. Join kinds work the same way, so a left outer join still keeps every Orders row.
Choosing a threshold
The threshold trades missed matches against wrong matches.
- A low threshold, such as 0.5, matches many values, including ones that are not the same entity. "Smith Consulting" could match "Smith Catering".
- A high threshold, such as 0.95, matches only near-identical text and may leave real variants unmatched.
Start at the default of 0.8. Lower it in small steps while you inspect the new matches.
Limits and common errors
- False matches. Fuzzy matching can pair values that merely look alike. Check the matches by eye, especially for short strings, where one changed character is a large share of the text.
- Duplicated rows. One source row can match several rows in the other table. Set
NumberOfMatchesto 1 if you expect a single match. Then compare row counts as you do for any merge. - Slow refresh. Fuzzy comparison costs more than an exact join. On large tables, expect longer refreshes.
- Hidden decisions. Results depend on the threshold, so another person can get different results unless the settings are recorded. Keep the options in the M code.
- Text only. Numeric and date keys cannot be matched fuzzily. Convert them to text only when similarity makes sense for them.
For recurring mismatches, a transformation table is more reliable than a lower threshold. You state the mappings yourself, and nothing is left to the similarity score.
Exercise
A fuzzy left outer merge of Orders to Customers at threshold 0.6 returns more rows than Orders contains. What most likely happened, and what two changes could you try?
Show answer
Some Orders rows matched more than one Customers row, which duplicated them. Set NumberOfMatches to 1 so each order keeps only its best match, or raise the threshold so fewer customers count as similar. Then compare the row count with the original Orders table.
Exercise
Which kind of key suits fuzzy matching: a product code like "PX-204", or a company name typed by hand in several spellings? Explain why.
Show answer
The company name. Its variants differ in small ways but refer to the same entity. A product code identifies exactly one item, so similar codes are different products, and a fuzzy match would give wrong results.