lrobbo314
Well-known Member
- Joined
- Jul 14, 2008
- Messages
- 3,929
- Office Version
- 365
- Platform
- Windows
UNPIVOT translates a set of columns in a table into attribute-value pairs, combined with the rest of the values in each row.
Based on the Power Query Table.Unpivot function. I used several lambda helper functions which will be listed below.
I will list the name of each lambda above the code for each formula.
UNPIVOT
Additional helper lambdas...
RPTROW
RPTCOLUMN
ATT
PVAL
Based on the Power Query Table.Unpivot function. I used several lambda helper functions which will be listed below.
I will list the name of each lambda above the code for each formula.
UNPIVOT
Excel Formula:
=LAMBDA(pivot_cols,unpivot_cols,
LET(
pvt_rng,pivot_cols,
upvt_rng,unpivot_cols,
pvt_data,DROP(pvt_rng,1),
upvt_data,DROP(upvt_rng,1),
pvt_head,TAKE(pvt_rng,1),
upvt_head,TAKE(upvt_rng,1),
pvt_col_count,COLUMNS(pvt_data),
upvt_col_count,COLUMNS(upvt_data),
total_rows,ROWS(pvt_data)*upvt_col_count,
pvt_idx,RPTCOL(total_rows,pvt_col_count),
upvt_idx,RPTROW(total_rows,pvt_col_count,upvt_col_count),
pvt_res,INDEX(pvt_data,upvt_idx,pvt_idx),
atr,ATT(total_rows,upvt_head),
val,PVAL(upvt_data,total_rows,upvt_col_count),
HSTACK(
HSTACK(
VSTACK(pvt_head,pvt_res),
VSTACK("Attribute",atr)
),
VSTACK("Value",val)
)
)
)
UNPIVOT | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | |||
1 | Company | Area | Color | ID | Letter | Company | Area | Attribute | Value | |||
2 | CompanyA | North | Red | 1 | A | CompanyA | North | Color | Red | |||
3 | CompanyB | South | Blue | 2 | B | CompanyA | North | ID | 1 | |||
4 | CompanyC | East | Yellow | 3 | C | CompanyA | North | Letter | A | |||
5 | CompanyD | West | Green | 4 | D | CompanyB | South | Color | Blue | |||
6 | CompanyB | South | ID | 2 | ||||||||
7 | CompanyB | South | Letter | B | ||||||||
8 | CompanyC | East | Color | Yellow | ||||||||
9 | CompanyC | East | ID | 3 | ||||||||
10 | CompanyC | East | Letter | C | ||||||||
11 | CompanyD | West | Color | Green | ||||||||
12 | CompanyD | West | ID | 4 | ||||||||
13 | CompanyD | West | Letter | D | ||||||||
Sheet3 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
G1:J13 | G1 | =UNPIVOT(A1:B5,C1:E5) |
Dynamic array formulas. |
Additional helper lambdas...
RPTROW
Excel Formula:
=LAMBDA(
ro,co,div,
MAKEARRAY(
ro,co,
LAMBDA(
r,c,
INT((r-1)/div)+1
)
)
)
RPTCOLUMN
Excel Formula:
=LAMBDA(
ro,co,
MAKEARRAY(
ro,co,
LAMBDA(
r,c,c
)
)
)
ATT
Excel Formula:
=LAMBDA(
ro,h,
LET(
co,COLUMNS(h),
s,SEQUENCE(ro,1,0),
INDEX(h,MOD(s,co)+1
)
)
)
PVAL
Excel Formula:
=LAMBDA(
rng,ro,co,
LET(
s,SEQUENCE(ro,,0),
rx,INT(s/co)+1,
cx,MOD(s,co)+1,
INDEX(rng,rx,cx)
)
)
Upvote
0