简体   繁体   中英

EXCEL: How to automatically create groups based on sum being less than X and not greater than Y

I have table in Excel with some information, the main column is Weight (in KG). I need Excel to group Rows into groups, where each group's sum of Weight (in KG) is less than 24000 kg and greater than 23500 kg. To do so manually is very time consuming, since there are thousands of rows with different Weight values.

table example:

ID | Weight (KG)
1 | 11360
2 | 22570
3 | 10440
4 | 20850
5 | 9980
6 | 9950
7 | 19930
8 | 9930
9 | 9616
10 | 9580
... and so on

The closest I got to solving the problem is adding 3 new columns: Total, Starts Group and Group Number.

Total function: =IF(SUM(B3+C2)>24000,B3,SUM(B3+C2)) - calculates current sum of Weight values in the current group
Starts group function: =IF(SUM(B3+C2)>24000,B3,SUM(B3+C2)) - checks if current row makes a new group
Group number function: =IF(D3,E2+1,E2) - all rows that contain same number are in the same group

The problem with this is that it doesn't create groups that are greater than 23500 too, but only that are less than 2400 kg.

It doesn't have to be in Excel, any app/script would work too, it just has to get the job done.

Desired output:
ID | Weight (KG) | Group ID
1 | 11360 | 1
2 | 2570 | 2
3 | 10440| 1
4 | 20850 | 2
5 | 180| 2
6 | 1950 | 1

So i want to get groups similar to these:
Group number 1 - Total 23750kg
Group number 2 - Total 2360kg

Url to my example table with functions I added:
https://1drv.ms/x/s!Au0UogL2uddbgTFJJ4TzSKLhPFPE?e=r02sPX

You may want to try this for total:

=IF(SUM(B3+C2)>24000;B3;IF(SUM(B3+C2)<=23500;SUM(B3+C2);B3))

edit:

I just saw you pasted the proposal into your sample file. You may need to replace the ; with , due to regional format settings.

The limitation remains:

first priority is <24k and second priority is >=23.5k If the next row's value makes the “jump” above 24k you may end up remaining below 23.5k and switching to the next group

edit2:

You may want to look up some optimization models and algorithms for your combination problem before trying to implement it in Excel.

Or try with simple rules, eg categorizing your rows such as weight over 20k, 16k, 12k,8k, 4k, 2k, 1k, 500, etc. and try to group/combine them accordingly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM