Power Query Problems

Unpivot State Columns into One Column with Excel Power Query

Steve KSteve K Dec 2, 2026 868 views

Question details

Convert separate state columns into a single State column with corresponding counts, using a repeatable process.

Unpivot State Columns into One Column with Excel Power Query
Product
Microsoft Excel Power Query
Device & OS
not provided
Scenario
A table contains drug counts in separate columns for each state, and the user wants to reshape multiple datasets.
Observed behavior
The desired result is one row per drug and state, with the state name and count in separate columns.
Before you start

Make sure the data has a header row and identify the columns to keep as identifiers, such as Drug and Dataset. Check that your Excel version has Power Query available on the Data tab.

Solution 1Recommended

Use Power Query to unpivot the state columns

Keep the identifier columns and turn all remaining state columns into State and Count fields.

1
Load the source table

Select a cell in the data and choose Data > From Table/Range. If prompted, confirm the range and select My table has headers.

2
Select identifier columns

In Power Query Editor, select the Drug column and hold Ctrl to select any other identifier columns, such as Dataset.

3
Unpivot the state columns

With the identifier columns selected, choose Transform > Unpivot Columns > Unpivot Other Columns. Power Query creates Attribute and Value columns.

4
Rename and load the results

Double-click Attribute and rename it State, then rename Value to Count. Choose Home > Close & Load to return the transformed table to Excel.

5
Refresh updated data

Update or replace the data in the source table while retaining the identifier headers, then select Data > Refresh All. New state columns in the source table will also be unpivoted.

Use Power Query to unpivot the state columns
Blank values: Unpivoting typically omits null values, so state and drug combinations with blank counts may not appear in the result.
Free Microsoft Office alternative

Work with State Count Tables in WPS Office

WPS Office is a free, lightweight alternative for viewing and editing spreadsheet data in a familiar interface. Open compatible Excel files to continue working with your tables; WPS does not fix or replace Excel Power Query.

View and edit drug and state count tables in WPS SpreadsheetsOpen and save compatible Microsoft Excel XLSX and XLS filesUse a familiar spreadsheet interface for easier migrationWork with a lightweight office suite available for free
microsoft office alternative - wps office

Frequently Asked Questions

Why should I select the drug column instead of the state columns?

Selecting Drug and any other identifiers before choosing Transform > Unpivot Columns > Unpivot Other Columns keeps those fields in place and converts the remaining columns into state names and counts.

Will the query include new state columns?

Yes. If the new columns are part of the source table and the identifier headers remain unchanged, Data > Refresh All applies Unpivot Other Columns to the new state columns.

How can I use the query with another dataset?

Replace the contents of the source table with the new data, keep the same identifier headers, and choose Data > Refresh All. Alternatively, create a separate query for the other dataset.

Why are some state and drug combinations missing?

Power Query typically omits null values when unpivoting. Check the source table for blank count cells if expected combinations do not appear.