Maria
Maria

Reputation: 1

How to sort a pivot table obtained with the query function in Google Sheet

I know that it is not possible to "order by" after a pivot in query function, that is why I apply a nested query.

=QUERY(
QUERY(Dataset,"select B,count(B) where A>date'2019-10-01' group by B pivot year(A),month(A)",1),
"Select * order by Col2 Desc ",1)

However the result is not sorted by count(B) (Col2 of the inner query) as a total.
It is sorted only by the Col2 generated because of the pivot. The final table is sorted incorrectly (Option 10 should be in fifth place and not at the bottom).

Example of the result

Any workaround to solve this?

Upvotes: 0

Views: 1663

Answers (1)

marikamitsos
marikamitsos

Reputation: 10573

Instead of a second query use the SORT function outside the first query.

Try something like:

 =SORT(QUERY(Dataset,"select B,count(B) where A>date'2019-10-01' group by B pivot year(A),month(A)",1),2,0)

Upvotes: 1

Related Questions