How to "unpivot" or "reverse pivot" in Excel?

67

31

I have data that looks like this:

Id | Loc1 | Loc2 | Loc3 | Loc4
---+------+------+------+-----
1  | NY   | CA   | TX   | IL
2  | WA   | OR   | NH   | RI

And I want to convert it to this:

Id | LocNum | Loc
---+--------+----
1  |   1    | NY
1  |   2    | CA
1  |   3    | TX
1  |   4    | IL
2  |   1    | WA
2  |   2    | OR
2  |   3    | NH
2  |   4    | RI

What's the easiest way to do this in Excel 2007?

devuxer

Posted 2009-12-03T19:26:49.990

Reputation: 3 331

Answers

69

You can do this with a pivot table.

  1. Create a "Multiple Consolidation Ranges PivotTable." (Only on Pivot Table Wizard. Callup with ALT+D, P on Excel 2007)
  2. Select "I will create my own page fields".
  3. Select your data.
  4. Double click on the grand total value - the one at the intersection of Row Grand and Column Grand, all the way on the lower right hand corner of your pivot table.

You should see a new sheet containing all of the data in your pivot table, transposed in the way you're looking for.

Datapig technologies provides step-by-step instructions that are actually more complicated than you need - his example transposes only part of the data set & uses the pivot technique combined with TextToColumns. But it does have lots of pictures.

Note that a pivot table will group the data. If you want it ungrouped, the only way to do it is to copy the pivot table, and "paste special" as values. You can then fill in the blanks with a technique like this: http://www.contextures.com/xlDataEntry02.html

DaveParillo

Posted 2009-12-03T19:26:49.990

Reputation: 13 402

Can this work if there are two levels of column headers? For example, this already has Loc1, Loc2, Loc3, Loc4 -- but what if a row above. Loc1 and Loc2 are in LocGroup1, and Loc3 and Loc4 are in LocGroup2 ? – The Red Pea – 2016-01-05T20:21:59.423

Ooh, I can do 2 levels but it's a new set of steps (specifically adding two ranges, one for LocGroup1 and one for LocGroup2), and manually naming each of these ranges for its corresponding Grouper. Anyone else have this experience? – The Red Pea – 2016-01-05T20:39:03.840

Definitely works, thanks! I had some trouble following DaveParillo's condensed instructions, and I found that I needed to follow DataPig's step-by-step instructions to get everything to work. Pictures! – Kent Hu – 2013-04-25T16:27:01.797

Thanks for your answer, but I'm not quite sure what you're saying here. For the example in my question, what would I use for Row Labels, Column Labels, Values, and Report Filter? – devuxer – 2009-12-03T21:05:59.647

If I put Id into Row Labels and Loc1 through Loc4 into Column Labels, then click the lower-right cell (the intersection of the Grand Totals), it does create a new sheet with a table, but it's not what I want. It's basically just a copy of the regular pivot table. – devuxer – 2009-12-03T21:20:36.717

I'm sorry - I left out a critical piece of data. The pivot Table must be a 'multiple consolidation range' type pivot table, even though in this case you have nothing to consolidate. – DaveParillo – 2009-12-03T23:19:23.780

Dave, thanks for editing your answer, but are you sure these instructions apply to Excel 2007? I'm not seeing anything about "multiple consolidation ranges". – devuxer – 2009-12-07T20:06:11.867

4Ah okay, I looked it up in the help and found a back-door way to get the old Pivot Table Wizard in Excel 2007 (you need to press ALT+D+P). Now I'm able to follow your steps, and it seems to work. Thanks for your help! – devuxer – 2009-12-07T20:44:00.213

7

If your data isn't an Excel Pivot Table but just data, you might want to "un-pivot" it with some simple VBA code. The code depends on two named ranges, Source and Target. Source is the data you want to un-pivot (exclusive of the column/row headers, e.g. NY-RI in the sample) and Target is the first cell where you want to place your result.

Sub unPivot()
Dim oTarget As Range
Dim oSource As Range
Dim oCell As Range

Set oSource = Names("Source").RefersToRange
Set oTarget = Names("Target").RefersToRange

For Each oCell In oSource
    If oCell.Value <> "" Then
        oTarget.Activate
      ' get the column header
        oTarget.Value = oCell.Offset(-(oCell.Row - oSource.Row + 1), 0).Text 
      ' get the row header
         oTarget.Offset(0, 1).Value = oCell.Offset(0, _
           -(oCell.Column - oSource.Column + 1)).Text 
      ' get the value
        oTarget.Offset(0, 2).Value = oCell.Text 
      ' move the target pointer to the next row
        Set oTarget = oTarget.Offset(1, 0) 
    End If
Next
Beep
End Sub

TJMelrose

Posted 2009-12-03T19:26:49.990

Reputation: 71

1This looks great, but as best I can tell, it will only work for a Table that has only one row header. If the table in the example had multiple row headers, say it had a "SubId" in addition to the Id field, this wouldn't work. Is there an easy way to modify it so that it would work in this situation? – Chris Stocking – 2015-01-29T20:42:58.783

1Thanks for this. Works as excepted and make me save a lot of time. – tigrou – 2013-04-22T14:56:11.993

Thank you!! this is just magic! much better than jigiry-pokery with unpivoting reports that never worked for me – trailmax – 2014-05-21T16:55:32.390

5

I have built an add-in that will let you do that, and that makes it easy to adapt to different situations. Check it out here: http://tduhameau.wordpress.com/2012/09/24/the-unpivot-add-in/

nutsch

Posted 2009-12-03T19:26:49.990

Reputation: 1 923

4

There is quite a nice solution in excel 2010, just need to play a bit with pivot table.

Create a pivot table from your data with these settings:

  • no subtotals, no grand totals
  • report layout: tabular form, repeat all item labels
  • add all the columns you need, keep columns you want to transform at the right
  • for each column you want to transform: open field settings – in layout & print tab: select “Show item labels in outline form” and check both check boxes below it
  • copy your table to a separate location (just values)
  • if you have empty cells in your original data then filter for empty values in the rightmost column and delete those rows (don’t filter in pivot table as it won't work as you need!)

Máté Juhász

Posted 2009-12-03T19:26:49.990

Reputation: 16 807

1This is certainly the best answer if you are in 2010 and up, and not at all obvious in the interface. Great find -- sadly I have only one upvote! – jkf – 2018-11-03T06:16:41.643

3

Best I've come up with so far is this:

Id   LocNum  Loc
---------------------------------
1    1       =INDEX(Data,A6,B6)
1    2       =INDEX(Data,A7,B7)
1    3       =INDEX(Data,A8,B8)
1    4       =INDEX(Data,A9,B9)
2    1       =INDEX(Data,A10,B10)
2    2       =INDEX(Data,A11,B11)
2    3       =INDEX(Data,A12,B12)
2    4       =INDEX(Data,A13,B13)

This works, but I have to generate the Id's and LocNum's manually. If there's a more automated solution (besides writing a macro), please let me know in a separate answer.

devuxer

Posted 2009-12-03T19:26:49.990

Reputation: 3 331

2

If the dimensions of your data are as in the sample provided in your question, then the following set of formulae using OFFSET should give you your required result:

Assuming

1 | NY | CA | TX | IL

2 | WA | OR | NH | RI

are in the range A2:E3, then enter

=OFFSET($A$2,FLOOR((ROW(A2)-ROW($A$2))/4,1),0)

in F2, say, and

=MOD(ROW(A2)-ROW($A$2),4)+1

in G2, say, and

=OFFSET($B$2,FLOOR((ROW(B2)-ROW($B$2))/4,1),MOD(ROW(A2)-ROW($A$2),4))

in H2, say.

Then copy these formulae down as far as required.

This is the easiest, pure, built-in formula solution that I can think of.

Aaa

Posted 2009-12-03T19:26:49.990

Reputation: 256

1

There is a parametric VBA conversion utility to unpivot or reverse pivoted data back to a database table, please see

http://www.spreadsheet1.com/unpivot-data.html

Petros

Posted 2009-12-03T19:26:49.990

Reputation: 11

1

Welcome to Super User! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.

– Peachy – 2012-11-06T16:55:40.727

1

Here is a nice tool to unpivot, normalize a Pivot Table.

Normalisieren von Pivot Tabellen

I hope it helps.

L4a-Thompson

Posted 2009-12-03T19:26:49.990

Reputation: 11

1

Welcome to SuperUser! While the tool you linked to offers a solution, this post is only useful as long as the link remains active. To make sure your post remains useful well into the future, please edit your answer to include any additional information about the product, including how to use it to solve the problem described in the question. Thanks!

– Excellll – 2014-04-24T14:02:55.323

1

You seem to have gotten the "loc" column (evidenced by your first answer), and now you need help getting the other two columns.

Your first option is to simply type the first several (say, 12) rows into those columns and drag down - I think Excel does the right thing in this case (I don't have excel on this computer to test it for sure).

If that doesn't work, or if you want something more programmer-y, try using the row() function. Something like "=Floor(row()/4)" for the ID column and "=mod(row(),4)+1" for the LocNum column.

Roie Marianer

Posted 2009-12-03T19:26:49.990

Reputation:

1

@DaveParillo answer is the best answer for a single tab table. I wanted to add some extras here.

For Multiple columns pre the unpivot columns

This method does not work.

Youtube unpivot simple data like the question

This is a youtube video showing how to do it in a simple manner. I skipped the part about adding the shortcut and used @devuxer shortcut that is in DaveParillo answer.

https://www.youtube.com/watch?v=pUXJLzqlEPk

BrinkDaDrink

Posted 2009-12-03T19:26:49.990

Reputation: 119

3The substance of your answer is a link to a video and the content isn't described. If the link breaks or is unavailable, your answer would have no value. Please include the essential information in your answer and use the link just for attribution and further exploration. – fixer1234 – 2015-06-16T18:47:20.763

-2

It's much simple than this. Just click the "Transpose" option in "Paste Special..." to get the transposition you are requesting.

C Tyler

Posted 2009-12-03T19:26:49.990

Reputation: 7

1This wouldn't work. – zx8754 – 2013-10-07T10:34:28.457