Not sure if this helps, but here is what I did in the attached.
I created a table in Raw Data, O1:Q3 of all the categories that have subcategories. I named each of these ranges by selecting each range individually and entering the main header value as a name in the Name Box just to the left of the formula bar.
E.g. Selected O1:O3 and in the Name Box, entered Apple and repeated for P1:P2 and Carrot and O1:O2 and Mango
Then formula in End Table sheet, B2 is:
which references indirectly the named ranges to find the subcategories to include in the summation.
Copied down and across the matrix.
Adjust ranges to suit... but no whole columns allowed (minimize number of rows in ranges to avoid efficiency issues).
Bookmarks