I have a sheet with a lot of data, but to keep this simple let's say column A has a product, column B has a date, and column C has an integer which can be blank, 1, or an integer greater than 1:
Product Date # Sold
Thing1 2020-9-9 1
Thing2 2020-9-9 2
Thing3 2020-9-9 3
Thing1 2020-8-8 1
Thing2 2020-8-8 2
Thing3 2020-8-8 1
Thing1 2020-7-7 2
I need to find the date on which I sold the 3rd-last of that item:
Product Turn 3 Date
Thing1 2020-7-7
Thing2 2020-8-8
Thing3 2020-9-9
Note that for other reasons I will likely end up with a Query in each cell of the second table anyway, so it doesn't need to get any more complicated than something like:
=query(Table1!A1:C,"Select min(B) where A='"&A2&"' limit X")
where "X" would be how to limit it to the running total of 3.
Thanks in advance for any ideas!
Best Answer
I did finally solve this.
The solution involved first creating another column with cumulative sales of each product. Using the data above:
The formula for the cumulative column looks like this:
The -- has the effect of converting a boolean to a number. The * becomes a logical AND. The sumproduct essentially multiplies the matrices from its arguments.
So, if something in column A is the same as the thing in cell A2 ($A$2:$A=$A2) AND the date is greater than the date on this row, the first argument is a 1 and it gets multiplied by the number of things sold on that row. But, this calculation is looking at all of the other rows on each and every row, so I can imagine that large data volumes might not work well with it.
Anyway, once we have that Cumulative column, it's a pretty simple query to get the date on which the 3rd-last item was sold:
Works like a charm so far!