Sum all items belonging to a category

LemonMan

New Member
Joined
May 3, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, I am trying to sum up the amounts belonging to certain categories through their ID number but cannot think of any way to find all matching items - xlookup only finds the first item and I can't have a spilling function like filter. The ID number/amount table and ID No./category table must remain separate table. My current function only finds the first result and does not sum all options. I added some sample data (not actual tables or amounts I'm working with) to try to illustrate my issue:

ID No.amount
1245​
23​
2345​
45​
3453​
56​
6546​
78​
7454​
89​
6335​
87​
7456​
98​
ID No.category
1245​
red
2345​
red
3453​
green
6546​
blue
7454​
orange
6335​
red
7456​
blue
categorytotal amount
red
Excel Formula:
=SUMIF($A$19:$A$25,XLOOKUP(A38,$B$29:$B$35,$A$29:$A$35),$B$19:$B$25)
green
Excel Formula:
=SUMIF($A$19:$A$25,XLOOKUP(A39,$B$29:$B$35,$A$29:$A$35),$B$19:$B$25)
blue
Excel Formula:
=SUMIF($A$19:$A$25,XLOOKUP(A40,$B$29:$B$35,$A$29:$A$35),$B$19:$B$25)
orange
Excel Formula:
=SUMIF($A$19:$A$25,XLOOKUP(A41,$B$29:$B$35,$A$29:$A$35),$B$19:$B$25)

Red should equal 155 but the formula only shows 23 (the first result) and so on.

Any help would be greatly appreciated.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How about:

Book1
ABCDEFGH
1ID No.amountID No.categoryCategoryTotal
21245231245redred155
32345452345redgreen56
43453563453greenblue176
56546786546blueorange89
67454897454orange
76335876335red
87456987456blue
Sheet12
Cell Formulas
RangeFormula
G2:G5G2=UNIQUE(E2:E8)
H2:H5H2=SUM(XLOOKUP(FILTER($D$2:$D$8,G2=$E$2:$E$8),$A$2:$A$8,$B$2:$B$8))
Dynamic array formulas.
 
Upvote 1
Solution
An alternative solution with Power Query

Book5
ABCDEFGH
1ID No.amountID No.categorycategorySum
21245231245redred155
32345452345redgreen56
43453563453greenblue176
56546786546blueorange89
67454897454orange
76335876335red
87456987456blue
Sheet1


Load your two tables to Power Query and then Join them as shown below

Power Query:
let
    Source = Table.NestedJoin(Table1, {"ID No."}, Table2, {"ID No."}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(Source, "Table2", {"category"}, {"category"}),
    #"Grouped Rows" = Table.Group(#"Expanded Table2", {"category"}, {{"Sum", each List.Sum([amount]), type nullable number}})
in
    #"Grouped Rows"
 
Upvote 1
Other options are to do them all at once like in column H (no need to copy down) or as in column I (need to copy down)

23 05 04.xlsm
ABCDEFGHI
1ID No.amountID No.categoryCategoryTotal
21245231245redred155155
32345452345redgreen5656
43453563453greenblue176176
56546786546blueorange8989
67454897454orange
76335876335red
87456987456blue
Sum Categories
Cell Formulas
RangeFormula
G2:G5G2=UNIQUE(E2:E8)
H2:H5H2=BYROW(G2#,LAMBDA(rw,SUM(FILTER(B$2:B$8,VLOOKUP(A$2:A$8,D$2:E$8,2,0)=rw))))
I2:I5I2=SUM(FILTER(B$2:B$8,VLOOKUP(A$2:A$8,D$2:E$8,2,0)=G2))
Dynamic array formulas.
 
Upvote 1
How about:

Book1
ABCDEFGH
1ID No.amountID No.categoryCategoryTotal
21245231245redred155
32345452345redgreen56
43453563453greenblue176
56546786546blueorange89
67454897454orange
76335876335red
87456987456blue
Sheet12
Cell Formulas
RangeFormula
G2:G5G2=UNIQUE(E2:E8)
H2:H5H2=SUM(XLOOKUP(FILTER($D$2:$D$8,G2=$E$2:$E$8),$A$2:$A$8,$B$2:$B$8))
Dynamic array formulas.
This is exactly what I was looking for! Very much appreciated kweaver :)
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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