Counting and comparing numbers until condition is met

jaffar

New Member
Joined
Oct 12, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi all
I'm trying to sum a set of numbers until it is bigger than a given number, then count how many numbers there were until that condition is met.
My example is to count how many trains are required to service a given pedestrian queue at a station, when the profile of boarding demand and boarding capacity of each train is a known value but which varies across the trains over time. The passengers unable to board is after the train is full and departed the station i.e. the size of the queue as soon as the train departs.

I have attached the data where I have manually calculated the correct trains required to clear a given queue associated with a train, but as an example, for station 2 train 7 departs with 826 people unable to board. It requires the next 4 trains to clear the back of this queue, as the next 4 trains have 87, 312, 312, 312 boarding capacity each. Another example is station 2 train 3 departs with 366 people unable to board, and requires the subsequent 6 trains to clear this queue (49,49,49,38,,87,312 boarding capacity from trains 4-9, until the queue associated with train 3 is cleared).

So any help to develop a formula with the smarts to replicate these numbers is what I am trying to figure out how to do. Hopefully I explained the issue and what I am after but happy to help clarify further, thanks.
Note my current formula i have manually dragged the range of board capacity to figure out how many trains were required, a manual process.

Queue test.xlsx
ABCDEFGHIJK
3Passenger Unable to boardStation 1Station 2trains required to serve end of queueStation 1Station 2
4Train 183122Train 113
5Train 2167244Train 216
6Train 3250366Train 316
7Train 4333488Train 415
8Train 5417610Train 515
9Train 6500732Train 615
10Train 7226826Train 714
11Train 80872Train 803
12Train 90693Train 903
13Train 100513Train 1002
14Train 110334Train 1102
15Train 120154Train 1201
16Train 1300Train 1300
17Train 1400Train 1400
18Train 1500Train 15
19
20Passenger Board capacityStation 1Station 2
21Train 188249
22Train 288249
23Train 388249
24Train 488249
25Train 588249
26Train 688249
27Train 7102338
28Train 8102387
29Train 91023312
30Train 101023312
31Train 111023312
32Train 121023312
33Train 131023312
34Train 141023312
35Train 151023312
Sheet1
Cell Formulas
RangeFormula
J4:J17,K16:K17J4=ROUNDUP(C4/C22,0)
K4,K11:K12K4=IF((SUM(D22:D24)>D4),COUNT(D22:D24),"FALSE")
K5:K6K5=IF((SUM(D23:D28)>D5),COUNT(D23:D28),"FALSE")
K7:K9K7=IF((SUM(D25:D29)>D7),COUNT(D25:D29),"FALSE")
K10K10=IF((SUM(D28:D31)>D10),COUNT(D28:D31),"FALSE")
K13:K14K13=IF((SUM(D31:D32)>D13),COUNT(D31:D32),"FALSE")
K15K15=IF((SUM(D33)>D15),COUNT(D33),"FALSE")
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I couldn't understand the specifics of your problem, so I just looked at the first sentence and tried something for that:

Here I use randarray in A to make some numbers. B1 has the target. B2 is the formula that tells how many numbers in A add up to more than the target.

MrExcelPlayground4.xlsx
ABC
1Numbers300target
2907to get over the targe
352
429
571
621
713
839
912
1015
1132
1240
1351
1452
1581
1631
1736
1855
1972
2057
2151
Sheet17
Cell Formulas
RangeFormula
A2:A21A2=RANDARRAY(20,1,10,90,TRUE)
B2B2=SUMPRODUCT(--(SUBTOTAL(9,OFFSET(A2#,,,ROW(A2#)-ROW(INDEX(A2#,1))+1))<B1))+1
Dynamic array formulas.
 
Upvote 0
Solution
Hi James, thanks for taking the time to help.
That seems perfect, I was able to use your formula on my example and produce the same numbers.
Are you able to give a bit of a explanation as to how it works/how you came up with it? I will have to study those formulas used a bit more but hearing how you broke down the problem would be of great value to me.
 
Upvote 0
This needs a lot of unpacking. I find it's helpful to take bits out of the formula and put them in their own cells.

The OFFSET part gives you an increasing large column of numbers, starting from row 2, then 2:3, then 2:4,... So it goes through each row, and subtracts the row number of the first row and constructs a range. The a2# represents a range like a2:a21 might have done - it works with dynamic arrays - a new feature of 365 I think.

Subtotal(9...) works where SUM does not in these kinds of calculations - it maintains the array.

You see it check that it is less than B1.

So that gives you a load of true, true, true,... false, false, false.

Doing 'identity math' on a true/false, gives you a 1,0. Meaning that you add 0, or multiply by 1, or multiply by -1 and then -1 again (--).

Then summing it up.

MrExcelPlayground4.xlsx
ABCDEFG
1Numbers300target
2368to get over the targe36TRUE18
31551TRUE1
44899TRUE1
543142TRUE1
622164TRUE1
714178TRUE1
875253TRUE1
961314FALSE0
1074388FALSE0
1147435FALSE0
1258493FALSE0
1340533FALSE0
1471604FALSE0
1564668FALSE0
1643711FALSE0
1727738FALSE0
1872810FALSE0
1917827FALSE0
2031858FALSE0
2162920FALSE0
Sheet17
Cell Formulas
RangeFormula
A2:A21A2=RANDARRAY(20,1,10,90,TRUE)
B2B2=SUMPRODUCT(--(SUBTOTAL(9,OFFSET(A2#,,,ROW(A2#)-ROW(INDEX(A2#,1))+1))<B1))+1
D2:D21D2=SUBTOTAL(9,OFFSET(A2#,,,ROW(A2#)-ROW(INDEX(A2#,1))+1))
E2:E21E2=D2#<B1
F2:F21F2=--(E2#)
G2G2=SUMPRODUCT(F2#)+1
Dynamic array formulas.
 
Upvote 0
Really brilliant solution! Even after doing a bit of study on this one, still wrapping my head around it. For the OFFSET reference why use A2# instead of A2 - haven't quite figured out why this one works yet. And why use SUMPRODUCT instead of SUM?

Also do you have any recommended recent books on excel dynamic arrays? Been out of the loop for a bit excel has got quite a few new features.
 
Upvote 0
A2# - is a reference to a dynamic array. It replaces A2:A21. A new feature of 365. A handy way of handling variable array sizes if they are generated as a dynamic array.

Sum would work fine too. you get into the habit of sumproduct because you can have multiple conditions. In this case there is just one condition - that the running sum is less than the target.

No books. Here. Here is the place to learn about dynamic arrays. I've probably doubled my skill in excel (which I had deemed pretty good already) in the last year. Some of the real masters here demonstrate the elegance of brevity so well. You'll come to know them.

I'd recommend solving peoples problems yourself on this site when you can (post the answers or not), but see how some of the masters do it. You'll be very sharp in a short time.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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