Search code examples
sortinggoogle-sheetsgoogle-sheets-formulatransposegoogle-query-language

Sort Query Pivot Table - Google Sheets


I am trying to sort a query pivot table in order that the columns should be in descending order.

I tried:=QUERY({Data!A1:C24},"Select Col1, Sum(Col2) group by Col1 pivot Col3 order by Col1 Desc, Col2 Desc") but it tells me that "COL_IN_ORDER_MUST_BE_IN_SELECT: Col2".

Is there a way to sort the columns of the pivot table in addition to the rows?

Here is a sample sheet: https://docs.google.com/spreadsheets/d/1W8T2BJvSRN_CMdTs1asONEWrDjT3c5yDTFRxh8mSh10/edit?usp=sharing


Solution

  • try:

    =TRANSPOSE(QUERY(TRANSPOSE(QUERY({Data!A1:C24},
     "select Col1,sum(Col2)
      group by Col1
      pivot Col3
      order by Col1 desc")), 
     "order by Col1 desc", 1))
    

    0