sean
sean

Reputation: 727

How to Transpose Data Using Excel

is there a way that i can do the following transformation ?

enter image description here

Nov-2010    
1195
1175
1676
36500.5
$33,607
Dec-2010    
1215
1189
1775
46866.5
$38,714

to

Nov-2010 1195   1175    1676    36500.5    $33,607
Dec-2010 1215   1189    1775    46866.5    $38,714

Do i have to fill in the blanks first ?

Upvotes: 0

Views: 93

Answers (2)

user4039065
user4039065

Reputation:

The Nov-2010 would have to be on top of the 1195 in order to use the worksheet's Copy, Paste Special, Transpose command. The OFFSET function could work with a little row and column maths but that is a volatile¹ function and best kept for when nothing else will suffice. The INDEX function provides a non-volatile alternative to OFFSET.

Your sample data neatly offers 5 associated entries in column B for each date in column A. It has been my experience that sample data is not always representative of actual data. You will have to fill every row right to the maximum that any one date could have associated values.

In E4:F4 as standard formulas,

'in E4
=IFERROR(INDEX(A:A,
               AGGREGATE(15, 6, ROW(A$1:INDEX(A:A, MATCH(1E+99,A:A )))/SIGN(LEN(A$1:INDEX(A:A, MATCH(1E+99,A:A )))),
               ROW(1:1))), "")
'in F4
=IFERROR(INDEX(INDEX($B:$B, MATCH($E4,$A:$A, 0)):INDEX($B:$B,
               IFERROR(MATCH($E5,$A:$A, 0)-1, MATCH(1E+99,$B:$B ))),
               COLUMN(A:A)), "")

Fill E4 down until you run out of dates from column A to retrieve. Fill F4 right until you have reached the maximum of associated values that any date could have then fill down to the extent of the dates in column E.

      aggregate_offset


¹ Volatile functions recalculate whenever anything in the entire workbook changes, not just when something that affects their outcome changes. Examples of volatile functions are INDIRECT, OFFSET, TODAY, NOW, RAND and RANDBETWEEN. Some sub-functions of the CELL and INFO worksheet functions will make them volatile as well.

Upvotes: 0

Jerry Jeremiah
Jerry Jeremiah

Reputation: 9618

I am assuming that not every month has exactly five numbers. If it does you don't need any of the extra checks and you can just paste a much simpler formula into F4:I9 (explained below). But my formula works if one month has 2 values and another month has 12 values.

If your original data is in A1:B10 and your transposed heading are in E4:E5 like the screenshot of the spreadsheet shows then you can put this in F4 and copy it to F4:Z10

=IF(IF(ISERROR(MATCH($E5,$A:$A,0)>MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-1),MATCH(9.99999999999999E+307,$B:$B)+1>=MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4),MATCH($E5,$A:$A,0)>MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-1),OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0),"")

Gow it works:

The integral bit is OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0) which gets the value from a certain cell based on the value in E4. (This is what you would paste into F4:I9 if every month had exactly five values.) But that isn't sufficient if the months could each have a different number of values. In that case we need to know how to stop it from getting more values than we want:

If there is another month in E below the current row we can use IF(MATCH($E5,$A:$A,0)>MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-1,OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0),"") to optionally display a value.

If there is no other month in E below the current row we can use IF(MATCH(9.99999999999999E+307,$B:$B)+1>=MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4),OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0),"") to optionally display a value.

But we need both so we have to use an if to decide which one to use and we end up with the big one. Here it is spread across multiple lines for clarity - you can paste it into the formula bar like this but you can't paste it into a cell directly because of the line feeds.

=IF(
   IF(
      ISERROR(MATCH($E5,$A:$A,0)>MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-1),
      MATCH(9.99999999999999E+307,$B:$B)+1>=MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4),
      MATCH($E5,$A:$A,0)>MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-1),
   OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0),
   "")

Upvotes: 1

Related Questions