Wide vs Long Tables: How to Reshape Data

At some point every analyst hits the same wall: “This table looks fine… why is it so hard to group, filter, or chart?” Usually the problem is table shape, not the data itself.

1. The Two Main Shapes

  • Wide → many columns, each representing a category or period (e.g., sales_Q1, sales_Q2, sales_Q3).
  • Long → fewer columns, more rows, with a 'type' column (e.g., quarter, region, metric).

2. When Wide Tables Help

Wide is useful when you:

  • Want side-by-side comparison in a single row.
  • Are building human-friendly reports or exports.
  • Have a small, fixed set of periods or categories.

3. When Long Tables Win

Long is usually better when you:

  • Need flexible GROUP BY and aggregation across many values.
  • Expect new categories or periods to be added over time.
  • Are feeding the data into BI tools or visualisations.

4. Same Idea Across SQL, Python, and Excel

  • SQL → use CASE + SUM for wide layouts; UNION or unpivot patterns for long.
  • Python (pandas) → use pivot_table/unstack for wide; melt/stack for long.
  • Excel → use PivotTables and Power Query for pivot/unpivot logic.

Once you recognise shape problems, many ‘hard’ queries become simple: you reshape first, then aggregate or visualise.

Try it yourself

Pivoting with CASE WHEN is Level 23, in Chapter 4: Shaping data.

The first 5 levels are free · no sign-up needed