Search code examples
excelsortingfunctional-programminggroupingcolumnsorting

How do I create groups based on the sum of values?


I haven't been able to find anything like this and maybe I'm looking in the wrong place because I have very limited knowledge in programming so any help would be appreciated.

If there is a way to do this in Microsoft Excel without code that would be preferable, but if code is necessary then please help me by telling me the code is or how I can write it myself.

I need to be able to make separate groups of rows based on the sum of the values in a single column. This can be done either by inserting a "Total" row after a set summation value is reached or by color coding each group that reaches this summation value.

So, the order I see that this needs to happen is:

  1. In one column, of a multi column table, sum the rows in order (if possible, starting on a row of my choosing)

  2. Once the sum has reached a certain value, but doesn't exceed that value, those rows are grouped together

  3. This occurs down the entire length of the table, creating separate groups based on the sum of the rows being as close to the set value as possible

For example:

Starting with this data:

Cust. │Qty.│ Type

A │2│ L

B │4│ XL

C │4│ M

D │9│ S

E │1│ L

F │9│ M

G │10│ L

H │1│ L

I │1│ XL

J │5│ L

K │1│ M

L │5│ S

M │4│ S

N │2│ S

The quantities are summed and checked against the value 10 and then grouped accordingly:

Cust. Qty. Type

A │2│ L

B │4│ XL

C │4│ M

│**Total: 10**│ 

D │9│ S

E │1│ L

│**Total: 10**│ 

F │9│ M

│**Total: 9**│

G │10│ L

│**Total: 10**│ 

H │1│ L

I │1│ XL

J │5│ L

K │1│ M

│**Total: 8**│

L │5│ S

M │4│ S

│**Total: 9**│  

N │2│ S

... etc.

Or by color coding the different groups:

Color Coded Groups

The data table is constantly changing, with old rows being removed, and new ones added. The table is static once the information is pulled, so I would need to be able to apply this easily every time I pull updated data.

Also, if possible, the algorithm needs to be dynamic enough so if I insert, remove, or rearrange the rows that the groups are automatically updated.

Any help, suggestions, or comments would be greatly appreciated. Doing this manually is very time consuming and cumbersome due to the large amount of data that needs to be sorted.

Thank you in advance for the help.


Solution

  • One solution using only excel is this:

    1) Add three additional columns to the table: "Total", "Starts Group", and "Group Number". So the table has 5 columns:

    Customer Quantity Type Total Starts Group Group Number

    2) Add one empty row between the headers and the data rows - this will make crafting and maintaining the formulas easier.

    3) On the third row, which would be the first row with actual data (A | 2 | L), put the following formulas for the three new columns:

    "Total" -> =IF(SUM(B3+D2)>10,B3,SUM(B3+D2))
    "Starts Group" -> =IF(SUM(B3+D2)>10,TRUE,FALSE)
    "Group Number" -> =IF(E3,F2+1,F2)
    

    4) The "Group Number" column contains the information that you want. You can color code the rows using that value. Also, the table should be completely dynamic - you can add/remove rows as you wish and it will get recomputed.

    So your specific example would look like this:

    OrderID  Contract  Price  BuySell  OrderType  Quantity
    1        ZS        10914  Buy                        6
    2        ZS        10916  Buy                        4
    3        ZL         3188  Sell                       9
    4        ZM         3981  Sell                       9
    5        ZM         3985  Sell                       2
    6        ZS        10914  Buy                       10
    7        ZL         3186  Sell                       9
    8        ZM         3982  Sell                      11
    9        ZS        10910  Buy                        2
    10       ZS        10911  Buy                        4
    11       ZS        10913  Buy                        2
    12       ZS        10914  Buy                        4
    13       ZL         3184  Sell                       9
    14       ZM         3983  Sell                      11
    15       ZS        10926  Buy                       10
    16       ZL         3184  Sell                       9
    17       ZM         3983  Sell                      11
    18       ZS        10926  Buy                       10
    19       ZL         3184  Sell                       9
    20       ZM         3983  Sell                      11