WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 519 reviews for our classroom and online training
Unpivoting data in SSIS using the Unpivot transformation
If you've been given the output from a pivot table or a cube, you can collapse it back into its original state using the UNPIVOT transform in Integration Services.

Posted by Andy Brown on 07 April 2016

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Using the UNPIVOT transform in Integration Services

The UNPIVOT transform allows to collapse a two-dimensional array of data back into a relational database!  That sentence probably makes little sense - so read on.

The diagrams for this blog are taken from SSIS 2012, but the UNPIVOT transform has been there since 2005.

Our problem

Someone has given you the following pivot table to import:

Pivot table to import

The pivot table that you have to import.


Your mission is to turn it into this:

Unpivoted data

We want to show each row separately in a table.


The rest of this blog shows how to achieve this.  You can try this out yourself using this Excel workbook.

Step 1 - Importing the Data

Start by creating a data flow task, with an Excel source based on the above workbook:

The Excel source

The Excel source should be based on the Pivot Table worksheet.


When you edit this source, you can preview its columns:

Previewing columns

Double-click on the source to edit it, then click on the Preview... button as shown here.


You'll see that we have a lot of work to do!

The data to pivot

The imported data looks nothing like we want it to!

Step 2 - the Unpivot Transform

Add an Unpivot transform:

The Unpivot transform

You'll find the Unpivot transform here.


Connect this up, then double-click on the Unpivot transform to edit it:

Editing the Unpivot transform

Double-click on the transform to change what it does.


Here are the columns you'll see:

Columns in unpivot transform

The columns coming from the Excel pivot table.


Compare that with the pivot table, and it's reasonably obvious what you need to do:

The original pivot table

We need to change Sum of Spending to Store, F2 to 2014, F3 to 2015, F4 to 2016 and F5 to 2017.


To achieve this, first tell SSIS that you don't want to anything special with the Sum of Spending column (we'll rename it later, but as far as I can see you can't do this at this stage):

Pass-through column

We'll unpivot the data for the years, but keep the Sum of Spending as it is.


You can now specify how each of the year columns should be treated, by following the numbered steps listed below this diagram:

The columns

Complete the dialog box as explained below.

The steps are:

  1. For each column, say what value SSIS should use (eg the column F2 represents data for year 2014).
  2. Give a name to this set of values, to determine the name of the column SSIS will create.
  3. For each column, say what value is being stored.  Here we're telling SSIS to create a single extra column called Spending.

Add a Union all transform with a data viewer beneath it to view the output from this Unpivot transform:

Viewing the results

The data viewer will allow us to see what's coming out of the unpivot transform.


This is what you should see:

The output in the data viewer

We're already nearly there - we just need to get rid of the null row added, and rename the Sum of Spending column.


Step 3 - completing the package

There may be more elegant ways to tidy up the package, but I've gone for:

  • A conditional split transform to lose the null row; followed by
  • A union all transform to rename the Sum of Spending column.

The final diagram looks like this:

The final diagram

The numbers show that the conditional split transform has rejected one row.


Here's what the conditional split transform looks like:

Conditional split transform

The transform splits off rows where the spending is null - these are then not included further in our package.

I've used the union all transform to rename the Sum of Spending column to Store (there may be an easier way to do this):

Renaming our column

The output column name has been changed.


And that's the unpivot transform in Integration Services! 


This blog has 0 threads Add post