Calculation Issues

How to Calculate Remaining Stock Holdings with Excel GROUPBY

Natalie TaylorNatalie Taylor Nov 10, 2026 869 views

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.

How to Calculate Remaining Stock Holdings with Excel GROUPBY
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.
Before you start

Ensure your transaction data is organized in an Excel Table (e.g., named 'M') with clear columns for Ticker symbols, Transaction Types, and Units.

Solution 1Recommended

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.

1
Verify unit signs

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.

2
Apply the GROUPBY formula

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)).

3
Exclude specific transaction types

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 Net Stock Holdings Using LET and GROUPBY
Function Availability: The GROUPBY function is available in newer Microsoft 365 builds. If your version does not support it, consider using alternative methods like SUMIFS or PivotTables.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website and download the free Office suite.
  2. 2. Install and Open: Run the lightweight installer and launch WPS Spreadsheet.
  3. 3. Open your Excel file: Drag and drop your existing .xlsx portfolio tracker into WPS Office to continue analyzing your stocks seamlessly.
Free to use with a fast, lightweight installationFully compatible with Microsoft Excel (.xlsx) formatsFamiliar user interface for a seamless migrationPowerful data analysis tools and pivot tables for financial tracking
microsoft office alternative - wps office

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.