Home > database >  How to count the number of contiguous blocks of cells, each block comprising of the same cell value?
How to count the number of contiguous blocks of cells, each block comprising of the same cell value?

Time:07-21

In Google Sheets, I have a sheet with a list of customers.
Row 1 has headers, and data starts in row 2.
Column A is Customer name,
Column B is street address,
Column C is City and Post Code,
Column D is Country.

I would like to count the number of occurrences of each customer's record, i.e. when A, B, C, D are the same as a composite key.

However, I want to count different occurrences of a row ONLY IF those occurrences are not adjacent / concurrent, i.e.
I do want to count separate occurrences if row 5 and 7 have the same customer,
but not if row 5 and 6 have the same customer...in this case I will count it as one occurrence

e.g.
row 5:
A. London Eye
B. Riverside Building, County Hall,
C. London SE1 7PB
D. United Kingdom
row 6:
A. London Eye
B. Riverside Building, County Hall,
C. London SE1 7PB
D. United Kingdom
row 7:
A. London Eye
B. Riverside Building, County Hall,
C. London SE1 7PB
D. United Kingdom
COUNT: 1

row 5:
A. London Eye
B. Riverside Building, County Hall,
C. London SE1 7PB
D. United Kingdom
row 7:
A. London Eye
B. Riverside Building, County Hall,
C. London SE1 7PB
D. United Kingdom
row 9:
A. London Eye
B. Riverside Building, County Hall,
C. London SE1 7PB
D. United Kingdom
COUNT: 3

row 5:
A. London Eye
B. Riverside Building, County Hall,
C. London SE1 7PB
D. United Kingdom
row 6:
A. London Eye
B. Riverside Building, County Hall,
C. London SE1 7PB
D. United Kingdom
row 8:
A. London Eye
B. Riverside Building, County Hall,
C. London SE1 7PB
D. United Kingdom
COUNT: 2

Sample sheet:
enter image description here

Explanation Pending

1 - Search_key of the enter image description here

2.a - To get the left column we need ArrayFormula(CONCAT(CONCAT(QUERY(UNIQUE(A2:B)," Select Col1 where Col1 is not null ")," "),QUERY(UNIQUE(A2:B)," Select Col2 where Col1 is not null ")))

  • We get the UNIQUE(A2:B) and QUERY only non empty Col1 values with the query set to " Select Col1 where Col1 is not null "

  • concatenate the output with CONCAT function with " " like this CONCAT(QUERY(UNIQUE(A2:B)," Select Col1 where Col1 is not null ")," ")

  • once more concatenate the output with CONCAT function with " " like this CONCAT(CONCAT(QUERY(UNIQUE(A2:B)," Select Col1 where Col1 is not null ")," "),QUERY(UNIQUE(A2:B)," Select Col2 where Col1 is not null "))

  • wrapped in a ArrayFormula Like this ArrayFormula(Output)

2.b - To get the right column we need QUERY(SEQUENCE(COUNTA(ArrayFormula(CONCAT(CONCAT(QUERY(UNIQUE(A2:B)," Select Col1 where Col1 is not null ")," "),QUERY(UNIQUE(A2:B)," Select Col2 where Col1 is not null ")))),1,1,1)

  • Create a SEQUENCE With rows set to the COUNTA of the Output of step 2.b which is ArrayFormula(CONCAT(CONCAT(QUERY(UNIQUE(A2:B)," Select Col1 where Col1 is not null ")," "),QUERY(UNIQUE(A2:B)

  • [columns] Set to 1.

  • [start] Set to 1.

  • [step] Set to 1.

  • QUERY Col1 of the Output of SEQUENCE with query set to " Select * where Col1 is not null ".

3 - Set the index of VLOOKUP to 2 and [is_sorted] to 0.

  • Related