For some reason I cannot figure out how to combine or use the needed expressions to get the list I need. Here is what I am looking to select (in more of a SQL format):
SELECT TOP 2 [Quantity]
FROM Batch Details
WHERE AND([Batch ID]=[_THISROW].[Batch ID], [Code]=[_THISROW].[Code], [Offer]=[_THISROW].[Offer])
ORDER BY [_RowNumber] DESC
How would I write that using an AppSheet expression? Once I have those two Quantity values, I will wrap them in a SUM() function. So, in other words, I want the two most recent Quantity values that meet the given filtering criteria. Thanks for any help!
Edit: this will return the top 2 row keys; I suppose you wanted the quantity values of these.
You could use a secondary virtual column with INDEX() against this. I’m having a hard thing thinking how to return the top two quantity values as you are describing in a single formula, without having to do two expensive searches.
TOP 2 ROWS : Find the two largest row numbers having the described criteria, Say this list type column is called [Top 2] with following expression something like below
Of the matching batch detail rows, get the 2 with the highest quantity values, then order those two rows by their original row numbers in reverse order (newest first).
Alternatively, grab the two newest of the matching rows, then order by quantity (highest first):
It seems everyone is having the same problem I had. I need the Quantity value from the filtered records, not the ID (key) value. I think I must require this in one expression because I’m using it in a Action where I am setting the Quantity field of a certain row equal to this expression I’m trying to write. I need to SUM() the two most recent Quantity values that meet the given filtered and sorted criteria.
If only the SELECT statement had options for filtering and sorting…
Or if the ORDERBY or FILTER expressions had the ability to return a value other than the key…
That’s it!! Thank you! I’ll be more descriptive next time. I tend to think in SQL and then try to convert that into AppSheet expressions. I would have never come up with that complicated expression. It’s very simple in SQL.