How to Count and Sum the Last N Positive or Negative Values in Excel
Question details
The user needs to dynamically calculate the count and sum of the most recent N positive or negative transactions in Excel without manually updating helper columns.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Calculating profit or loss over a variable number of recent trades (e.g., last 10, 50, or 100 transactions) by extracting the latest qualifying entries.
- Observed behavior
- The user wants to input an integer N into a control cell, causing the spreadsheet to automatically filter values by sign, find the last N matching records, and return their count and total sum.
Ensure your transaction data is formatted as an Excel Table so formulas automatically include new rows. Verify that your version of Excel or WPS Spreadsheet supports modern dynamic array functions like FILTER and TAKE.
Use Dynamic Array Functions (FILTER, TAKE, SUM, ROWS)
This is the most efficient and highly recommended method for modern spreadsheet software. It leverages dynamic arrays to filter and summarize data without needing helper columns.
Modern spreadsheet versions introduced dynamic arrays, allowing you to nest functions like FILTER and TAKE to directly target the exact subset of data you need from the bottom of your dataset.
Select your entire dataset and press Ctrl+T to convert it into an Excel Table. Name the table 'Transactions' and the value column 'Amount' for easier referencing.
Choose a dedicated cell, such as A1 on a separate 'Stats' sheet, and enter your desired number of recent transactions (e.g., 10).
Use the FILTER function to extract only the values you want. For positive values (profits), use the formula =FILTER(Transactions[Amount], Transactions[Amount]>0).
Wrap the FILTER function with the TAKE function. Use a negative sign before your control cell reference to pull from the bottom of the list: =TAKE(FILTER(Transactions[Amount], Transactions[Amount]>0), -Stats!A1).
To get the total sum, wrap the entire formula in the SUM function: =SUM(TAKE(FILTER(Transactions[Amount], Transactions[Amount]>0), -Stats!A1)). To get the count, replace SUM with ROWS: =ROWS(TAKE(FILTER(Transactions[Amount], Transactions[Amount]>0), -Stats!A1)).

Use a Helper Column for Legacy Excel Versions
Best for users on older spreadsheet versions where modern functions like FILTER and TAKE are not available.
Efficiently Analyze Financial Data with WPS Spreadsheet
WPS Spreadsheet fully supports advanced dynamic array functions like FILTER and TAKE, allowing you to instantly analyze the last N transactions without setting up complex helper columns.
- 1. Open your dataset in WPS Spreadsheet: Launch WPS Office, open your financial tracking workbook, and ensure your data is formatted as a Table.
- 2. Define your variable: Designate a control cell for 'N' where you can type the number of recent trades you want to review.
- 3. Apply dynamic formulas: Enter the formula =SUM(TAKE(FILTER(Data[Amount], Data[Amount]>0), -N)) into your summary cell to instantly view the calculated total.

Frequently Asked Questions
What happens if there are fewer than N positive or negative values in my dataset?
If your dataset contains fewer matching values than your specified 'N' in the control cell, the TAKE function will return a #CALC! error. You can prevent this by wrapping your entire formula in an IFERROR function to display a custom message or alternative calculation.
Why does the TAKE function require a negative number?
In the TAKE function, a positive number extracts rows starting from the top (the beginning) of the array, while a negative number extracts rows starting from the bottom (the end). Using a negative reference like '-Stats!A1' ensures you are grabbing the most recent transactions.
Can I use this formula to filter by a specific category instead of positive/negative signs?
Yes. You can easily adjust the condition inside the FILTER function. For instance, instead of filtering for values greater than zero, you can set the condition to filter where a 'Category' column equals 'Sales' or 'Expenses'.




