Saturday, July 11, 2009

Unpivot Transformation with Multiple Destination Columns

The need to unpivot data during an ETL operation is quite common. One method to perform the unpivoting is writing the source query in such a way that the data starts off unpivoted using the UNPIVOT T-SQL statement in SQL Server 2005 and later. This however would obviously not work out on a source that does not support unpivoting such as a text file or Office Excel or even older versions of popular RDBMSs. The other method of course is to use the Unpivot Transformation in Integration Services.

The Unpivot Transformation is quite a simple component, especially if you need to unpivot a bunch of columns to a single destination column. Unpivoting to multple destination columns however, seems a tad challenging the first time round, at least for me it did. Until I figured out how to. This post is all about unpivoting a columns to multiple destination columns.

Consider this scenario. A simple Product table (which I shall call Product_Source this point forward):

Source Table 

Needs to be transformed into the following Product table (which I shall call Product_Destination this point forward):

Destination Table with Single Output Column

Configuring the Unpivot transformation for this would be quite simple, and would look like this:

 Unpivot_SingleDestination_01

The problem starts when you need the Product_Destination table looking like this, with multiple destination columns (Quantity and Price):

Destination table with multiple output columns

You add the price columns as well (as in the image below) to the Unpivot transformation,

Unpivot_MultipleDestination_01

and get the following validation error (encountered by a lot of people):

PivotKeyValue is not valid. In an UnPivot transform with more than one unpivoted DestinationColumn, the set of PivotKeyValues per destination must match exactly.

The reason is this. If you look closely at the Pivot Key Value column of the Unpivot transformation you would notice that for the Quantity destination column, the set of values are ‘Quantity1’, ‘Quantity2’ and ‘Quantity3’. Whereas, for the Price destination column, the set of values are ‘Price1’, ‘Price2’ and ‘Price3’, which are clearly not matching. The solution to the problem here is quite trivial. Change the default values of the Pivot Key Value column of the offending destination column to match the values of the other destination columns. Like this:

Unpivot_MultipleDestination_02

And you’re good to go!

I’ve attached a sample solution of this scenario, so that you too could try it out first hand.

Share this post :

12 comments:

  1. You saved my life! Thanks a lot, there should be more like you...

    ReplyDelete
  2. just what i was looking for. Thanks

    ReplyDelete
  3. hey man very very thanks for posting the code and also for the sample downloadable code......

    one thing i want to tell here if you are starting any thing adding control, adding columns, datatypes etc..start from the scratch..it is worth doing with easy going without any strain and without any time consuming..i spent uselessly 3-4 hours even by seeing the sample code....

    ReplyDelete
  4. Thanks a lot... u made my day...

    ReplyDelete
  5. Superb stuff... Cannot thank you enough for this.

    ReplyDelete
  6. Clear explanation. Thanks for writing this.

    ReplyDelete
  7. Yes, great work. Fixed my problem once I deleted the unpivot and started fresh. Thank!!!!

    ReplyDelete