Pivot and UnPivot with SSIS
The next key configuration is the Input and Output Configuration of the Pivot transformation. We will need to add all twelve columns for Month apart from the Product Category column.
The configuration is similar as in the first example. For example, the Jan column PivotKeyValue is Jan and the SourceColumn is LinegeID of OrderQty of InuputColumns.
Below depicts the final output of this package.
In both of these cases, we have used one column to pivot, which is ProductCategory. Now we will attempt to add two columns for pivoting – Year and Month.
We will need to add the Year column, which is YEAR(OrderDate) at derive column. Year column was included for Aggregate transformation. Sorting is done for Month and Year.
Let us see what we have to do for Pivot transformation. PivotUsage of intYear column is 1. Source column of Year output column LineageID of intYear input column.
Below depicts the output for the above package.
Now that we have reviewed pivoting, it is time to learn how to “Unpivot”. The unpivot transformation makes a de-normalized dataset into a more normalized version by expanding values from multiple columns in a single record into multiple records within the same values in a single column. It is the exact the opposite of pivoting.
We can begin with our earlier data format, which is in a comma separated value (csv) format.
You can get the above data in a text file name PivotedData.txt, which you will find with the sample package.