Search code examples
google-sheetsarray-formulas

Google sheets Query function with Arrayformula


For each of the email id, I want to get latest 10 records by timestamp. How do I get the results with arrayformula? Query function is not important as long as I can still achieve this with arrayformula. Here is the sample data: https://docs.google.com/spreadsheets/d/1YAHA02VM-5MXzVKhkxu_eODPKObpoz441mGX8lOFu5M/edit?usp=sharing


Solution

  • On second thoughts, maybe this is bit cheeky, but this might do it ( taken from conditional rank idea )

    =ArrayFormula(filter(A2:C,countifs(A2:A,">="&A2:A,B2:B,B2:B)<=10,A2:A<>""))
    

    enter image description here

    EDIT

    The above assumes (because the data is time-stamped) dups shouldn't occur. If they do and the data is pre-sorted, you can use row number as a proxy for time stamp as suggested by @Aresvik.

    Alternatively, you could count separately

    (a) only rows with a later timestamp

    plus

    (b) rows with the same time stamp but with earlier (or identical) row number

    =ArrayFormula(filter(A2:C,countifs(A2:A,">"&A2:A,B2:B,B2:B)+countifs(A2:A,"="&A2:A,B2:B,B2:B,row(A2:A),"<="&row(A2:A))<=10,A2:A<>""))