r/excel
•
u/land_cruizer
•
Jan 28 '26
•
1
solved Modified PIVOTBY solution to insert additional aggregations for each unique row block
Hi I have the following dataset:
| Area | Item | Month | Target | Actual |
|---|---|---|---|---|
| North | Mobiles | Jan-25 | 100 | 100 |
| North | Mobiles | Feb-25 | 50 | 120 |
| North | Mobiles | Mar-25 | 200 | 50 |
| South | PC | Jan-25 | 100 | 200 |
| South | PC | Feb-25 | 50 | 100 |
| South | PC | Mar-25 | 200 | 50 |
| South | Mobiles | Jan-25 | 100 | 100 |
| South | Mobiles | Feb-25 | 50 | 150 |
| South | Mobiles | Mar-25 | 200 | 50 |
My required result is in this format :
| Area | Item | Measure | Jan-25 | Feb-25 | Mar-25 |
|---|---|---|---|---|---|
| North | Mobiles | Target | 100 | 50 | 200 |
| North | Mobiles | Actual | 100 | 120 | 50 |
| North | Mobiles | Runn. Target | 100 | 150 | 350 |
| North | Mobiles | Runn.Actual | 100 | 220 | 270 |
| North | Mobiles | Total Variance | 0 | 70 | -80 |
| South | PC | Target | 100 | 50 | 200 |
| South | PC | Actual | 200 | 100 | 50 |
| South | PC | Runn. Target | 100 | 150 | 350 |
| South | PC | Runn.Actual | 200 | 300 | 350 |
| South | PC | Total Variance | 100 | 150 | 0 |
| South | Mobiles | Target | 100 | 50 | 200 |
| South | Mobiles | Actual | 100 | 150 | 50 |
| South | Mobiles | Runn. Target | 100 | 150 | 350 |
| South | Mobiles | Runn.Actual | 100 | 250 | 300 |
| South | Mobiles | Total Variance | 0 | 100 | -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
β’
u/AutoModerator Jan 28 '26
/u/land_cruizer - Your post was submitted successfully.
Solution Verifiedto close the thread.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.
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/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.
2
u/MayukhBhattacharya 1327 Jan 28 '26
Here is another method (Uses
PIVOTBY()):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/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:
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]