Search code examples
google-sheetsgoogle-sheets-formula

Get average of multiple columns


I have a table like the picture below and I would like

  1. to get the average of every column of the table using a query

  2. to sort all the average of every column of the table and keep only the 3 maximum (using a query)

Below is what I tried :

  1. the following query only gives me the average of the first column =QUERY({17:22};"SELECT AVG(Col1) WHERE Col1<>0";1)

  2. for sorting, I guess I will have to use ORDER BY something DESC ? And for limiting values, I guess I will have to use LIMIT 3, isn't it ?

enter image description here

Thanks for your help


Solution

  • You can't query an average across multiple columns to my knowledge while excluding zero's. Therefore you cannot order the results and limit it to 3.

    However if you are willing to accept zero's then this is a query that would work:

    =TRANSPOSE(QUERY(TRANSPOSE(QUERY({A17:I},"SELECT "&ARRAYFORMULA("AVG(Col"&JOIN(",AVG(Col",SEQUENCE(COLUMNS(A:I),1)&")"))&"")),"SELECT * ORDER BY Col2 DESC LIMIT 3"))
    

    UPDATE:

    A super messy solution is to just compile arrays, not very dynamic and requires attention if more days are added:

    =TRANSPOSE(QUERY({TRANSPOSE(A17:I17),{AVERAGEIF(A18:A,">0");AVERAGEIF(B18:B,">0");AVERAGEIF(C18:C,">0");AVERAGEIF(D18:D,">0");AVERAGEIF(E18:E,">0");AVERAGEIF(F18:F,">0");AVERAGEIF(G18:G,">0");AVERAGEIF(H18:H,">0");AVERAGEIF(I18:I,">0")}},"SELECT * ORDER BY Col2 DESC LIMIT 3"))
    

    enter image description here