← All challenges

P6 · Power Query · about 25 min

Merge Without Duplicating

hard

Batting needs each player's name, and the only source is a lookup file from the scouting team. After the merge, Batting has 5,468 rows instead of 5,400 and one player's runs are counted twice. Nobody changed the batting data.

Your task

Add a Name column to the Batting table by merging it with Players_lookup.csv from the SourceFolder folder. Batting must keep exactly one row per batting line (GameID, PlayerID), with the player's name.

What the grader checks

You see the situations, not the expected numbers. The expected values are computed live from your file and the source data.

  • T1Every batting line once, with a name
    Batting compared row by row with the batting lines, matched on GameID and PlayerID: no line duplicated, every line named.

Stuck?

Open one hint at a time.

Hint 1

A merge returns every matching row from the other table. Count the rows before and after the merge: which batting lines multiplied?

Hint 2

Look for a PlayerID that appears more than once in the lookup. A lookup key must be unique before you merge on it.

Hint 3 (near-answer)

In the lookup query, keep PlayerID and Name and Remove Duplicates on PlayerID. Then merge Batting with it on PlayerID and expand Name.

Show the editorial

Solve it first if you can: the editorial gives the answer away.

The trap

A merge is a join: each batting line is repeated once for every lookup row with the same PlayerID. The scouting file lists a traded player twice, once per team, so each of his batting lines became two. Row counts and runs grow, and nothing warns you: the merge did exactly what it was asked.

The fix

Make the lookup unique on its key first: keep PlayerID and Name, Remove Duplicates on PlayerID, then merge and expand Name.

Why it works

With one lookup row per PlayerID, each batting line finds exactly one match, so the merge adds a column without adding rows. The player's name is the same in both rows, so which one Remove Duplicates keeps doesn't matter here; for a column that differs (like Team), decide which row is right before deduplicating.

Before you merge, prove the lookup key is unique. A merge on a duplicated key multiplies rows silently.