Unpivoting Data - Split from one table to apply to other table.

himalarohee

New Member
Joined
Mar 11, 2012
Messages
19
Hi, I have a table which have been loaded into data model like:

201420152016
A102030
B101525
C252530

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>


I have another one like

201420152016
a0.50.60.7
b0.40.30.2
c0.10.10.1

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>

How can I create a third table like below, which takes split from one table and it applies to the other one to create like below?

201420152016
A-a51221
A-b466
A-c123
B-a5917.5
B-b44.55
B-c11.52.5
C-a12.51521
C-b107.56
C-c2.52.53

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I'll have a go at it...

I put your first table in Sheet1 and your second table in Sheet2, exactly as you laid it out, with cell A1 of each sheet empty, the titles in row 1 and the letters A, B, C or a, b, c in column 1.

Then, in Sheet3, I made the following column headings in the first row:

Seq, Sheet2, Sheet1, =TRIM(Sheet2!A1), =TRIM(Sheet2!B1), =TRIM(Sheet2!C1), =TRIM(Sheet2!D1)

so the column headings look like this when displayed:

Seq, Sheet2, Sheet1, , 2014, 2015, 2016

Then, in the second row, I placed these formulas:

=SUM(1,OFFSET(A2,-1,0))
=MOD($A2-1,COUNTA(INDIRECT(B$1&"!$A:$A")))+1
=MOD(INT(($A2-1)/COUNTA(INDIRECT(B$1&"!$A:$A"))),COUNTA(INDIRECT(C$1&"!$A:$A")))+1
=INDEX(INDIRECT($C$1&"!$A:$D"),$C2+1,COLUMNS($D2:D2))&"-"&INDEX(INDIRECT($B$1&"!$A:$D"),$B2+1,COLUMNS($D2:D2))
=INDEX(INDIRECT($C$1&"!$A:$D"),$C2+1,COLUMNS($D2:E2))*INDEX(INDIRECT($B$1&"!$A:$D"),$B2+1,COLUMNS($D2:E2))
=INDEX(INDIRECT($C$1&"!$A:$D"),$C2+1,COLUMNS($D2:F2))*INDEX(INDIRECT($B$1&"!$A:$D"),$B2+1,COLUMNS($D2:F2))
=INDEX(INDIRECT($C$1&"!$A:$D"),$C2+1,COLUMNS($D2:G2))*INDEX(INDIRECT($B$1&"!$A:$D"),$B2+1,COLUMNS($D2:G2))

Notice that the last three are essentially the same; just copy one to the next. Then copy the formulas down from row 2 to the subsequent rows 3 through 10 in this case. The resulting table, in cells Sheet3!D1:G10 looks like this:

2014 2015 2016
A-a51221
A-b466
A-c123
B-a5917.5
B-b44.55
B-c11.52.5
C-a12.51521
C-b107.56
C-c2.52.53

<tbody>
</tbody>

As you can see, this approach is fairly general; It can handle sheets of any number of rows and columns, however it doesn't scale to more than 2 sheets, and it doesn't stop after 9 (in this case) rows (it just keeps spitting out the same table over and over!). With a little work, it could be made more general, however; I just wanted to throw this out there so you can tell me if you think I'm on the right track.
 
Last edited:
Upvote 0
Hi, I have just three cells in second row, how do you have 7 different formulas for it? Where do I use it all?

I'll have a go at it...

I put your first table in Sheet1 and your second table in Sheet2, exactly as you laid it out, with cell A1 of each sheet empty, the titles in row 1 and the letters A, B, C or a, b, c in column 1.

Then, in Sheet3, I made the following column headings in the first row:

Seq, Sheet2, Sheet1, =TRIM(Sheet2!A1), =TRIM(Sheet2!B1), =TRIM(Sheet2!C1), =TRIM(Sheet2!D1)

so the column headings look like this when displayed:

Seq, Sheet2, Sheet1, , 2014, 2015, 2016

Then, in the second row, I placed these formulas:

=SUM(1,OFFSET(A2,-1,0))
=MOD($A2-1,COUNTA(INDIRECT(B$1&"!$A:$A")))+1
=MOD(INT(($A2-1)/COUNTA(INDIRECT(B$1&"!$A:$A"))),COUNTA(INDIRECT(C$1&"!$A:$A")))+1
=INDEX(INDIRECT($C$1&"!$A:$D"),$C2+1,COLUMNS($D2:D2))&"-"&INDEX(INDIRECT($B$1&"!$A:$D"),$B2+1,COLUMNS($D2:D2))
=INDEX(INDIRECT($C$1&"!$A:$D"),$C2+1,COLUMNS($D2:E2))*INDEX(INDIRECT($B$1&"!$A:$D"),$B2+1,COLUMNS($D2:E2))
=INDEX(INDIRECT($C$1&"!$A:$D"),$C2+1,COLUMNS($D2:F2))*INDEX(INDIRECT($B$1&"!$A:$D"),$B2+1,COLUMNS($D2:F2))
=INDEX(INDIRECT($C$1&"!$A:$D"),$C2+1,COLUMNS($D2:G2))*INDEX(INDIRECT($B$1&"!$A:$D"),$B2+1,COLUMNS($D2:G2))

Notice that the last three are essentially the same; just copy one to the next. Then copy the formulas down from row 2 to the subsequent rows 3 through 10 in this case. The resulting table, in cells Sheet3!D1:G10 looks like this:

2014 2015 2016
A-a51221
A-b466
A-c123
B-a5917.5
B-b44.55
B-c11.52.5
C-a12.51521
C-b107.56
C-c2.52.53

<tbody>
</tbody>

As you can see, this approach is fairly general; It can handle sheets of any number of rows and columns, however it doesn't scale to more than 2 sheets, and it doesn't stop after 9 (in this case) rows (it just keeps spitting out the same table over and over!). With a little work, it could be made more general, however; I just wanted to throw this out there so you can tell me if you think I'm on the right track.
 
Upvote 0
if you add field header "ID" to top LH corner of each range and create normal defined names "A" for one table and "B" for the other, then this SQL returns the desired result

SELECT A.ID & '-' & B.ID AS [ID], A.[2014]*B.[2014] AS [2014], A.[2015]*B.[2015] AS [2015], A.[2016]*B.[2016] AS [2016]
FROM A, B

regards
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,017
Members
448,937
Latest member
BeerMan23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top