← All challenges

P5 · Power Query · about 20 min

Unpivot the Innings

medium

The line score has one column per inning, Inning1 to Inning11, because 44 games went to extra innings. The runs-by-inning chart puts Inning10 between Inning1 and Inning2, and a second version has no extra innings at all.

Your task

Load Innings_wide.csv from the SourceFolder folder as a long table named Innings with columns GameID, Inning and Runs, all whole numbers: one row per game and inning played (an empty cell means the inning was not played).

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.

  • T1One row per inning played
    Innings compared row by row with the file, matched on GameID and Inning: innings 10 and 11 included, innings not played left out.
  • C1Inning is a number
    Innings[Inning] has data type Integer.

Stuck?

Open one hint at a time.

Hint 1

A chart wants one column for the inning and one for the runs. Which Power Query command turns columns into rows?

Hint 2

Unpivot the columns you don't keep, not the ones you can see today: a game with a 12th inning would add a column. Then look at the type of the new Inning column.

Hint 3 (near-answer)

Select GameID, Transform > Unpivot Other Columns, remove rows with empty Runs, replace "Inning" with nothing in Inning, and set Inning and Runs to Whole Number.

Show the editorial

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

The trap

Selecting Inning1 to Inning9 and unpivoting them writes those nine names into the query, so innings 10 and 11 are silently left behind as columns. Unpivoting correctly still leaves Inning as text, and text sorts character by character: Inning1, Inning10, Inning11, Inning2.

The fix

Select GameID, Unpivot Other Columns, drop the empty innings, strip the word "Inning" and type Inning as Whole Number.

Why it works

Unpivot Other Columns names only the column to keep, so any number of inning columns becomes rows, including ones a future file adds. As a whole number, Inning sorts 1, 2 ... 11 and can be filtered as a range. Empty cells are innings not played; removing them keeps counts and averages per inning honest.

Unpivot by naming what stays, not what moves. Then give the new attribute column its real type.