PIVOT

PIVOT(tbl,tbl_hdrs,row_attr_idxs,col_attr_idxs,val_idxs,agg_fn_idxs)
tbl
a reference to an Excel table, or suitable array
tbl_hdrs
a reference to [#Headers] for tbl, or a headers for a data in tbl
row_attr_idxs
a number or 1-d array of numbers for columns to group rows by
col_attr_idxs
a number or 1-d array of numbers for columns to group cols by
val_idxs
a number or 1-d array of numbers for columns to apply aggregation to
agg_fn_idxs
a number or 1-d array of numbers corresponding to AGGREGATE function_num; can also be custom function

Simple pivot table using SUMMARIZE

tboulden

Board Regular
Joined
Jan 14, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Built to create a simple pivot table using output from SUMMARIZE then picking out the unique values for the row and column grouping and doing a look-up off hashed values.

Excel Formula:
=LAMBDA(
  tbl,           // Table reference
  tbl_hdrs,      // Table1[#Headers], needed if tbl is from FILTER
  row_attr_idxs, // = # or {#,#,#,...}, 1-D
  col_attr_idxs, // = # or {#,#,#,...}, 1-D
  val_idxs,      // = # or {#,#,#,...}, 1-D
  agg_fn_idxs,   // = #, {#,#,#,...}, 1-D, or custom functions
    /* Custom function layout:
      LAMBDA(sw,
        SWITCH(sw,
          "label","Example Agg of",
          "function",LAMBDA(vals,fn(vals))
        )
      ) */
  LET(
    // Step 1a - Combine attr idxs
    groupby_idxs,hStack(row_attr_idxs,col_attr_idxs),
    // Step 1b - Summarize
    summary,
      summarize(tbl,tbl_hdrs,groupby_idxs,val_idxs,agg_fn_idxs),
    // Step 1c - Get summary hdrs and data
    summary_hdrs,vHead(summary),
    summary_data,vTail(summary),
    // Step 1d - Determine rel pos of row attrs
    row_rel_idxs,MATCH(row_attr_idxs,groupby_idxs,0),
    // Step 1e - Determine rel pos of col attrs
    col_rel_idxs,MATCH(col_attr_idxs,groupby_idxs,0),
    // Step 1f - Determine rel pos of val cols
    val_rel_idxs,SEQUENCE(,COLUMNS(val_idxs),COLUMNS(groupby_idxs)+1,1),
    // Step 1f - Combine rel idxs
    rel_idxs,hStack(row_rel_idxs,col_rel_idxs),
    // Step 1g - Select attr cols from tbl
    attr_cols,pickCol(tbl,groupby_idxs),
    // Step 1h - Sort unique attr combinations
    sort_uniq_attrs,SORT(UNIQUE(attr_cols),rel_idxs),
    // Step 2 - Helper function
    getTblHdr,
      LAMBDA(hdr_ref,idxs,
        INDEX(hdr_ref,1,idxs)),
    // Step 3a - Row headers
    row_hdrs,getTblHdr(summary_hdrs,row_rel_idxs),
    // Step 3b - Col headers
    col_hdrs,getTblHdr(summary_hdrs,col_rel_idxs),
    // Step 3c - Val headers
    val_hdrs,getTblHdr(summary_hdrs,val_rel_idxs),
    // Step 3d - Create row labels
    row_labels,
      SORT(
        UNIQUE(pickCol(summary_data,row_rel_idxs)),
        SEQUENCE(,COLUMNS(row_rel_idxs))
      ),
    // Step 3e - Attr for col labels
    col_attr_vals,
      SORT(
        UNIQUE(pickCol(summary_data,col_rel_idxs)),
        SEQUENCE(,COLUMNS(col_rel_idxs))
      ),
    // Step 3f - Summary agg vals
    agg_vals,pickCol(summary_data,val_rel_idxs),
    // Step 3g - Create column labels
    col_labels,
      TRANSPOSE(
        allPairs(
          col_attr_vals,
          TRANSPOSE(val_hdrs)
        )
      ),
    // Step 3h - Create hashes for attributes
    hash_uniq_attrs,BYROW(sort_uniq_attrs,hashArray),
    // Step 3i - Hash prior step w/ agg labels
    summary_hashes,
      pairWise(hash_uniq_attrs,val_hdrs,
        hashFunc
      ),
    // Step 3j - Reshape hashes into single column
    flatten_hashes,
      REDUCE(
        /*null*/,
        summary_hashes,
        LAMBDA(flattened,hash,
          vStack(flattened,hash)
        )),
    // Step 3k - Reshape vals into single column
    flatten_vals,
      REDUCE(
        /*null*/,
        agg_vals,
        LAMBDA(flattened,val,
          vStack(flattened,val)
        )),
    // Step 3l - Create hashes for row labels
    row_hashes,BYROW(row_labels,hashArray),
    // Step 3m - Create hashes for col labels
    col_hashes,BYCOL(col_labels,hashArray),
    // Step 3n - Combine row and col hashes
    data_hashes,
      pairWise(row_hashes,col_hashes,hashFunc),
    // Step 3o - Look-up summary vals for final
    data_vals,
      INDEX(
        flatten_vals,
        MATCH(data_hashes,flatten_hashes,0)),
    
    // Step 4a - Stack row attr hdrs and labels
    row_hdrs_and_labels,
      vStack(row_hdrs,row_labels),
    // Step 4b - Stack col attr hdrs and labels
    col_hdrs_and_labels,
      vStack(col_hdrs,col_labels),
    // Step 4c - Whitespace padding for top-left corner
    padding,
      pairWise(col_labels,row_hdrs_and_labels,
        LAMBDA(x,y,"")),
    // Step 4d - Stack padding and row details
    LHS,vStack(padding,row_hdrs_and_labels),
    // Step 4e - Stack col details and data vals
    RHS,vStack(col_hdrs_and_labels,data_vals),
    // Step 4f - Stack LHS and RHS side-by-side
    final,hStack(LHS,RHS),
    final
  )
)

Uses same functions as SUMMARIZE, plus the hash functions below:

hashFunc
Excel Formula:
=LAMBDA(x,y,x&CHAR(1)&y);

hashArray
Excel Formula:
=LAMBDA(array,REDUCE(,array,hashFunc));
 
Upvote 0
Uses the same data as SUMMARIZE examples.

LAMBDA_pairwise_thunks_summarize_pivot.xlsx
ABCDEFGHIJK
1IdxHeaderCountryValues
21IDCanadaUSA
32TeamTeamAverage of WeightAverage of HeightFtAverage of WeightAverage of HeightFt
43CountryMen193.166.0066666671915.976666667
54NameFWomen152.875.630434783148.86956525.514492754
65NameL
76Weight Country
87HeightCanadaCanadaUSAUSA
98DOBTeamAVERAGE of WeightAVERAGE of HeightFtAVERAGE of WeightAVERAGE of HeightFt
109HometownMen193.166.0066666671915.976666667
1110ProvWomen152.875.630434783148.86956525.514492754
1211Pos
1312AgeTeamValues
1413HeightFtMenMenMenWomenWomenWomen
1514HtInCountryPosMin of HeightFtAverage of HeightFtMax of HeightFtMin of HeightFtAverage of HeightFtMax of HeightFt
1615BMICanadaDefence5.755.9895833336.255.3333333335.5416666675.75
17CanadaForward5.6666666675.9880952386.255.4166666675.6547619055.833333333
18CanadaGoalie6.0833333336.1388888896.1666666675.6666666675.6944444445.75
19USADefence5.756.0520833336.3333333335.0833333335.5119047626
20USAForward5.5833333335.9226190486.4166666675.1666666675.5128205135.916666667
21USAGoalie66.0277777786.0833333335.4166666675.5277777785.583333333
22
23 Team
24MenMenMenWomenWomenWomen
25CountryPosMIN of HeightFtAVERAGE of HeightFtMAX of HeightFtMIN of HeightFtAVERAGE of HeightFtMAX of HeightFt
26CanadaDefence5.755.9895833336.255.3333333335.5416666675.75
27CanadaForward5.6666666675.9880952386.255.4166666675.6547619055.833333333
28CanadaGoalie6.0833333336.1388888896.1666666675.6666666675.6944444445.75
29USADefence5.756.0520833336.3333333335.0833333335.5119047626
30USAForward5.5833333335.9226190486.4166666675.1666666675.5128205135.916666667
31USAGoalie66.0277777786.0833333335.4166666675.5277777785.583333333
pivot
Cell Formulas
RangeFormula
A2:B16A2=CHOOSE({1,2},SEQUENCE(COLUMNS(PlayerData[#Headers])),TRANSPOSE(PlayerData[#Headers]))
D7:H11D7=pivot(PlayerData,PlayerData[#Headers],2,3,{6,13},{1,1})
D23:K31D23=pivot(PlayerData,PlayerData[#Headers],{3,11},2,{13,13,13},{5,1,4})
Dynamic array formulas.
 
Formula with comments stripped out.
Excel Formula:
=LAMBDA(
  tbl,tbl_hdrs,row_attr_idxs,col_attr_idxs,val_idxs,agg_fn_idxs,
  LET(
    groupby_idxs,hStack(row_attr_idxs,col_attr_idxs),
    summary,
      summarize(tbl,tbl_hdrs,groupby_idxs,val_idxs,agg_fn_idxs),
    summary_hdrs,vHead(summary),
    summary_data,vTail(summary),
    row_rel_idxs,MATCH(row_attr_idxs,groupby_idxs,0),
    col_rel_idxs,MATCH(col_attr_idxs,groupby_idxs,0),
    val_rel_idxs,SEQUENCE(,COLUMNS(val_idxs),COLUMNS(groupby_idxs)+1,1),
    rel_idxs,hStack(row_rel_idxs,col_rel_idxs),
    attr_cols,pickCol(tbl,groupby_idxs),
    sort_uniq_attrs,SORT(UNIQUE(attr_cols),rel_idxs),

    getTblHdr,
      LAMBDA(hdr_ref,idxs,
        INDEX(hdr_ref,1,idxs)),

    row_hdrs,getTblHdr(summary_hdrs,row_rel_idxs),
    col_hdrs,getTblHdr(summary_hdrs,col_rel_idxs),
    val_hdrs,getTblHdr(summary_hdrs,val_rel_idxs),
    row_labels,
      SORT(
        UNIQUE(pickCol(summary_data,row_rel_idxs)),
        SEQUENCE(,COLUMNS(row_rel_idxs))
      ),
    col_attr_vals,
      SORT(
        UNIQUE(pickCol(summary_data,col_rel_idxs)),
        SEQUENCE(,COLUMNS(col_rel_idxs))
      ),
    agg_vals,pickCol(summary_data,val_rel_idxs),
    col_labels,
      TRANSPOSE(
        allPairs(
          col_attr_vals,
          TRANSPOSE(val_hdrs)
        )
      ),
    hash_uniq_attrs,BYROW(sort_uniq_attrs,hashArray),
    summary_hashes,
      pairWise(hash_uniq_attrs,val_hdrs,
        hashFunc
      ),
    flatten_hashes,
      REDUCE(
        ,
        summary_hashes,
        LAMBDA(flattened,hash,
          vStack(flattened,hash)
        )),
    flatten_vals,
      REDUCE(
        ,
        agg_vals,
        LAMBDA(flattened,val,
          vStack(flattened,val)
        )),
    row_hashes,BYROW(row_labels,hashArray),
    col_hashes,BYCOL(col_labels,hashArray),
    data_hashes,
      pairWise(row_hashes,col_hashes,hashFunc),
    data_vals,
      INDEX(
        flatten_vals,
        MATCH(data_hashes,flatten_hashes,0)),
    
    row_hdrs_and_labels,
      vStack(row_hdrs,row_labels),
    col_hdrs_and_labels,
      vStack(col_hdrs,col_labels),
    padding,
      pairWise(col_labels,row_hdrs_and_labels,
        LAMBDA(x,y,"")),
    LHS,vStack(padding,row_hdrs_and_labels),
    RHS,vStack(col_hdrs_and_labels,data_vals),
    final,hStack(LHS,RHS),
    final
  )
)
 

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

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