Report with two header rows unpivoted into a table with Power Query

Download the Advanced Unpivot Workbook

Get the Excel file from the video with the sales report with Actual and Budget headers, so you can build the Power Query steps yourself and test the refresh with a new month.

The Report and the Three Challenges

Let’s look at what we have been given to work with.

Sales report with customers, articles and Actual and Budget months in two header rows

What the Report Looks Like

  • We have a list of customer’s names, article descriptions, and sales.
  • Customer names are only listed for the first article description. It is assumed that all successive article descriptions belong to the noted customer until a subtotal row & new customer name is encountered.
  • A total row occurs after all transactions for a single customer.
  • A Grand Total row occurs at the bottom of the data.
  • Sales are shown by month. The month headers are real dates, formatted to show the month name.
  • Each month has a value for Actual along with a value for Budget.
  • The scenario headings Actual and Budget sit above their months, so the report has two header rows.

What the Result Should Look Like

  • Display a table with the records categorized by the scenario.
  • Exclude the total/grand total rows from the final report.
  • The report needs to accommodate expansion when new months are added to the data.
Final table with Customer, Article, Scenario, Date and Sales Value columns

Step 1: Use Center Across Selection Instead of Merged Cells

Notice that the scenario headings Actual and Budget are centered across the month headings, but the cells are not merged.

Actual and Budget headings centered across the month columns

When we bring the data into Power Query, we don’t want to disrupt this look.

Instead of merging the cells to center the scenario label, a better approach is to use the “Center Across Selection” alignment option.

This feature can be found by highlighting the desired cells and pressing CTRL-1.  In the Format Cells dialog box, under the Alignment tab is a dropdown for Horizontal.  It is from this dropdown that the Center Across Selection option can be found.

Format Cells Alignment tab with Center Across Selection selected

The advantage is that the range can become a proper Excel table and still look the same. Excel tables don’t allow merged cells. If you turn a range with merged cells into a table, Excel unmerges them and the report loses its look.

Step 2: Split the Window to See the Totals

Because the Grand Total row is so very far from the header rows, we will utilize a split-window view to keep track of the top and bottom of the table simultaneously.

Select View (tab) -> Window (group) -> Split.

Split command on the View tab

We can scroll the top and bottom windows separately giving us a view of the extents of the data.

Split window showing the top of the report and the grand total

Step 3: Turn the Report Into a Table Without Headers

  1. Select a cell in the data, press Ctrl+A to select the entire data range.
  2. Press Ctrl+T to create an Excel table and uncheck My table has headers, so Actual and Budget don’t become column headers.
Create Table dialog box with My table has headers unchecked
  1. Return the table to its original look by clearing the color formatting. This is done by selecting Table Design (tab) -> Table Styles (group) -> and selecting the None style in the upper-left corner of the style library.
Table Styles gallery with the None style selected
  1. Remove the automatically applied generic Header Row by deselecting Table Design (tab) -> Table Style Options (group) -> Header Row.
Header Row unchecked in Table Style Options
  1. Rename the table “TCustomer” (Table Design (tab) -> Properties (group) -> Table Name).
Table named TCustomer in the Table Name box
  1. Load the data into Power Query as a Table by selecting Data (tab) -> Get & Transform Data (group) -> From Table/Range.
From Table/Range on the Data tab

Step 4: Fill Down the Customer Names

  1. Start by giving the query a better name. In the Query Settings panel (right), change the current name of “TCustomer” to “DataProper”.
Query renamed to DataProper in Query Settings
  1. Remove the automatically applied Changed Type and Promoted Headers steps from the query.
Promoted Headers and Changed Type steps removed from Applied Steps
  1. Fill in the missing customer names for each blank row by performing a Fill Down operation on the customer names (Column1). Select Transform (tab) -> Any Column (group) -> Fill -> Down.
Fill Down on the Transform tab in Power Query

Step 5: Merge, Transpose and Unpivot the Headers

If our headers were confined to a single row, we could proceed to the unpivoting portion of the process.

Because we need to get the scenario names Actual and Budget associated with each month, we need to do a bit of processing on the headers first.

  1. Merge the customer names (Column1) and the article descriptions (Column2) using a semicolon as the delimiter into a new column named “Merged”. Select Column1 and Column2 and click Transform (tab) -> Text Column (group) -> Merge Columns.  The necessity of this step will soon become obvious.

NOTE: The separator can be any character as long as it doesn’t appear in the data.

If you only need to flip rows and columns once, without a refresh, these 3 ways to transpose data in Excel are faster.

This works with more than two anchor columns and more than two header rows too. Merge all anchor columns into one, and the rest of the steps stay the same.

Merge Columns on the Transform tab
Merge Columns dialog box with a semicolon separator
  1. Transpose the table by selecting Transform (tab) -> Table (group) -> Transpose.
Transposed table in Power Query with scenarios and dates in the first columns
  1. Perform a Fill Down operation on the scenario names (Column1).
  2. Promote the first row as a header row (Transform (tab) -> Table (group) -> Use First Row as Headers).
Use First Row as Headers on the Transform tab
  1. Remove the automatically applied Changed Type step.
  2. Select the first two columns (scenario and date) and click Transform (tab) -> Any Column (group) -> Unpivot Columns -> Unpivot Other Columns.
Unpivot Other Columns on the Transform tab
  1. Select the Attribute column and split the column by delimiter by selecting Transform (tab) -> Text Column (group) -> Split Column -> By Delimiter.
Split Column by Delimiter on the Transform tab
  1. The delimiter is a semicolon and will be split at each occurrence.
Split Column by Delimiter dialog box with semicolon and each occurrence
  1. Remove the automatically applied Changed Type.
  2. Rename the columns to Scenario, Date, Customer, Article and Sales Value.
Old and new column names for the unpivoted table
Renamed Scenario, Date, Customer, Article and Sales Value columns
  1. Filter the Customer column to keep rows that do not end with “Total”.
Filter Rows dialog box keeping customers that do not end with Total
  1. Change the data types: Text for Scenario, Customer and Article, Date for Date (this drops the time part) and Currency for Sales Value.
  2. Reorder the columns as Customer, Article, Scenario, Date and Sales Value.
  3. Close & Load the results to an Excel Table.
Close and Load on the Home tab in Power Query
Unpivoted sales table loaded back to Excel

Step 6: Add a New Month and Refresh

We need to ensure that when new months of data are added, the table updates accordingly.

To test it, insert a column for April Actual inside the table, right after March Actual. Then add April Budget at the end of the table so the table expands. Right-click the output table and choose Refresh.

April Actual and Budget columns added to the report

Once the data is refreshed, we can see the April transaction in the output table.

April rows in the output table after refresh

The same refresh idea works across files. See how to combine multiple Excel files into one with Power Query.

Step 7: Remove Empty Sales Values

There are rows in our table where there was no sale for an item in a given month.

Output rows with empty sales values

Why didn’t Unpivot remove them? Unpivot skips null cells, but some empty cells come in as empty text, not null. Once you change Sales Value to Currency, those empty cells turn into null. So add the filter after the Changed Type step.

We can add a step to the query that removes any rows where the sale is missing.

  1. Right-click the query and select Edit.
  2. Filter out the null values from the Sales Value column (uncheck null in its filter drop-down).
  3. Close & Load the results to an Excel Table.
Output table after removing rows with empty sales values

The results are as follows:

Final unpivoted table with Actual and Budget rows

Download the Advanced Unpivot Workbook

Get the Workbook From the Video

Practice the unpivot steps on the same report I use in the video.

Frequently Asked Questions

How do I unpivot data with multiple header rows in Excel?

Load the data into Power Query without headers, fill down the anchor columns and merge them into one column. Transpose the table, fill down the top header row, promote the first row to headers and use Unpivot Other Columns. Then split the merged column back apart.

Why do I need to merge the columns before transposing?

Unpivot needs a single header row. Merging the anchor columns into one column means that after the transpose they become a single header, so the scenario and month rows can turn into normal columns.

Why should I use Center Across Selection instead of merged cells?

Excel tables do not allow merged cells. If you create a table from a range with merged cells, Excel unmerges them. Center Across Selection gives the same look without merging, so the report keeps its layout.

How do I remove total rows in Power Query?

Open the filter on the customer column, choose Text Filters and Does Not End With, and type Total. This removes the customer totals and the grand total in one step.

Will the query pick up new months automatically?

Yes, as long as the new columns are inside the Excel table. Add the new month columns to the table, then right-click the output and choose Refresh.

Why do empty cells still show up after unpivoting?

Unpivot skips null cells, but some empty cells come in as empty text. After you change the value column to a number or currency type they turn into null, so filter out null in that column after the type change.

Leila Gharani

Founder of XelPlus and ten-time Microsoft MVP. Leila helps over 500,000 professionals master Excel, Power BI, and data automation through practical, real-world training.