Smart Systems for Home

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

Power Query Unpivot: How to Transform Your Data

· 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

Power Query Unpivot is a data transformation process that converts columns into rows, effectively turning a "wide" dataset into a "long" dataset. This is essential for data analysis because most visualization tools, such as Power BI and Excel Pivot Tables, require data to be in a normalized, tabular format where each column represents a single attribute and each row represents a single record. Instead of having separate columns for "January," "February," and "March," unpivoting creates one column for "Month" and another for "Value."

What You'll Need

  • Microsoft Excel (with Power Query installed) or Power BI Desktop.
  • A dataset that is currently in a "cross-tab" or "matrix" format (where data is spread across multiple columns that should actually be values in a single column).
  • Access to the Power Query Editor window.

Step-by-Step

  1. Load Your Data: Open Excel or Power BI. In Excel, go to the Data tab and select Get Data to import your file, table, or range. Once the data is loaded into the Power Query Editor, you will see your data grid with the column headers at the top.
  2. Identify Your Anchor Columns: Look at your table and determine which columns should stay exactly as they are. These are your "anchor" columns (e.g., Product ID, Customer Name, or Region). These columns provide the context for the data you are about to unpivot.
  3. Select the Anchor Columns: Click on the header of the first anchor column. Hold the Ctrl key and click on any other columns that should not be unpivoted. These columns will be highlighted.
  4. Execute the Unpivot: Right-click on any of the selected (highlighted) column headers. From the context menu, select Unpivot Other Columns. (Note: Menu labels may differ slightly by version or region). This tells Power Query to keep the selected columns fixed and collapse all other columns into two new columns.
  5. Verify the Transformation: Your data will now shift. The columns you did not select have disappeared, and in their place, you will see two new columns: Attribute (containing the old column headers) and Value (containing the data that was inside those columns).
  6. Rename the New Columns: The default names "Attribute" and "Value" are rarely useful for final reports. Double-click the "Attribute" header and rename it to something descriptive, such as "Date," "Month," or "Category." Double-click the "Value" header and rename it to something like "Sales Amount" or "Quantity."
  7. Set Data Types: Check the icon next to the column headers (e.g., ABC or 123). Ensure the new Value column is set to the correct data type (Decimal Number, Whole Number, etc.) and the Attribute column is set to Text or Date.
  8. Apply Changes: Once the data looks correct, go to the Home tab and select Close & Apply (Power BI) or Close & Load (Excel) to bring the transformed data back into your worksheet or model.

Common Mistakes

One of the most frequent errors is selecting the columns you want to remove and choosing "Unpivot Columns" instead of selecting the columns you want to keep and choosing "Unpivot Other Columns." While both achieve a similar result initially, "Unpivot Other Columns" is dynamic. If you add a new month column (e.g., "April") to your source data next month, "Unpivot Other Columns" will automatically include that new column in the unpivot process during the next refresh. If you explicitly selected Jan, Feb, and March, the new April column will be ignored, leaving your data incomplete.

Another common mistake is ignoring data types before unpivoting. If your value columns have mixed data types (some text, some numbers), Power Query may force the resulting "Value" column into a "Any" or "Text" type. This prevents you from performing mathematical calculations like sums or averages in your final report. Always ensure your source value columns are consistent in type before starting the process.

Users also frequently forget to rename the Attribute and Value columns. While this doesn't break the data, it makes the dataset confusing for other users or for yourself when building complex DAX measures or Pivot Tables later. Habitually renaming these columns immediately after the unpivot step ensures a clean data dictionary.

Tips

For datasets with a massive number of columns, using the "Unpivot Other Columns" method is significantly faster than manually selecting fifty different columns. It reduces the amount of M-code generated in the Advanced Editor and makes the query easier to maintain.

If you have a few columns that should not be unpivoted but they are scattered across the table, you can still use the selection method. However, if your data has a consistent structure where the first three columns are always the anchors, "Unpivot Other Columns" is the gold standard for stability.

Consider adding a "Conditional Column" after unpivoting if you need to group the newly created attributes. For example, if your unpivoted attribute column contains "Jan-2023," "Feb-2023," etc., you can create a new column to extract just the year for higher-level reporting.

Always check for null values before unpivoting. By default, Power Query removes rows where the value is null during an unpivot operation. If a null value is meaningful (e.g., it represents a zero or a specific lack of activity), you may need to replace those nulls with a 0 or "N/A" using the Replace Values feature before you unpivot.

Troubleshooting

If your data does not unpivot as expected, first check your selection. If you see that some columns remained as columns while others became rows, you likely used "Unpivot Columns" instead of "Unpivot Other Columns." To fix this, look at the Applied Steps pane on the right, click the gear icon next to the Unpivot step, or simply delete the step by clicking the X and try again using the "Other Columns" option.

To resolve this, go back to the step before the unpivot, select the problematic column, and use Replace Errors or Replace Values to clean the data.

If the unpivot process is taking an extremely long time or crashing your system, check the size of your dataset. Unpivoting multiplies the number of rows in your table. If you have 1,000 rows and 12 month columns, you will end up with 12,000 rows. If you have 100,000 rows and 100 columns, you are creating 10 million rows. In these cases, try to filter out unnecessary rows or columns before performing the unpivot to reduce the memory load.

If new columns are appearing in your final table that you didn't want, it means your anchor columns weren't correctly defined. Go back to the Unpivot step in the Applied Steps list and ensure that every column that should remain a column is explicitly selected as an anchor.

Frequently Asked Questions

Can I unpivot only a specific range of columns?

Yes. You can manually select a specific group of columns by holding Ctrl and clicking them, then right-clicking and selecting "Unpivot Columns." However, this is less flexible than "Unpivot Other Columns" when source data grows.

What happens to the original column headers?

The original column headers are moved into the rows of the new "Attribute" column. They are no longer headers but become data values within that column.

Does unpivoting delete my original data?

No. Power Query does not change the source file. It creates a set of transformation steps that are applied to the data as it is loaded into your tool. Your original Excel or CSV file remains untouched.

Can I undo an unpivot?

Yes. You can either delete the Unpivot step from the Applied Steps pane or use the "Pivot Column" feature to turn the rows back into columns, though the latter requires specifying an aggregation method (like Sum or Count).

Why are some of my rows missing after unpivoting?

Power Query automatically removes rows that contain null values during an unpivot. If you need to keep those rows, replace the nulls with a zero or a placeholder value before you perform the unpivot operation.

Is there a limit to how many columns I can unpivot?

There is no hard limit on the number of columns, but there is a practical limit based on your computer's RAM. Because unpivoting significantly increases the row count, very large datasets may slow down your system.