Forum Discussion
Excel Sumifs
- Jun 05, 2025
The only issue was, in your sample table, all "Units" amounts were entered as positive numbers, so all solutions were written to subtract "Sold" from "Bought"; however, in your actual Stocks.xlsx data table, "Sold" amounts were entered as negative numbers. Simply modify any one of the suggested formulas to add "Sold" to "Bought" instead of subtracting.
The GROUPBY method also becomes significantly easier:
=LET( grp, GROUPBY(M[Ticker],M[Units],SUM,0,0), FILTER(grp,TAKE(grp,,-1)>0) )
Or, to include stocks with a negative balance:
=LET( grp, GROUPBY(M[Ticker],M[Units],SUM,0,0), FILTER(grp,TAKE(grp,,-1)) )
Also, I noticed the "Trans" column contains some "Dividend" entries (aside from just "Bought" and "Sold"). If these entries were to have amounts in the "Units" column that needed to be excluded, you can filter them out using the optional [filter_array] argument:
=LET( grp, GROUPBY(M[Ticker],M[Units],SUM,0,0,,M[Trans]<>"Dividend"), FILTER(grp,TAKE(grp,,-1)>0) )
Cheers!
I am not sure which option I prefer
=LET(
holding, GROUPBY(
DemoTbl[Ticker],
IF(DemoTbl[Transaction] = "Bought", 1, -1) * DemoTbl[Units],
SUM, , 0
),
FILTER(holding, TAKE(holding, , -1) > 0)
)
or
=LET(
ticker, SORT(UNIQUE(DemoTbl[Ticker])),
subtotals, SUMIFS(
DemoTbl[Units],
DemoTbl[Ticker], ticker,
DemoTbl[Transaction], {"Bought", "Sold"}
),
holding, BYROW({1, -1} * subtotals, SUM),
FILTER(HSTACK(ticker, holding), holding > 0)
)