← All challenges

P4 · Power Query · about 20 min

Combine Monthly Files

medium

Batting lines arrive as one file per month in the batting_monthly folder. Someone also saved Batting_summary.csv in the same folder. After Combine Files, total runs are 5,330: exactly double.

Your task

Load every monthly file in the batting_monthly folder under SourceFolder into one table named Batting with columns GameID, PlayerID, Runs and Hits (whole numbers): one row per batting line, from the monthly files only.

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
    Batting compared row by row with the 12 monthly files, matched on GameID and PlayerID.

Stuck?

Open one hint at a time.

Hint 1

Combine Files takes every file the folder query lists. Before combining, look at the Name column of that list.

Hint 2

One of the files has the same columns but holds totals, not batting lines. Keep it out of the list before the combine step.

Hint 3 (near-answer)

In the folder query, filter Name to names that begin with "Batting_20" (or exclude Batting_summary.csv), then Combine Files.

Show the editorial

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

The trap

A folder query combines whatever is in the folder. Batting_summary.csv has the same header as the monthly files, so Combine Files accepts it without complaint, and its monthly totals and grand total are added on top of the real lines: runs double. Next month someone drops another stray file in and it happens again.

The fix

Filter the folder's file list to the monthly files (Name begins with "Batting_20") before combining.

Why it works

The combine step trusts its input list. Filtering on a naming rule keeps the query safe when the folder changes: next month's file matches the pattern and is picked up, anything else is ignored. Totals then come from measures over clean rows.

Before combining a folder, filter the file list to the files you mean. Every file in the list becomes rows.