google-sheets

How to stack multiple queries indirectly defined in an array


I'm struggling with an "optimisation" issue and hope someone could have a genius hint ;-)

Context

I have a bunch of sheets which contains "compatible but heterogeneous" data => each sheet contain same type of data but structured different ways.

Goal

My goal is to produce a table in another sheet which aggregate all data from all sheets described in "Context" Hence I set up a "parameters" sheet in which I define all queries to run, listing all named ranges and corresponding query to do on this range.

Issue

I can build and run any query without any problem, but can't sort stacking output all queries of the configuration table

Here is a sandbox workbook which describes my issue

Hope all is clear with this description.

Any thought on this?

Thanks to community!

Emmanuel


Solution

  • You can stack the results of multiple queries using REDUCE

    =LET(
       ranges; A4:A5;
       queries; I4:I5;
       REDUCE(
         TOCOL(;1);
         SEQUENCE(ROWS(ranges));
         LAMBDA(result; i; VSTACK(
           result;
           QUERY(INDIRECT(INDEX(ranges; i)); INDEX(queries; i))
         ))
       )
     )