Convert Columns to Rows with Excel Power Query

Galaxy Glossary

How do I convert columns to rows with Excel Power Query?

Converting columns to rows in Power Query—known as unpivoting—reshapes data by turning multiple header columns into attribute-value pairs.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

Overview

When working with data in Excel, you often receive wide tables where each attribute (for example, a month or a product) is stored in its own column. Reporting tools and downstream analytics, however, frequently require data in a long, tidy format — one column for the attribute name and another for its value. Power Query’s Unpivot feature automates this transformation, letting you convert columns to rows with just a few clicks or a short M script.

Why Unpivoting Matters

Data that is organized with one column per attribute is visually convenient for humans, but it is often inefficient for:

  • Building scalable PivotTables, charts, and Power BI models
  • Appending new data (for example, additional months)
  • Writing consistent formulas or DAX measures
  • Joining to other data sources

By unpivoting, you normalize the dataset, making it more resilient to structural changes and easier to analyze programmatically.

Step-by-Step Guide

1. Load Data into Power Query

In Excel, select any cell in your table > Data > From Table/Range. Power Query opens and displays a preview of your data.

2. Identify Key Columns

Decide which columns should remain fixed (e.g., CustomerID, Product) and which columns need to be converted into rows (e.g., Jan, Feb, Mar sales figures).

3. Use the Unpivot Command

  1. Select the fixed columns.
  2. Right-click any selected header > Unpivot Other Columns.

Power Query instantly reshapes the table into three columns: the keys you kept, an Attribute column holding former header names, and a Value column containing the data.

4. Rename the New Columns

Rename Attribute to something descriptive such as Month, and Value to Sales.

5. Close & Load

Click Home > Close & Load to push the transformed data back to Excel.

Underlying M Code

let
Source = Excel.CurrentWorkbook(){[Name="tblSales"]}[Content],
Unpivoted = Table.UnpivotOtherColumns(
Source,
{"CustomerID", "Product"},
"Month",
"Sales"
)
in
Unpivoted

This script performs the same operation programmatically, useful for automation or documentation.

Best Practices

  • Use named tables (Ctrl + T) before launching Power Query; they auto-update as data grows.
  • Keep a staging query with raw data and reference it in subsequent queries to preserve lineage.
  • Combine unpivot with data types by setting the Sales column to Decimal Number after transformation.
  • Document steps via meaningful step names; future maintainers will thank you.

Common Pitfalls

Hard-coding Column Lists

Don’t manually select the columns to unpivot if your dataset may add new periods. Instead, select the stable columns and use Unpivot Other Columns so new headers are automatically included.

Incorrect Data Types

After unpivoting, the Sales column may default to Any. Explicitly change it to Decimal Number to avoid errors in aggregations.

Over-Unpivoting

If you unpivot columns that should stay wide (e.g., Latitude/Longitude pairs), you end up with redundant rows. Plan your key columns carefully.

Real-World Example

A SaaS finance analyst receives monthly revenue by customer with one column per month. By unpivoting, she can build a dynamic PivotChart that automatically incorporates new months when the source file is updated—no extra VBA or manual edits required.

How This Relates to SQL Workflows

In SQL, you would use UNPIVOT (T-SQL) or UNION ALL/CROSS APPLY techniques to achieve the same shape. While Galaxy’s SQL editor streamlines those operations for database tables, Power Query offers a no-code path for Excel users handling local spreadsheets. If your data eventually lands in a database, the long format produced by Power Query will integrate more cleanly with Galaxy-authored SQL queries and BI pipelines.

Next Steps

  • Schedule a Refresh so the unpivoted query updates automatically.
  • Combine with Append Queries to union multiple files before unpivoting.
  • Publish the transformed data to Power BI or load it into a relational database for further analysis with Galaxy.

Why Convert Columns to Rows with Excel Power Query is important

Most analytical tools—including PivotTables, Power BI, and SQL databases—prefer data in a long, normalized form. Unpivoting in Power Query eliminates manual reshaping, reduces formula complexity, and ensures your datasets remain scalable as new columns appear. Mastering this technique is essential for anyone who prepares data for reporting or integrates Excel with modern BI or SQL workflows.

Convert Columns to Rows with Excel Power Query Example Usage


CustomerID | Product | Jan | Feb | Mar
-----------|---------|-----|-----|----
1001       | Widget  | 120 | 135 | 142

Common Mistakes

Frequently Asked Questions (FAQs)

Can I unpivot only some of the numeric columns?

Yes. Instead of Unpivot Other Columns, select only the specific headers you want to transform and choose Unpivot Columns. Just remember you’ll need to adjust the selection if new columns are added later.

How do I reverse an unpivot in Power Query?

Use the Pivot Column command. Select the attribute column (e.g., Month) to pivot on, choose the value column, and pick an aggregation method (usually Don’t Aggregate).

Does unpivoting affect the original Excel table?

No. Power Query works on a copy of the data. The transformed result is loaded to a new worksheet or Data Model, leaving your source intact.

What happens when new columns appear in my source data?

If you used Unpivot Other Columns, Power Query automatically includes the new columns when you refresh. Otherwise, you’ll need to edit the query and add them manually.

Want to learn about other SQL terms?