01 / 20

Pivoting Data
Wide ↔ Long

Learn how to reshape your data on the Tableau Data Source page: convert wide-format tables into long-format tables and back again.

Tableau

What Is Pivoting?

Pivoting is the act of reshaping a table so that column headers become row values or vice-versa. In Tableau, you pivot directly on the Data Source page without writing any code.

Wide Format
Each measure gets its own column: Q1, Q2, Q3, Q4. Fewer rows, many columns.
Long Format
One column for the measure name, another for the value: Quarter + Sales. Many rows, fewer columns.

Why Does Format Matter?

Tableau works best with long (tidy) data. When your data is wide, you cannot easily drag a single field onto Color or Rows to compare categories. Pivoting solves this instantly.

The Visual Transformation

WIDE FORMAT Product Q1 Q2 Q3 Widget A 100 150 200 Widget B 80 120 90 PIVOT LONG FORMAT Product Quarter Sales Widget A Q1 100 Widget A Q2 150 Widget A Q3 200 Widget B Q1 80 Widget B Q2 120 Widget B Q3 90 2 rows, 4 columns 6 rows, 3 columns Column headers become row values Pivot Field Names → Quarter  |  Pivot Field Values → Sales

The Pivot Rule

When you select columns and pivot them in Tableau, two new fields are created:

Pivot Field Names
Contains the original column headers as row values. This becomes a dimension.
Pivot Field Values
Contains the cell values from those columns. This becomes a measure.

Wide vs. Long: Key Differences

Wide Format Long Format
Structure One column per measure One row per observation
Columns Many (grows with categories) Few (fixed structure)
Rows Fewer More (grows with categories)
Tableau Friendly? Often needs pivoting Ready to use
Use Case Spreadsheet summaries Visualization, analysis

Recognizing Wide Data

Before you pivot, you need to spot the problem. Here are the telltale signs:

1
Repeating pattern in headers: Column names follow a sequence: months, years, quarters, categories.
2
Same data type across columns: Multiple columns all contain the same kind of value (e.g., all dollar amounts).
3
You want to compare by color or filter: If you need "Year" on Color but each year is a separate column, you must pivot.

How to Pivot in Tableau

1
Connect to your data source and navigate to the Data Source page (bottom-left tab).
2
Select the columns you want to pivot. Hold Ctrl (or Shift) and click each column header.
3
Right-click any selected header and choose "Pivot" from the context menu.
4
Rename the generated fields: "Pivot Field Names" and "Pivot Field Values" to meaningful names (e.g., "Quarter" and "Sales").
5
Verify by scrolling through the preview grid. Each original column header now appears as a row value.

Example 1: Monthly Sales

A spreadsheet tracks revenue per store per month. The columns are Store, Jan, Feb, Mar.

BEFORE (Wide) Store Jan Feb Mar North 500 600 550 South 400 450 480 PIVOT AFTER (Long) Store Month Revenue North Jan 500 North Feb 600 North Mar 550 South Jan 400 South Feb 450 South Mar 480
\( 2 \) rows × \( 3 \) months
= \( 6 \) rows in long format

Example 2: Survey Scores

A survey file has columns: Student, Math Score, Science Score, English Score. You want to compare scores by subject on a single bar chart.

1
Select the three score columns: Math Score, Science Score, English Score.
2
Right-click and choose Pivot.
3
Rename "Pivot Field Names" to Subject and "Pivot Field Values" to Score.
4
Drag Subject to Columns, Score to Rows, and Student to Color for a grouped bar chart.

Example 3: Multi-Year Census

A government dataset has columns: County, State, Pop_2010, Pop_2015, Pop_2020. You want a line chart of population over time.

1
Do NOT pivot the County or State columns: these are identifiers, not repeated measures.
2
Select only Pop_2010, Pop_2015, Pop_2020 and pivot them.
3
Rename Pivot Field Names to Year and Pivot Field Values to Population.
4
Clean up the Year field: values will read "Pop_2010" etc. Use Aliases or a calculated field to extract just the year number.

When Not to Pivot

Not every multi-column dataset needs pivoting. Here are cases where your data is already structured correctly:

Different Measures
Columns like Revenue, Profit, Cost represent different measures, not one measure repeated. Do not pivot.
Already Long
If your data already has one row per observation with a category column, pivoting would break the structure.
Calculated Fields
If you only need one or two fields from wide columns, a calculated field may be simpler than a full pivot.

Undoing a Pivot

If you pivot the wrong columns or change your mind, you can reverse the operation easily.

1
Look for the pivot icon (two small arrows) on the pivoted field headers in the Data Source grid.
2
Click the dropdown arrow on any pivoted field header.
3
Select "Remove Pivot" from the menu. The data reverts to its original wide format.

The Row Count Formula

Understanding how many rows you will get after pivoting helps you verify the result is correct.

Example A
\( 10 \) rows × \( 4 \) quarters = 40 rows
Example B
\( 50 \) rows × \( 12 \) months = 600 rows
Example C
\( 100 \) rows × \( 3 \) subjects = 300 rows

Common Mistakes

✕ Pivoting Identifier Columns
Selecting columns like "Name" or "ID" along with the measure columns creates a meaningless pivot. Only pivot repeated measures.
✕ Not Renaming Fields
Leaving "Pivot Field Names" and "Pivot Field Values" makes your viz confusing. Always rename to meaningful labels.
✕ Pivoting When Not Needed
If columns are truly different measures (Revenue, Profit, Cost), pivoting combines them into one field incorrectly.

Summary: Pivoting at a Glance

Concept Details
What it does Converts column headers into row values
Where to do it Data Source page (not a worksheet)
Creates Pivot Field Names + Pivot Field Values
Row formula \( R_{\text{original}} \times C_{\text{pivoted}} \)
Undo method Dropdown arrow on field header → Remove Pivot
When to use Repeated measure names across column headers
When NOT to use Columns are different measures or data is already long

Question 1 of 5

A dataset has 5 product rows and columns: Product, Jan, Feb, Mar, Apr. If you pivot Jan through Apr, how many rows will the pivoted table have?

Question 2 of 5

After pivoting, the original column headers end up in which generated field?

Question 3 of 5

A table has columns: Employee, Department, Salary, Bonus. You want to compare Salary and Bonus. Should you pivot?

Question 4 of 5

Where in Tableau do you perform a pivot operation?

Question 5 of 5

You accidentally pivoted the wrong columns. What is the correct way to undo it?