How to Calculate Remaining Stock Holdings with Excel GROUPBY
Question details
The user needs to calculate the current remaining shares for each stock in a portfolio by netting out sold quantities from bought quantities and filtering out stocks with a zero balance.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Creating a stock portfolio dashboard to visualize current holdings and exclude stocks that have been completely sold off.
- Observed behavior
- Previous formulas aggregated all bought and sold transactions without calculating the net remaining shares, resulting in a list that included fully sold stocks.
Ensure your transaction data is organized in an Excel Table (e.g., named 'M') with clear columns for Ticker symbols, Transaction Types, and Units.
Calculate Net Stock Holdings Using LET and GROUPBY
Use a combination of LET, GROUPBY, and FILTER functions to aggregate stock units and hide stocks with a zero remaining balance.
The GROUPBY function allows you to aggregate data dynamically. By wrapping it in a LET function, you can store the grouped results as a variable and then apply a FILTER to remove stocks that have a net balance of exactly zero.
Ensure your 'Units' column has positive values for purchases and negative values for sales. If sales are recorded as positive, you will need to adjust the logic to subtract them.
In your destination cell on the Dashboard sheet, enter the formula: =LET(grp,GROUPBY(M[Ticker],M[Units],SUM,0,0),FILTER(grp,TAKE(grp,,-1)<>0)).
To filter out non-share transactions like dividends, modify the data array inside the formula to include a filter, such as filtering the source table where M[Trans]<>"Dividend".

Calculate Remaining Stocks Using a PivotTable
If the GROUPBY function is not available in your Excel version, use a PivotTable to quickly summarize and net out stock holdings.
Manage Your Stock Portfolio with WPS Office
Looking for a free, lightweight, and highly compatible Office suite? WPS Office provides powerful Spreadsheet tools, including advanced formulas and PivotTables, to help you track your stock holdings seamlessly without a costly subscription.
- 1. Download WPS Office: Visit the official WPS website and download the free Office suite.
- 2. Install and Open: Run the lightweight installer and launch WPS Spreadsheet.
- 3. Open your Excel file: Drag and drop your existing .xlsx portfolio tracker into WPS Office to continue analyzing your stocks seamlessly.

Frequently Asked Questions
Why does my formula still show stocks I've already sold?
If your formula only aggregates transactions without netting positive buys against negative sells, or if it lacks a filter for zero balances, fully sold stocks will still appear. Use the FILTER function to hide rows where the total unit count equals zero.
How can I handle sold units if they are recorded as positive numbers?
If both buys and sells are recorded as positive numbers, you must subtract the sales. You can use a SUMIFS formula that subtracts the sum of sales from the sum of purchases, or multiply sold units by -1 in an auxiliary column before grouping.
Can I create dashboard pie charts from this dynamic filtered data?
Yes. Once you have dynamically extracted your remaining holdings using the LET and FILTER formula, you can select the spill range results and insert charts from the Insert tab to visualize your portfolio distribution by industry or ticker.




