google-sheetsgoogle-sheets-formula

How to group by additional column?


Here is the data in my range:

Date(A) Color(B) Type(C) Number(D)
1/1/25 White Horse 12
2/1/25 White Horse 55
1/1/25 Blue Horse 76
1/2/25 Blue Dog 31

Desired Output

Date(A) Color(B) Type(C) Number(D)
2/1/25 White Horse 55
1/1/25 Blue Horse 76
1/2/25 Blue Dog 31

I want to combine the duplicate color(B) and type(C) ("White Horse") and show the most recent date and corresponding number(D). Here is the query so far, but cannot figure out how to include column D. I only want it to appear if it is the row with the most recent date.

Query

=query(
  A1:D,
  "select max(A), B, C
   where C is not null
   group by B, C",
  1
)

Solution

  • Use sortn(), like this:

    =let(
      data, sort(filter(A2:D, len(A2:A)), 1, false),
      keys, byrow(choosecols(data, 2, 3), lambda(row, join("→", row))),
      sortn(data, 9^9, 2, keys, false)
    )
    

    The formula lets you use an open-ended reference like A2:D so it will work even when the number of rows isn't known in advance.

    More or less the same can be expressed through a one-liner like this:

    =sortn(sort(A2:D, 1, false), 9^9, 2, 2, false, 3, false)
    

    The difference is that the former formula will run faster and won't return a superfluous blank row at the bottom.

    See sortn().