top of page

You are learning Power Query in MS Excel

How to unpivot data from a wide format to a long format in Power Query?

There are two main ways to unpivot data from a wide format to a long format in Power Query:

Method 1: Using the "Unpivot Columns" Option

1. Select the columns to unpivot: In the Power Query Editor, highlight all the columns you want to transform from wide to long format. Exclude the column(s) containing the constant values you want to use as attributes in the long format.
2. Go to Transform: Click on the "Transform" tab in the Power Query ribbon.
3. Unpivot Other Columns: Choose the option "Unpivot Other Columns" from the dropdown menu.

Power Query will automatically restructure your data. The previously selected columns will be transformed into separate rows, with a new "Attribute" column indicating which specific value came from which original column. A "Value" column will hold the corresponding data points.

Method 2: Using the "Unpivot" Function

1. Select all columns: Highlight all the columns in your data, including the ones you want to use as attributes.
2. Go to Transform: Click on the "Transform" tab.
3. Unpivot with Custom Columns: Choose the option "Unpivot with Custom Columns" from the dropdown menu.

This option offers more control over the unpivoting process. You'll be prompted to define a custom column by specifying:

- Unpivot By: Select the columns that will be converted into attribute names.
- New Name: Provide a name for the new "Attribute" column.
- Value Column From: Choose the column containing the data points you want to keep in the "Value" column.

Choosing the Right Method:

- Use "Unpivot Other Columns" for a quick and straightforward unpivoting of multiple data columns.
- Use "Unpivot with Custom Columns" if you need more control over the unpivoting process, such as renaming the attribute column or specifying a different value column.

Additional Tips:

- After unpivoting, you might need to rename or adjust the data types of the newly created "Attribute" and "Value" columns.
- You can further customize your long format by filtering, sorting, or performing other data transformations within Power Query.

bottom of page