Search code examples
google-sheets

QUERY + IMPORTRANGE - ORDER BY 2 columns


I'm fetching and querying data from 2 spreadsheets, each with the same 5 columns. I want to display the spreadsheets horizontally and sort each spreadsheet by its 5th column so that I can compare the top results for each.

In the merged dataset, it will be columns E (1st dataset) and J (2nd dataset). I could only come up with the function for sorting the first column, is it even possible to apply sorting to both columns simultaneously?

=QUERY({IMPORTRANGE("spreadsheet_url", "A1:E"), IMPORTRANGE("spreadsheet_url", "A1:E")}, "SELECT * WHERE Col4 = 'no' ORDER BY Col5 desc")


Solution

  • You can use hstack with iferror:

    =iferror(hstack(
    query(importrange("URL","Sheet1!a1:e"),"SELECT * WHERE Col4 = 1 ORDER BY Col5 desc"),
    query(importrange("URL","Sheet2!a1:e"),"SELECT * WHERE Col4 = 1 ORDER BY Col5 desc")))
    

    enter image description here