Search code examples
google-sheetsgoogle-query-language

Last Date - Query - Google Sheets - Duplication


I have a table with the following data:

Column A: Country Page

Column B: Link to post

Column C: Date posted

I created the following query to get the last date each Country Page posted:

=QUERY('Post Level'!A:C, "select A, max(C) where A<>'#N/A' and A<>'' group by A label max(C) 'Last Posted Date'",1)

The query works perfectly. I'd like to add also a link to the last post. So I changed it to:

=QUERY('Post Level'!A:C, "select A,B max(C) where A<>'#N/A' and A<>'' group by A,B label max(C) 'Last Posted Date'",1)

This version doesn't work. I get duplicate entries for each country.

Ideally I would like to see: Country Page, Last Date Country Posted, Link to last post


Solution

  • Well here is a proof of concept that it is do-able:

    =ArrayFormula(iferror(vlookup({unique(A:A)&query(A:C, "select max(C) group by A offset 1 label max(C) 'Date'")},{A:A&C:C,B:B},2,false),""))
    

    So I am concatenating the Country and Last Date and using them as a lookup value to get the associated Link.

    The reason for using Unique rather than a query to get the list of countries is that you can't just put

    select A group by A
    

    You have to have at least one aggregate. As long as unique returns the countries in the same order as the group by (which it should do), this will work correctly.

    enter image description here