Google-sheets – Expand Countifs Arrayformula in rows and columns

google sheetsgoogle-sheets-arrayformula

In this google sheet
I have an array of Types and floors, The criteria rows and headers are auto populated by unique formulas.

I'm trying to create an array formula which will expand in the width (columns) and height (rows) infinitely, to lookup the count.

So instead of having an arrayformula in each cell in row 3, there will be just one in F3

enter image description here

Thanks,

Best Answer

Try this query:

={Transpose(query(A3:B46,"SELECT count(A) GROUP BY B label count(A) 'Total'"));query(A3:B46,"SELECT A, count(A) GROUP BY A PIVOT B")}


The formula consists of two queries:

  • Transpose(query(A3:B46,"SELECT count(A) GROUP BY B label count(A) 'Total'"))
    • this counts the number of "Floor" items for each "Type".
    • Note inclusion of transpose to get the output to appear on a single row, and label count(A) 'Total' to generate an appropriate label for the row.
  • query(A3:B46,"SELECT A, count(A) GROUP BY A PIVOT B")
    • this uses the PIVOT clause.

Example output

Screenshot