Cumulative weights while satisfying a condition

alanlambden

Board Regular
Joined
Nov 20, 2014
Messages
73
Hi there,

I'm trying to run a small calculation that I can't seem to crack. I guess it would be best with a macro, but if it can be done using a formula, then that would be even better. Here's what I want to do:

Step one: Count down column B until cell value is no longer larger than the previous.
Step two: Sum all column A until column B is no longer larger than previous.
Step Three: Divide the cumulative sum of column A by total sum of column A until column B restarts at cell value of "1".

The answer I want is in Column C in the image below. I did this manually but I have many more records. In summary, calculate the cumulative weight of column A as a percentage of the total weight while column B counts upwards.

Apologies for the image, cant seem to install XL2BB. I'll keep trying.

1721078694213.png
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I am not sure if you have 365 and if I understand it correctly that you are only interested in column C but test this:

Excel Formula:
=LET(
weight,A2:A19,
count,B2:B19,
group,SCAN(0,--(count=1),LAMBDA(a,b,a+b)),
sum,DROP(REDUCE("",group,LAMBDA(a,b,VSTACK(a,SUM(FILTER(weight,group=b))))),1),
proportion,weight/sum,
100*SCAN(0,proportion,LAMBDA(a,b,IF(a+b<=1,a+b,0+b))))
 
Upvote 0
That seemed to work, thank you so much!

What tweaks would I make in order to calculate the proportion of values in the count column until the numbers reset? for example ... in column B working down the column until the numbers resest to 1 ... 1 would be 20% of 5, 2 is 40% of 5 and so on until it resets and then 1 is 16.6% of 6 and so on.
 
Upvote 0
Here is a pic to help demonstrate:
1721090080538.png


I want to find the value in %cumcount which I did manually. Using the highest number in the sequence until it starts again when resetting to 1.
 
Upvote 0
Please test this:

Excel Formula:
=LET(
weight,A2:A19,
count,B2:B19,
filter,FILTER(weight,count=1),
seq_1,SEQUENCE(ROWS(weight),,1,0),
seq_2,DROP(REDUCE("",filter,LAMBDA(a,b,VSTACK(a,SEQUENCE(b,,b,0)))),1),
proportion,seq_1/seq_2,
SCAN(0,proportion,LAMBDA(a,b,IF(a+b<=1,a+b,0+b))))
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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