Google Sheets – How to Query Until Reaching a Particular Sum

google sheetsgoogle-sheets-query

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:

Product  Date      # Sold   Cumulative
Thing1   2020-9-9       1   1
Thing2   2020-9-9       2   2
Thing3   2020-9-9       3   3
Thing1   2020-8-8       1   2
Thing2   2020-8-8       2   4
Thing3   2020-8-8       1   4
Thing1   2020-7-7       2   3

The formula for the cumulative column looks like this:

 =sumproduct(--($A$2:$A=$A2)*--($B$2:$B>=$B2),$C$2:$C)

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:

=QUERY($A$2:$D,"Select B where A='"&A2&"' and D>3 Order By B desc Limit 1")

Works like a charm so far!