Unpivot State Columns into One Column with Excel Power Query
Question details
Convert separate state columns into a single State column with corresponding counts, using a repeatable process.

- 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.
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.
Use Power Query to unpivot the state columns
Keep the identifier columns and turn all remaining state columns into State and Count fields.
Select a cell in the data and choose Data > From Table/Range. If prompted, confirm the range and select My table has headers.
In Power Query Editor, select the Drug column and hold Ctrl to select any other identifier columns, such as Dataset.
With the identifier columns selected, choose Transform > Unpivot Columns > Unpivot Other Columns. Power Query creates Attribute and Value columns.
Double-click Attribute and rename it State, then rename Value to Count. Choose Home > Close & Load to return the transformed table to Excel.
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.

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.

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.




