Forum Discussion
Machinehandler
Feb 07, 2025Copper Contributor
Calculate which parts make up the total sum
I Have a problem with receiving only a total sum and then I need to figure out what partial sums in a long list that makes up that total. Is this somtehing that is possible for Excel to calculate, or in some other application.
Doing it manually takes a long time and is sometimes impossible.
- MachinehandlerCopper Contributor
Thank you both very much, it will save me alot of time. When learning about the name of the problem, sub set sum problem, I quickly found an online solver that will suffice. Again, thank you.
- Patrick2788Silver Contributor
This sounds like the subset sum problem. There are two ways to solve this problem in Excel: solver or create a Lambda. Last I checked Solver is limited to about 200 variables (For our example, 1 unique number = 1 variable).
With Lambda, the approach I've used is with a bitmask to produce all possible combinations. I avoid using DEC2BIN because of the function's limitations (In this example, it would only be good for 511 combinations. In my demo using only 12 integers, there are 4,095 combinations!). If you have a lot of numbers to consider then another approach would be to use a greedy algorithm.
SubsetSumλ = LAMBDA(integers, target_sum, LET( k, COUNT(integers), C, SUM(COMBIN(k, SEQUENCE(k))), bin, MOD( BITRSHIFT(SEQUENCE(C), SEQUENCE(, k, k - 1, -1)), 2 ), M, bin * TOROW(integers), check, BYROW(M, SUM) = target_sum, result, FILTER(M, check, "None"), result ) );
You may be able to use the Solver add-in for this purpose, but it depends on the size and complexity of the problem.
Could you attach a small sample workbook demonstrating the problem (without sensitive data), or if that is not possible, make it available through OneDrive, Google Drive, Dropbox or similar?