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/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! :)