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

View all comments

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.