Apple Numbers: Filtering pivot table for top/bottom 20 items

I’ve created a day trading spreadsheet and I am trying to track, among other metrics, the top 20 gainers and losers. The source table has hundreds of rows of data, with tickers appearing more than once throughout, and the two (top 20 & bottom 20) pivot tables almost work but always display MORE than 20 stock tickers. Top is usually 26 and bottom 21 or 22.

It’s not a huge deal but I would like to correctly limit the resulting bar charts to 20 bars each.

Any ideas on what I need to do to make these pivot table have only 20 rows?

Note: for stocks that appear multiple times, I want them added together and for their sum to be used in the sorting. The summing is being done properly and a stock traded multiple times does appear only once in the pivot table, with the correct profit or loss amount as column two.

Thanks.

iPhone 15 Plus, iOS 18

Posted on Mar 9, 2025 4:15 PM

Reply
16 replies

Mar 10, 2025 12:39 PM in response to RustyWiring

I don't have your data, so can't tell, but it may also be worth considering that the tables are built based on the cell underlying value, not the display value. That means that the Pivot Table would consider $100.00 and $100.00001 as different values even though they both display as $100.00. If you have fractional holdings or any kind of rounding on the values, that could affect the results.


Second, as a completely hokey workaround, if you absolutely want 20 rows then you could add one level of indirection. Have your pivot tables as you do now, with any number of rows, and create a separate table of 20 rows (plus headers, etc.) that directly reference the top 20 values from the pivot table. That way, even if the pivot table shows 25, 30, or even 100 values, your indirect table only references the first 20 rows. Hokey, like I said, but sometimes that's what we have to resort to. :)

Mar 13, 2025 2:27 PM in response to RustyWiring

Oh, it looks like Numbers is trying to be too smart with its references to the pivot table.


I vaguely recall seeing this before, but it wasn't critical to my workflow, so I ignored it.


The problem is that when you built the second-level table and linked it to a cell in the pivot table, Numbers internally referenced the underlying value of that cell.


When you update the pivot table, it adjusts all those links to maintain references to that specific data not the specific cell row/column in the table. If the stock that was in A3 is now no longer listed, the reference is invalid and Numbers (conveniently?) removes it from the lookup. That's the root of why there's a gap in the lookup range.


I guess in some world that makes sense. Just not in yours :)


All is not lost, though. You can break that relationship via the INDIRECT() function, and it's actually a little easier.


In the Top 20 table, replace the INDEX() functions with something like:


=INDIRECT("Pivot: Perf by Ticker, Top 20::$A$3").


This will grab the actual value from cell A3, without trying to track it through the pivot shenanigans as it moves up/down (in/out?) of the table.

Mar 10, 2025 11:39 AM in response to RustyWiring

I vaguely remember coming across this before. If I remember rightly, it has something to do with unique values.


When you ask for 'top 20', Numbers sorts the top 20 values, then shows all cells that match those values. If you have multiple cells that have the same value, they're all included because it's showing the top 20 values, not the top 20 cells sorted by values.


I don't recall the specifics, but there was some workaround that I'll have to dig out, but I don't think I was using pivot tables at the time, so that may be an issue.


If you want to force 20 rows then you may have to resort to a static table, not a pivot table.

Mar 10, 2025 12:08 PM in response to Camelot

I suspected something along that line. Yes, a profit or loss value appears multiple times. In the table snapshot above for losers, there are four duplicated values yet there are 22 rows. I would have expected 24 rows, if your reasoning and my suspicion held. Odd things happen when I reduce the ‘top’ or ‘bottom’ amount. I tried top 1 to top 20 and the values jump in, what looks to me, strange ways. Once specific oddity is symbol OSTX. When I filter for top 1-6 or so, it has a lesser value - a value corresponding to one trade only, although it was traded several times and should have a greater value. As I raise the ‘top’ filter to 7 and beyond, I get that greater value in the pivot table and I also start getting more rows appearing than the filter is set to. So, it certainly does seem to have something to do with repeat values in the source table.

Not the end of the world, by any means.

Mar 11, 2025 12:04 AM in response to RustyWiring

RustyWiring wrote:

I’ve submitted a request to Apple (yeah, right) to make pivots update automatically.


I doubt whether you (or I) would be happy with the results of that.


Pivot Tables take a lot of processing power and would slow down the interface if they constantly updated.


For that reason, both Excel and Numbers require you to "refresh" them, which is just one click.


SG

Mar 13, 2025 11:03 AM in response to Camelot

Maybe I spoke in haste…

Guys, I’ve tried a few ways to copy the top 20 rows of the sorted pivot tables but, when I update the source table and, then, the two pivot tables, I am getting ‘holes’ in the third ‘copy’ tables. I thought INDEX was the way to go but I must not understand something. In the attached photo, you see the bar chart with the ‘missing tooth’; its source table is the white table at the bottom right with the ‘0’ cell highlighted. Yesterday, the INDEX parameter for the pivot table was a simple range within the pivot table. After updating today, it’s now a broken range. There’s a new entry today in the second row of the pivot table and that is where the ‘hole’ is in the chart. This has occurred the last few days - whenever a cell in the top 20 is replaced/update with a new ticker and value. INDEX seems intent on attaching itself to the content of pivot table cell instead of the cell itself.

Any ideas how I should ‘hard copy’ the elements from the pivot table to the chart’s source table?

Mar 13, 2025 11:44 AM in response to Camelot

Well, by good fortune, the top losers pivot isn’t changing at the bottom. So, its INDEX ranges are all complete. I’ve fixed the top gainers table several days now and each time a change occurs in the top 20…boom. I first tried directly selecting the pivot cells from an “=“ in each chart table cell and the same thing happens. Actually, I think an error triangle appears, if I recall correctly, upon a shifting out or in of content.

This ought to be easy. I must be missing something so obvious that I’ll kick myself later.

Mar 13, 2025 3:00 PM in response to RustyWiring

> That yields an invalid reference error.


You did it wrong ;)


It looks like you entered INDIRECT() then provided a link to the cell. That's not how it should be set.

You, effectively, type out the reference to the cell that you want. Note my example is a quoted string, including the table name and column/row numbers. INDIRECT() decodes the string in real time to work out where to go look for data.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Apple Numbers: Filtering pivot table for top/bottom 20 items

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.