4 Ways to Unpivot Data in Microsoft Excel

Learn how to unpivot data in Microsoft Excel by following simple yet effective methods!

Mostly, you get normalized form data from databases, which you use in Microsoft Excel to create report form data using PivotTable, Power Query, etc.

What if you get report form data from any software or database and you need to use that dataset for data analytics? You can’t directly use a report form data to create further reports, like creating a PivotTable from another PivotTable.

Here comes the need to transform report form data to normalized form data, which is often known as unpivoting datasets. Follow along with me as I show the tried and tested methods to unpivot data in Excel. Let’s dive in!

Using the PivotTable and PivotChart Wizard

You can effortlessly use the PivotTable and PivotChart Wizard tool to unpivot a dataset from a report from data in Excel. To follow this method, first, you need to enable the PivotTable and PivotChart Wizard in your Excel desktop app.

Go to the Insert tab and check if the PivotTable and PivotChart Wizard tool is available in any commands block in the ribbon menu. If you don’t find it, the tool is disabled in the Excel desktop app.

Choose commands from
Choose commands from

To enable the tool, go to the source data worksheet and press Alt + F + T to launch the Excel Options dialog box.

Click on the Customize Ribbon category below the Excel Options column.

The Customize the Ribbon screen with two columns of Excel tools will open on the right side.

Click on Choose commands from the drop-down menu and select the All Commands option.

Now, scroll down to the Excel tools section that starts with P and slowly scroll through the list to find the PivotTable and PivotChart Wizard tool.

Click on it to select the command.

Select the Insert commands block in the Customize the Ribbon column.

Now, below the Customize the Ribbon column on the right side, click on the New Group button.

New Group (Custom) Rename
New Group (Custom) Rename

The New Group (Custom) group field will show up. Click on the Rename button.

Rename custom group
Rename custom group

The Rename dialog box will show up. Use it to rename the custom group. Also, choose a custom icon for the commands group.

Added PivotTable and PivotChart Wizard
Added PivotTable and PivotChart Wizard

Once added, the PivotTable and PivotChart Wizard tool will show up in the newly created group in the Insert tab.

Multiple consolidation ranges
Multiple consolidation ranges

Click on the PivotTable and PivotChart Wizard tool.

Select the Multiple consolidation ranges option in the PivotTable and PivotChart Wizard tool. Click Next.

I will create the page fields
I will create the page fields

Select the I will create the page fields option and click Next.

Step 2b of PivotTable and PivotChart Wizard
Step 2b of PivotTable and PivotChart Wizard

Step 2b of PivotTable and PivotChart Wizard will show up. Hit Finish to create a PivotTable.

Double-click grand total
Double-click grand total

Find the PivotTable and double-click on the final grand total value to generate normalized form data from the PivotTable as soon and as accurately as possible.

Normalized form data
Normalized form data

This normalized form of data will show up in a new worksheet. Copy this table and paste it somewhere else. You’ll, however, need to remove table formatting for the selected dataset.

Clear Formats
Clear Formats

Select the copied table and click on the Clear drop-down in the Clear Formats command.

The table will be transformed into a raw data format, and the Filter & Sort tool will appear.

Press Ctrl + Shift + L to disable the Filter & Sort tool.

How to unpivot data in Microsoft Excel
How to unpivot data in Microsoft Excel

That’s it! You’ve successfully converted report form data to normalized form data in Excel.

Using an Excel Formula

If you prefer using an Excel formula, you can use a combination formula created from INDEX, SEQUENCE, and INDIRECT functions.

Firstly, navigate to the worksheet where you’ve got the report form or PivotTable dataset that needs conversion into normalized form data.

Sample dataset 1

Suppose your source dataset looks something similar to the dataset given above. The source data is in the A1:G6 cell range.

Create the following column headers anywhere in the source data worksheet:

  • Region
  • Months
  • Sales
Formula to extract Region
Formula to extract Region

Let’s consider you’ve created the above column headers in the I1:K1 cell range.

Select I1, type in the following formula into the cell and hit Enter:

=INDEX($A$2:$A$6,INT((ROWS($I$2:I2)-1)/6)+1)

Here’s how you can customize this formula when using it in your worksheet:

  • $A$2:$A$6: The cell range of the source dataset.
  • $I$2:I2: The destination for the values of the first column, Region, from the source dataset.

Replace the above cell ranges according to your own dataset.

Formula to extract Months
Formula to extract Months

Now, select the cell J2 and type in the following formula:

=INDEX($B$1:$G$1,MOD(ROWS($J$2:J2)-1,6)+1)

Don’t forget to customize the cell range references in the above formula syntax. Here are the definitions of the references:

  • $B$1:$G$1: Fetches data from the first month column of the used dataset.
  • $J$2:J2: Destination for the month name column.

Hit Enter to calculate the cell and get the month’s name.

Formula to extract Sales
Formula to extract Sales

Finally, select K2 and enter the following formula into it:

=INDEX($B$2:$G$6,INT((ROWS($K$2:K2)-1)/6)+1,MOD(ROWS($K$2:K2)-1,6)+1)

Make sure you’re changing the cell range references according to your own input dataset.

Hit Enter to calculate the cell.

How to unpivot data using a formula
How to unpivot data using a formula

Now select I2:K2 and drag the fill handle down for up to 29 cells to copy the formula to the rest of the cells. There should be a total of 30 rows since the source data has 5 regions and 6 months, which translates to 30 cells after the transformation.

If your source data is much larger than that, calculate how many rows you need to drag the fill handle down.

Congratulations! You’ve successfully used this formula-based approach to unpivot data in Microsoft Excel.

Using Power Query

Power Query is the most preferred way to unpivot data in Excel. Once you create a data connection in this tool, you can reuse it continuously until you change the overall structure and organization of the source dataset.

There are two ways you can use this data transformation tool to transform report form data into normalized form data. The first one is importing data from an external source.

From Azure SQL Database
From Azure SQL Database

To follow this approach, click on the Get Data command in the Data tab of the Excel ribbon menu.

Now, hover the mouse cursor over your preferred data source, like From Azure. An overflow menu will open on the right side. There, you can choose the final data connector, like From Azure SQL Database.

You can now follow the onscreen instructions to complete the data import process into the Power Query Editor.

Create Table
Create Table

If you wish to export a PivotTable or any other report form data from the active worksheet, select the source data and click on the From Table/Range command.

Click OK on the Create Table dialog box to finalize the data export to the Power Query user interface.

Unpivot Other Columns
Unpivot Other Columns

Once the dataset is in Power Query Editor, select the first column that contains the column and row headers.

Right-click on the selected column and select the Unpivot Other Columns option in the context menu.

Unpivoted data in Power Query
Unpivoted data in Power Query

Power Query will instantly transform the report form data into normalized form data.

Close & Load To
Close & Load To

Click on the File menu and select the Close & Load To command.

Import Data
Import Data

Select the Existing worksheet option on the Import Data dialog box and click on the destination cell.

Click OK to complete the data import process.

Exported data to Excel
Exported data to Excel

You should see all the similar values under the same column in the table created in the active worksheet.

Clear Formats dialog
Clear Formats dialog

Select the imported data and click on the Clear Formats command in the Clear drop-down menu of the Home tab.

Unpivoted data in Excel using Power Query
Unpivoted data in Excel using Power Query

This should remove table formatting from the selected dataset. Now you have a normalized form dataset.

Using Excel VBA

If you prefer using Microsoft Excel using VBA scripting or are willing to learn VBA-based automation in Excel, you should try and practice this technique.

Firstly, go to the following Excel guide and find out how to set up a macro using a VBA script:

📒 Read More: How To Use The VBA Code You Find Online

If you’re ready to create your own macro, use this script:

VBA script 1
Sub UnpivotTable()
    Dim ws As Worksheet
    Dim srcRange As Range, destCell As Range
    Dim r As Long, c As Long, outputRow As Long
    Dim headerRow As Range
    
    ' Select source table
    On Error Resume Next
    Set srcRange = Application.InputBox("Select the source table:", Type:=8)
    On Error GoTo 0
    
    If srcRange Is Nothing Then
        MsgBox "No source table selected. Exiting.", vbExclamation
        Exit Sub
    End If
    
    ' Select destination cell
    On Error Resume Next
    Set destCell = Application.InputBox("Select the destination cell for unpivoted data:", Type:=8)
    On Error GoTo 0
    
    If destCell Is Nothing Then
        MsgBox "No destination cell selected. Exiting.", vbExclamation
        Exit Sub
    End If
    
    Set ws = srcRange.Worksheet
    outputRow = destCell.Row
    
    ' Determine headers
    Set headerRow = srcRange.Rows(1)
    
    ' Write new headers
    destCell.Value = "Category"
    destCell.Offset(0, 1).Value = "Attribute"
    destCell.Offset(0, 2).Value = "Value"
    outputRow = outputRow + 1
    
    ' Loop through the data and unpivot
    For r = 2 To srcRange.Rows.Count
        For c = 2 To srcRange.Columns.Count
            ws.Cells(outputRow, destCell.Column).Value = srcRange.Cells(r, 1).Value
            ws.Cells(outputRow, destCell.Column + 1).Value = headerRow.Cells(1, c).Value
            ws.Cells(outputRow, destCell.Column + 2).Value = srcRange.Cells(r, c).Value
            outputRow = outputRow + 1
        Next c
    Next r
    
    MsgBox "Unpivoting completed successfully!", vbInformation
End Sub
 
Macro dialog box
Macro dialog box

Once the VBA macro is ready, press Alt + F8 to launch the Macro dialog and select the UnpivotTable macro.

Hit Run to execute the script.

Select source data
Select source data

The macro will ask you to choose the source dataset through a dialog box.

Select destination
Select destination

Then, it’ll also ask you to select the destination cell for the normalized form dataset.

Unpivot data in Excel using Excel VBA
Unpivot data in Excel using Excel VBA

Once you supply all the input data, the Excel VBA macro will transform the input report form data into an unpivoted dataset.

📚 Read more: If you liked this Excel tutorial, you’ll also like the following:

Conclusions

Today, you’ve learnt how to unpivot data in Microsoft Excel using multiple proven methods, like the PivotTable wizard, an Excel formula, Power Query, and Excel VBA.

Which one did you like the most? Comment below to share your thoughts and feedback.

About the Author

Bipasha Nath

Bipasha Nath

Bipasha is a technical content writer with 10+ years of experience in the technology industry. She previously worked in a SaaS software development company focused on SharePoint, OneDrive, Word, Excel, Word solutions.

Related Posts

Comments

1 Comment

  1. Jon Peltier

    In the image Added-PivotTable-and-PivotChart-Wizard.png, you actually show the Pivot Table Fields, Items, and Sets command selected in the All Commands list, not the Pivot Table and Pivot Chart Wizard.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get the Latest Microsoft Excel Tips

Follow Us

Follow us to stay up to date with the latest in Microsoft Excel!

Subscribe for awesome Microsoft Excel videos 😃