Google Sheets Countunique in last n rows

95 Views Asked by At

I have a row of names of restaurants in Google Sheets, but I only want to countunique() the last n cells in the column without having to constantly update the range of the function manually. This is all being done on a pivot table as well.

E.g.:

n = 5

Raw Data

  • McDonald's
  • Starbucks
  • McDonald's
  • McDonald's
  • Chipotle
  • Jack's
  • Five Guys
  • Chipotle

Pivot Table

  • McDonald's | 1
  • Starbucks_ | 0
  • Chipotle__ | 2
  • Jack's____ | 1
  • Five Guys_ |1
1

There are 1 best solutions below

2
On

try:

=QUERY(QUERY(A:A, 
 "offset "&COUNTA(A:A)-B1), 
 "select Col1,count(Col1) 
  where Col1 is not null 
  group by Col1
  label count(Col1)''")

0


or:

=ARRAYFORMULA(QUERY({UNIQUE(A:A), 
 IFNA(VLOOKUP(UNIQUE(A:A), QUERY(QUERY(A:A, 
 "offset "&COUNTA(A:A)-B1), 
 "select Col1,count(Col1) 
  where Col1 is not null 
  group by Col1
  label count(Col1)''"), 2, 0))*1}, 
 "where Col1 is not null"))

enter image description here