Hello, young data explorer! Welcome to Module Ten. In this module, we will learn how to reshape our data. Sometimes data is in a "wide" format (many columns) and sometimes in a "long" format (many rows). We need to change from one to the other. This is called pivoting and unpivoting. Imagine you have a table where each month is a column. Unpivot turns those month columns into rows. Pivot does the opposite โ it turns rows into columns. This helps us prepare data for charts and reports. By the end of this module, you will be a Data Reshaping Master!
Chidi has a sales table with months as columns: January, February, March. He wants to create a chart, but the chart expects months in a single column. So he unpivots the data โ he turns the month columns into rows. Now he has a "Month" column and a "Sales" column. He can now make a beautiful chart. Later, his teacher wants a summary table with months as columns again. So he pivots the data back. That's the power of pivot and unpivot!
Definition: Wide data has many columns. Each column represents a different variable (e.g., Jan, Feb, Mar).
Why important: Wide data is easy to read for humans, but not always good for analysis.
Simple explanation: It's like a big table where each month has its own column.
Real-life example: A table with sales for each month as separate columns.
School example: A table with subjects as columns and student scores as rows.
Home example: A table with days of the week as columns and chores as rows.
Nigerian example: A table with states as columns and population as rows.
Illustration:
Wide Data (Months as columns) +--------+-----+-----+-----+ | Product| Jan | Feb | Mar | +--------+-----+-----+-----+ | Choco | 100 | 150 | 200 | | Lolly | 50 | 60 | 70 | +--------+-----+-----+-----+
Mini summary: Wide data has many columns.
Definition: Long data has fewer columns but many rows. One column identifies the variable (e.g., Month), and another holds the value (e.g., Sales).
Why important: Long data is great for charts and analysis.
Simple explanation: It's like a tall table where months are in a single column.
Real-life example: A table with a "Month" column and a "Sales" column.
School example: A table with "Subject" and "Score" columns.
Home example: A table with "Day" and "Expense" columns.
Nigerian example: A table with "State" and "Population" columns.
Illustration:
Long Data (Month in one column) +--------+-------+-------+ | Product| Month | Sales | +--------+-------+-------+ | Choco | Jan | 100 | | Choco | Feb | 150 | | Choco | Mar | 200 | | Lolly | Jan | 50 | | Lolly | Feb | 60 | | Lolly | Mar | 70 | +--------+-------+-------+
Mini summary: Long data has few columns but many rows.
Definition: Unpivot turns columns into rows. It takes wide data and makes it long.
Why important: Unpivot prepares data for charts and analysis.
Simple explanation: It's like taking a wide table and stacking it into a tall table.
Real-life example: Turning monthly sales columns into a Month column and a Sales column.
School example: Turning subject columns into a Subject column and a Score column.
Home example: Turning day columns into a Day column and a Chore column.
Nigerian example: Turning state columns into a State column and a Population column.
How to do it: Select the columns you want to unpivot, right-click, and choose "Unpivot Columns".
Illustration:
Wide โ Unpivot โ Long
+--------+-----+-----+-----+ +--------+-------+-------+
| Product| Jan | Feb | Mar | | Product| Month | Sales |
+--------+-----+-----+-----+ +--------+-------+-------+
| Choco | 100 | 150 | 200 | | Choco | Jan | 100 |
| Lolly | 50 | 60 | 70 | | Choco | Feb | 150 |
+--------+-----+-----+-----+ | Choco | Mar | 200 |
| Lolly | Jan | 50 |
| Lolly | Feb | 60 |
| Lolly | Mar | 70 |
+--------+-------+-------+
Mini summary: Unpivot turns columns into rows.
Definition: In Power Query, you can unpivot by selecting columns and using the "Unpivot Columns" option.
Why important: This is the main way to reshape data in Power Query.
Simple explanation: It's like clicking a button to change a wide table into a long table.
Real-life example: You have a table with months as columns. You unpivot to get a Month column.
School example: You have a table with subjects as columns. You unpivot to get a Subject column.
Home example: You have a table with days as columns. You unpivot to get a Day column.
Nigerian example: You have a table with states as columns. You unpivot to get a State column.
How to do it: Select the columns (e.g., Jan, Feb, Mar) โ right-click โ Unpivot Columns.
Illustration:
Select Jan, Feb, Mar โ Right-click โ Unpivot Columns
Mini summary: Use Unpivot Columns to turn columns into rows.
Definition: You can unpivot some columns while keeping others as identifiers (e.g., Product).
Why important: You often want to keep some columns as "keys" while unpivoting others.
Simple explanation: You keep the "Product" column as is, and unpivot the month columns.
Real-life example: Keep Product and Region, unpivot monthly sales.
School example: Keep Student Name, unpivot subject scores.
Home example: Keep Family Member, unpivot daily chores.
Nigerian example: Keep LGA, unpivot monthly rainfall.
How to do it: Select the columns to unpivot, right-click, and choose "Unpivot Columns". The other columns stay as identifiers.
Illustration:
Keep Product, unpivot Jan, Feb, Mar. Result: Product, Attribute (Month), Value (Sales).
Mini summary: Unpivot specific columns while keeping others as identifiers.
Definition: Pivot turns rows into columns. It takes long data and makes it wide.
Why important: Pivot helps create summary tables that are easy to read.
Simple explanation: It's like taking a tall table and spreading it out into a wide table.
Real-life example: Turning a Month column into separate columns for each month.
School example: Turning a Subject column into separate columns for each subject.
Home example: Turning a Day column into separate columns for each day.
Nigerian example: Turning a State column into separate columns for each state.
How to do it: In Power Query, select the column you want to pivot, go to Transform, and click "Pivot Column".
Illustration:
Long โ Pivot โ Wide +--------+-------+-------+ +--------+-----+-----+-----+ | Product| Month | Sales | | Product| Jan | Feb | Mar | +--------+-------+-------+ +--------+-----+-----+-----+ | Choco | Jan | 100 | | Choco | 100 | 150 | 200 | | Choco | Feb | 150 | | Lolly | 50 | 60 | 70 | | Choco | Mar | 200 | +--------+-----+-----+-----+ | Lolly | Jan | 50 | | Lolly | Feb | 60 | | Lolly | Mar | 70 | +--------+-------+-------+
Mini summary: Pivot turns rows into columns.
Definition: In Power Query, you pivot using the "Pivot Column" feature.
Why important: This is the main way to make wide tables from long tables.
Simple explanation: You choose a column whose values will become new columns.
Real-life example: You have a Month column. Pivot it to get columns for each month.
School example: You have a Subject column. Pivot it to get columns for each subject.
Home example: You have a Day column. Pivot it to get columns for each day.
Nigerian example: You have a State column. Pivot it to get columns for each state.
How to do it: Select the column to pivot (e.g., Month) โ Transform โ Pivot Column โ Choose the values column (e.g., Sales).
Illustration:
Select Month โ Pivot Column โ Values: Sales โ OK
Mini summary: Use Pivot Column to turn rows into columns.
Definition: You can pivot with more than one value column (e.g., Sales and Quantity).
Why important: Sometimes you need to pivot multiple measures.
Simple explanation: You can have both Sales and Quantity as values.
Real-life example: Pivot Month to get Sales and Quantity columns.
School example: Pivot Subject to get Score and Attendance.
Home example: Pivot Day to get Expenses and Calories.
Nigerian example: Pivot Year to get Population and GDP.
How to do it: In Pivot dialog, select "Advanced" and add multiple value columns.
Illustration:
Pivot Month, Values: Sales, Quantity Result: Jan_Sales, Jan_Qty, Feb_Sales, Feb_Qty, ...
Mini summary: Pivot can handle multiple value columns.
Definition: Unpivot makes data longer (more rows). Pivot makes data wider (more columns).
Why important: Choosing the right one depends on your goal.
Simple explanation: Unpivot when you need data for charts. Pivot when you need summary tables.
Real-life example: Unpivot for a line chart. Pivot for a summary report.
School example: Unpivot to compare scores. Pivot to show scores per subject.
Home example: Unpivot to track daily habits. Pivot to see weekly totals.
Nigerian example: Unpivot to analyze monthly trends. Pivot to show state-wise data.
Illustration:
Unpivot: Wide โ Long (for charts) Pivot: Long โ Wide (for summary)
Mini summary: Unpivot for analysis, Pivot for presentation.
Definition: After pivoting, you may want to rename the new columns.
Why important: To make column names clear and meaningful.
Simple explanation: Change "Jan" to "January Sales".
Real-life example: Rename "Jan" to "Jan-2025".
School example: Rename "Math" to "Math Score".
Home example: Rename "Monday" to "Monday Chores".
Nigerian example: Rename "Lagos" to "Lagos Population".
How to do it: Double-click the column header and type the new name.
Illustration:
Before: "Jan" After: "January Sales"
Mini summary: Rename pivoted columns for clarity.
Definition: You can keep multiple columns as identifiers when unpivoting.
Why important: Sometimes you need to keep more than one key column.
Simple explanation: Keep Product and Region, unpivot months.
Real-life example: Keep Product and Region, unpivot monthly sales.
School example: Keep Student and Class, unpivot subject scores.
Home example: Keep Family Member and Day, unpivot meals.
Nigerian example: Keep State and LGA, unpivot monthly rainfall.
How to do it: Select the columns to unpivot, the others stay as identifiers.
Illustration:
Identifiers: Product, Region Unpivot: Jan, Feb, Mar Result: Product, Region, Month, Sales
Mini summary: You can have multiple identifier columns when unpivoting.
Definition: Pivot has advanced options like aggregating values (e.g., Sum, Average).
Why important: If there are duplicate values, you need to aggregate.
Simple explanation: If you have two sales for the same month, sum them.
Real-life example: Sum sales for each month-product combination.
School example: Average scores for each subject-student combination.
Home example: Sum expenses for each day-category combination.
Nigerian example: Sum population for each state-LGA combination.
How to do it: In Pivot dialog, choose "Advanced" and select an aggregation.
Illustration:
Pivot Month, Values: Sales, Aggregation: Sum
Mini summary: Use advanced pivot options for aggregating.
Definition: Sometimes unpivot fails if columns have different data types.
Why important: To avoid errors, ensure all columns have the same data type.
Simple explanation: Make sure all month columns are numbers or all are text.
Real-life example: Jan is number, Feb is text โ error. Fix by changing types.
School example: Math is number, English is text โ error. Fix.
Home example: Monday is number, Tuesday is text โ error. Fix.
Nigerian example: Lagos is number, Abuja is text โ error. Fix.
How to fix: Change all columns to the same data type before unpivoting.
Illustration:
Before: Jan (number), Feb (text) โ Error Fix: Change both to number โ Unpivot works.
Mini summary: Ensure columns have the same data type before unpivoting.
Definition: You can pivot and then unpivot, or vice versa, to reshape data.
Why important: Sometimes you need multiple transformations.
Simple explanation: You might unpivot, clean data, then pivot back.
Real-life example: Unpivot to fix data, then pivot to create summary.
School example: Unpivot to analyze, then pivot to present.
Home example: Unpivot to track, then pivot to report.
Nigerian example: Unpivot for trends, then pivot for comparison.
Illustration:
Wide โ Unpivot โ Clean โ Pivot โ Wide (clean version)
Mini summary: Combine pivot and unpivot for powerful reshaping.
Definition: Tips to use pivot and unpivot effectively.
Why important: To avoid errors and get the desired shape.
Simple explanation: Like following a recipe for perfect cookies.
Illustration:
1. Check data types 2. Select identifiers 3. Choose pivot/unpivot 4. Rename columns 5. Verify result
Mini summary: Follow best practices for smooth reshaping.
How to Unpivot Data:
How to Pivot Data:
Did you know that the term "pivot" comes from sports โ pivoting means turning around a fixed point. In data, you turn rows into columns!
Wide Data
+--------+-----+-----+-----+
| Product| Jan | Feb | Mar |
+--------+-----+-----+-----+
| Choco | 100 | 150 | 200 |
| Lolly | 50 | 60 | 70 |
+--------+-----+-----+-----+
|
V
Unpivot
|
V
Long Data
+--------+-------+-------+
| Product| Month | Sales |
+--------+-------+-------+
| Choco | Jan | 100 |
| Choco | Feb | 150 |
| Choco | Mar | 200 |
| Lolly | Jan | 50 |
| Lolly | Feb | 60 |
| Lolly | Mar | 70 |
+--------+-------+-------+
Long Data
+--------+-------+-------+
| Product| Month | Sales |
+--------+-------+-------+
| Choco | Jan | 100 |
| Choco | Feb | 150 |
| Choco | Mar | 200 |
| Lolly | Jan | 50 |
| Lolly | Feb | 60 |
| Lolly | Mar | 70 |
+--------+-------+-------+
|
V
Pivot
|
V
Wide Data
+--------+-----+-----+-----+
| Product| Jan | Feb | Mar |
+--------+-----+-----+-----+
| Choco | 100 | 150 | 200 |
| Lolly | 50 | 60 | 70 |
+--------+-----+-----+-----+
| Feature | Wide | Long |
|---|---|---|
| Number of columns | Many | Few |
| Number of rows | Few | Many |
| Best for | Reports, human reading | Charts, analysis |
| Operation | Direction | Result |
|---|---|---|
| Unpivot | Columns โ Rows | Longer data |
| Pivot | Rows โ Columns | Wider data |
Congratulations! You have completed Module Ten on Pivot and Unpivot. You learned:
Pivot and unpivot are powerful tools to reshape data. Now you can transform any data into the perfect shape for your analysis or report!
Match the term to its description:
| Term | Description |
|---|---|
| 1. Wide Data | A. Many rows |
| 2. Long Data | B. Many columns |
| 3. Unpivot | C. Rows to columns |
| 4. Pivot | D. Columns to rows |
Answers: 1-B, 2-A, 3-D, 4-C
Scenario 1: You have a table with months as columns (Jan, Feb, Mar) and products as rows. You want to create a line chart showing sales by month. How would you reshape the data? Write the steps.
Scenario 2: You have a table with a "Month" column and a "Sales" column. You want to present it as a summary table with months as columns. How would you do it? Write the steps.
In groups, get a wide dataset (e.g., sales by month). Unpivot it to long format, then pivot it back to wide. Discuss why you might need to do each operation.
Create a small wide table (e.g., with months as columns). Use Power Query to unpivot it. Then rename the columns to "Month" and "Sales".
Project: Reshape School Data
You have a wide table of student scores: Student, Math, English, Science, Art. Unpivot the subject columns to create a long table. Then pivot it back to wide, but with subjects as rows and students as columns. Compare the two formats.
Download a sample dataset with months as columns (e.g., sales data). Use Power Query to unpivot it. Then create a pivot table in Excel based on the unpivoted data. Submit both the unpivoted table and the pivot table.
You have a dataset with columns: Product, Year, Quarter, Sales. Pivot the Quarter column to get columns for Q1, Q2, Q3, Q4. Then unpivot the year columns to get a Year column. This is a double transformation โ pivot and then unpivot.
Fill-in-the-Blank: 1. Wide, 2. Long, 3. Unpivot, 4. Pivot, 5. Attribute
True/False: 1-F, 2-T, 3-T, 4-F, 5-T
Matching: 1-B, 2-A, 3-D, 4-C
In Module Eleven, we will learn about Advanced Data Transformation โ combining multiple techniques to solve complex problems. We will use all the skills from previous modules to clean, combine, group, and reshape data like true experts. Get ready to become a Power Query Master!
Before the next class, practice unpivoting and pivoting with different datasets. Try to create both a chart and a report from the same data.
ยฉ 2025 Certified Power Query for Data Analysis Expert โ Module Ten