Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Unpivot Columns: A Complete Step-by-Step Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Unpivoting columns in Power Query transforms a "wide" dataset—where data is spread across multiple columns—into a "long" dataset, where those columns are collapsed into two: one for the attribute (the old column headers) and one for the value. This is essential for creating Pivot Tables and Power BI dashboards because analytical tools require data to be normalized (vertical) rather than summarized (horizontal) to perform calculations and filtering effectively.

What You'll Need

  • Microsoft Excel (with Power Query installed) or Power BI Desktop.
  • A dataset that contains "cross-tabulated" data (e.g., a table where months or years are used as column headers).
  • The data must be loaded into the Power Query Editor.

Step-by-Step

  1. Load Your Data: Open Excel or Power BI. Go to the Data tab, select Get Data, and choose your source (e.g., From Table/Range or From Workbook). This opens the Power Query Editor window. Note that menu labels may differ slightly depending on your version of Excel or Power BI.
  2. Identify Your Anchor Columns: Look at your table and determine which columns should not be unpivoted. These are typically your identifiers, such as "Customer Name," "Product ID," or "Region." These columns will remain fixed while the other columns collapse around them.
  3. Select the Anchor Columns: Click the header of the first anchor column. Hold the Ctrl key and click any other columns that should remain as they are. For example, if you have a table with "Employee Name," "Jan," "Feb," and "Mar," you would select only "Employee Name."
  4. Access the Unpivot Menu: Right-click on any of the selected column headers. From the context menu, select Unpivot Other Columns. This is the most robust method because if you add new columns (like "April") to your source data later, Power Query will automatically include them in the unpivot process without you having to update the steps.
  5. Alternative Selection (Unpivot Selected): If you only want to collapse a specific set of columns and ignore others, select only the columns you want to transform, right-click, and choose Unpivot Columns. Use this cautiously, as adding new columns to your source data will not automatically include them in this specific transformation.
  6. Alternative Selection (Unpivot Only): If you have a massive table and only one or two columns to keep, you can select the columns you want to collapse, right-click, and choose Unpivot Only Selected Columns.
  7. Rename the Resulting Columns: After unpivoting, Power Query creates two new columns named "Attribute" and "Value" by default. Double-click the "Attribute" header and rename it to something descriptive, such as "Month" or "Year." Double-click the "Value" header and rename it to something like "Sales Amount" or "Quantity."
  8. Verify Data Types: Check the icon next to the column headers. Ensure the new "Value" column is set to the correct data type (e.g., Decimal Number or Whole Number) and the "Attribute" column is set to Text.
  9. Apply and Load: Once the data looks correct, go to the Home tab and click Close & Apply (Power BI) or Close & Load (Excel) to return the transformed data to your worksheet or model.

Common Mistakes

One of the most frequent errors is using "Unpivot Columns" instead of "Unpivot Other Columns." When you select specific columns to unpivot, Power Query hard-codes those column names into the M-code. If your source data changes—for instance, if a new month is added to a monthly report—the new column will remain "wide" and will not be folded into the long list, leading to missing data in your reports. Always prefer "Unpivot Other Columns" by selecting the static IDs first.

Another common mistake is failing to clean the data before unpivoting. If your column headers have trailing spaces (e.g., "Jan " instead of "Jan"), these will be carried over into the "Attribute" column. This makes filtering and sorting difficult. It is best to use the Transform > Trim function on your headers or clean the source data before importing.

Users also frequently forget to check for nulls. If your wide table has empty cells, unpivoting will create rows with null values. While this is sometimes desired, it often creates unnecessary bulk in the dataset. If you don't need the empty records, you should filter out the nulls from the "Value" column immediately after the unpivot step.

Tips

To handle dynamic datasets, always think about the "Anchor." The anchor is the set of columns that define the unique row. By selecting these and using "Unpivot Other Columns," you create a flexible query that adapts to any number of new columns added to the right of the table in the future.

If you have a complex table with multiple groups of columns to unpivot (for example, a table with both "Sales" months and "Expense" months), you may need to perform the unpivot process twice. First, split the table into two separate queries using the Duplicate feature, unpivot the Sales columns in one and the Expense columns in the other, and then use the Append Queries feature to stack them back together into one long table.

Use the "Transpose" feature if your data is completely inverted (headers are in the first column instead of the first row). Transposing flips the axes of the table, allowing you to then use "Use First Row as Headers" and proceed with the standard unpivot workflow.

Troubleshooting

If the unpivot step results in an error saying "The column [Column Name] of the table wasn't found," it is because a column header in your source data was renamed or deleted. Power Query is looking for a specific name that no longer exists. To fix this, open the Query Settings pane on the right, find the "Unpivoted Columns" step, click the gear icon, and re-select the columns, or edit the formula bar to remove the reference to the missing column.

If you see "Duplicate" values that shouldn't be there, check if your anchor columns are truly unique. If you have two rows for "Customer A" and you unpivot, you will get two sets of the same attributes. Ensure you have a unique identifier (like a Customer ID) as one of your anchor columns to keep the data distinct.

If the data types are converting to "Any" (the mixed-type icon), explicitly set the data type for the "Value" column. Sometimes Power Query struggles to guess the type when columns of different types (e.g., text and numbers) are collapsed into one. Select the column, go to the Transform tab, and choose the specific data type required.

Frequently Asked Questions

Can I unpivot columns in a regular Excel sheet without Power Query?

Not easily. While you can use complex formulas or VBA macros, Power Query is the official and most efficient way to perform this action in modern Excel versions.

What is the difference between Pivot and Unpivot?

Pivoting turns unique values from one column into multiple new column headers (summarizing data). Unpivoting does the opposite, turning multiple column headers into unique values in a single column (normalizing data).

Will unpivoting delete my original data?

No. Power Query creates a series of transformation steps. The original source file remains untouched; Power Query simply changes how the data is presented when it is loaded into your report.

How do I handle multiple sets of columns to unpivot?

The best approach is to duplicate the query, unpivot the first set of columns in one query and the second set in another, then use the Append feature to combine them into one master list.

Why are my column headers appearing as data rows?

This is the intended result of unpivoting. The headers move from the top of the table into a new column (the Attribute column) so that they can be used as categories for filtering and grouping.