r/excel • • Jan 28 '26 • Mindblown 1

solved Modified PIVOTBY solution to insert additional aggregations for each unique row block

Hi I have the following dataset:

AreaItemMonthTargetActual
NorthMobilesJan-25100100
NorthMobilesFeb-2550120
NorthMobilesMar-2520050
SouthPCJan-25100200
SouthPCFeb-2550100
SouthPCMar-2520050
SouthMobilesJan-25100100
SouthMobilesFeb-2550150
SouthMobilesMar-2520050

My required result is in this format :

AreaItemMeasureJan-25Feb-25Mar-25
NorthMobilesTarget10050200
NorthMobilesActual10012050
NorthMobilesRunn. Target100150350
NorthMobilesRunn.Actual100220270
NorthMobilesTotal Variance070-80
SouthPCTarget10050200
SouthPCActual20010050
SouthPCRunn. Target100150350
SouthPCRunn.Actual200300350
SouthPCTotal Variance1001500
SouthMobilesTarget10050200
SouthMobilesActual10015050
SouthMobilesRunn. Target100150350
SouthMobilesRunn.Actual100250300
SouthMobilesTotal Variance0100-50

With native PIVOTBY, Im only able to get the first two measures.( Target & Actual )

Im looking for a dynamic solution to get the other three measures also for each unique Area-item Combo and they need to be inserted in the same order

5 Upvotes

17 comments sorted by

β€’

u/AutoModerator Jan 28 '26

/u/land_cruizer - Your post was submitted successfully.

Failing to follow these steps may result in your post being removed without warning.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

6

u/bradland 279 Jan 28 '26

AFAIK, isn't something you can do directly with PIVOTBY. What you'd end up doing is creating your pivot with PIVOTBY, and then augmenting the rows with SCAN to "inject" the running total and variance rows. This is probably a better fit for a Pivot Table + Data Model.

FWIW, your post did inspire me to update my UNPIVOT function to handle multiple row IDs though. I've pasted it below in case you find it useful for prep.

// UNPIVOT
=LAMBDA(row_ids,column_names,values,[string_values], LET(
  THUNK, LAMBDA(x,LAMBDA(x)),
  EXPANDTHUNKS, LAMBDA(thunk_array, LET(
      max_cols, MAX(MAP(thunk_array, LAMBDA(scalart, COLUMNS(scalart())))),
      MAKEARRAY(ROWS(thunk_array), max_cols, LAMBDA(r,c,
          LET(
              row_thunk, INDEX(thunk_array, r, 1),
              row_array, row_thunk(),
              IFERROR(INDEX(row_array, c), "")
          )
      ))
  )),
  row_ids_count, ROWS(row_ids),
  col_count, COLUMNS(column_names),
  values_count, row_ids_count * col_count,
  values_idx, SEQUENCE(values_count),
  ids_idx,  ROUNDUP(values_idx / col_count, 0),
  keys_idx,  MOD(values_idx-1, col_count)+1,
  id_col, MAP(ids_idx, LAMBDA(idx, THUNK(INDEX(row_ids, idx, 0)))),
  key_col, INDEX(column_names, keys_idx),
  val_col_prep, INDEX(values, ids_idx, keys_idx),
  val_col, IF(OR(ISOMITTED(string_values), NOT(string_values)), val_col_prep, val_col_prep&""),
  report_rows, HSTACK(EXPANDTHUNKS(id_col), key_col, val_col),
  report_rows
))

You can use it to prep your data like this:

From there, you can use PIVOTBY on the output, but I don't know of a way to add custom measures in the way you have listed. You can use VSTACK to add multiple calculation rows, but the calculations are performed per line. There's no way to tell PIVOTBY to calculate the difference between two rows, and no way to tell it to calculate a running total.

As far as I can see, this one is going to take a lot of prep and multiple steps.

2

u/land_cruizer Jan 29 '26

Adding this to my LAMBDA collection, thanks !

1

u/scoobydiverr Jan 28 '26

Impressive!!

2

u/Downtown-Economics26 647 Jan 28 '26

I think I got it, although it's not much simpler than u/bradland's and I cheated on the final sort cuz... close enough.

=LET(headers,HSTACK(A1:B1,"Measure"),
basetbl,VSTACK(HSTACK(A2:C10,MAKEARRAY(COUNTA(A2:A10),,LAMBDA(a,b,IF(a>0,"00-"&D1))),D2:D10),HSTACK(A2:C10,MAKEARRAY(COUNTA(A2:A10),,LAMBDA(a,b,IF(a>0,"01-"&E1))),E2:E10)),
trun,BYROW(A2:E10,LAMBDA(x,SUMIFS(D2:D10,C2:C10,"<="&CHOOSECOLS(x,3),A2:A10,CHOOSECOLS(x,1),B2:B10,CHOOSECOLS(x,2)))),
actrun,BYROW(A2:E10,LAMBDA(x,SUMIFS(E2:E10,C2:C10,"<="&CHOOSECOLS(x,3),A2:A10,CHOOSECOLS(x,1),B2:B10,CHOOSECOLS(x,2)))),
tvar,actrun-trun,
fulltbl,VSTACK(basetbl,HSTACK(A2:C10,MAKEARRAY(COUNTA(A2:A10),,LAMBDA(a,b,IF(a>0,"02-Runn. Target"))),trun),HSTACK(A2:C10,MAKEARRAY(COUNTA(A2:A10),,LAMBDA(a,b,IF(a>0,"03-Runn. Actual"))),actrun),HSTACK(A2:C10,MAKEARRAY(COUNTA(A2:A10),,LAMBDA(a,b,IF(a>0,"04-Total Variance"))),tvar)),
almostfinal,PIVOTBY(HSTACK(CHOOSECOLS(fulltbl,1,2),CHOOSECOLS(fulltbl,4)),CHOOSECOLS(fulltbl,3),CHOOSECOLS(fulltbl,5),SUM,,0,,0),
out,VSTACK(HSTACK(headers,DROP(TAKE(almostfinal,1),,3)),DROP(almostfinal,1)),
out)

2

u/land_cruizer Jan 29 '26

Great one! Works well on my testing Solution Verified

1

u/reputatorbot Jan 29 '26

You have awarded 1 point to Downtown-Economics26.


I am a bot - please contact the mods with any questions

1

u/bradland 279 Jan 28 '26

Now take a run at the running total and variance measures! lol

Srsly, this one is a real beast of a challenge.

1

u/Downtown-Economics26 647 Jan 28 '26

Look again!

2

u/bradland 279 Jan 28 '26

Look at yoooooou lol Nice work man! :)

2

u/MayukhBhattacharya 1327 Jan 28 '26

Here is another method (Uses PIVOTBY() ):

=LET(
     _a, DROP(A:.E, 1),
     _b, LAMBDA(_x, INDEX(_a, , _x)),
     _c, _b(1), 
     _d, _b(2), 
     _e, _b(3), 
     _f, _b(4), 
     _g, _b(5),
     _h, _c&"|"&_d,
     H, HSTACK,
     P, LAMBDA(_y, _z, DROP(PIVOTBY(H(XMATCH(_h, _h), 
                                      _c:_d, 
                                      IF(_f, _z)), 
                                    _e, 
                                    _y, 
                                    SUM, , 0, , 0), , 1)),
     R, LAMBDA(_m, MAP(_c, _d, _m, LAMBDA(_u, _v, _w, 
                   SUM(TAKE(_m, 1):_w * 
                      (TAKE(_c, 1):_u = _u) * 
                      (TAKE(_d, 1):_v = _v))))),
     _i, R(_f),
     _j, R(_g),
     _k, H(P(_f, D1), 
           P(_g, E1), 
           P(_i, "Runn. "&D1), 
           P(_j, "Runn. "&E1), 
           P(_j - _i, "Total Variance")),
     VSTACK(IFNA(EXPAND(H(A1:B1, "Measure"), , 6), TAKE(_k, 1, 6)), 
            WRAPROWS(TOCOL(DROP(_k, 1)), 6)))

2

u/land_cruizer Jan 29 '26

Works great ! Solution Verified

Just a quick one, since all the code is dynamic, is there an easy modification to allow for additional category columns ( can be assigned as a LET variable ) ?

1

u/reputatorbot Jan 29 '26

You have awarded 1 point to MayukhBhattacharya.


I am a bot - please contact the mods with any questions

1

u/MayukhBhattacharya 1327 Jan 29 '26

Oh yes most definitely, Variables _b, P ( PIVOTBY() to summarize data by grouping rows and columns), R ( MAP() function for rolling/running sum) and make sure to make _f <> "", where you need to consider for additional category columns. Thank You SO Much, for your valuable feedback.

1

u/KezaGatame 4 Jan 28 '26

I dont have experience with PIVOTBY but why not create the measures already on your dataset with helper columns?

I use pivot tables on a daily basis and the field item (or however it’s called, dont use it that often either) is basically column wise operation (or array operation in programming terms).

1

u/land_cruizer Jan 29 '26

Yes, I thought of it initially. The source table is coming from Power Query, so would need some additional M coding for the resetting running totals. Also I have a slightly higher preference for dynamic arrays πŸ˜„

1

u/Decronym Jan 28 '26 edited Jan 29 '26

Acronyms, initialisms, abbreviations, contractions, and other phrases which expand to something larger, that I've seen in this thread:

Fewer LettersMore Letters
BYROWOffice 365+: Applies a LAMBDA to each row and returns an array of the results. For example, if the original array is 3 columns by 2 rows, the returned array is 1 column by 2 rows.
CHOOSECOLSOffice 365+: Returns the specified columns from an array
COLUMNSReturns the number of columns in a reference
COUNTACounts how many values are in the list of arguments
DROPOffice 365+: Excludes a specified number of rows or columns from the start or end of an array
EXPANDOffice 365+: Expands or pads an array to specified row and column dimensions
HSTACKOffice 365+: Appends arrays horizontally and in sequence to return a larger array
IFSpecifies a logical test to perform
IFERRORReturns a value you specify if a formula evaluates to an error; otherwise, returns the result of the formula
IFNAExcel 2013+: Returns the value you specify if the expression resolves to #N/A, otherwise returns the result of the expression
INDEXUses an index to choose a value from a reference or array
ISOMITTEDOffice 365+: Checks whether the value in a LAMBDA is missing and returns TRUE or FALSE.
LAMBDAOffice 365+: Use a LAMBDA function to create custom, reusable functions and call them by a friendly name.
LETOffice 365+: Assigns names to calculation results to allow storing intermediate calculations, values, or defining names inside a formula
MAKEARRAYOffice 365+: Returns a calculated array of a specified row and column size, by applying a LAMBDA
MAPOffice 365+: Returns an array formed by mapping each value in the array(s) to a new value by applying a LAMBDA to create a new value.
MAXReturns the maximum value in a list of arguments
MODReturns the remainder from division
NOTReverses the logic of its argument
ORReturns TRUE if any argument is TRUE
PIVOTBYHelps a user group, aggregate, sort, and filter data based on the row and column fields that you specify
ROUNDUPRounds a number up, away from zero
ROWSReturns the number of rows in a reference
SCANOffice 365+: Scans an array by applying a LAMBDA to each value and returns an array that has each intermediate value.
SEQUENCEOffice 365+: Generates a list of sequential numbers in an array, such as 1, 2, 3, 4
SUMAdds its arguments
SUMIFSExcel 2007+: Adds the cells in a range that meet multiple criteria
TAKEOffice 365+: Returns a specified number of contiguous rows or columns from the start or end of an array
TOCOLOffice 365+: Returns the array in a single column
VSTACKOffice 365+: Appends arrays vertically and in sequence to return a larger array
WRAPROWSOffice 365+: Wraps the provided row or column of values by rows after a specified number of elements
XMATCHOffice 365+: Returns the relative position of an item in an array or range of cells.

Decronym is now also available on Lemmy! Requests for support and new installations should be directed to the Contact address below.


Beep-boop, I am a helper bot. Please do not verify me as a solution.
[Thread #47216 for this sub, first seen 28th Jan 2026, 20:40] [FAQ] [Full list] ^[Contact] [Source code]