Combine Two Tables with Unequal Rows Horizontally in Google Sheets

Using the HSTACK function, we can combine two tables or two Query formula results with unequal rows horizontally in Google Sheets.

I can explain it in simple terms.

I have two tables. The first table consists of three lines (C1:D3), while the second table consists of four lines (F1:G4).

Here I am combining the above two tables with unequal numbers of rows horizontally in Google Sheets.

This is our usual approach with Curly Brackets, which would return an error #REF.

={C1:D3,F1:G4}

The tooltip will remind you that function ARRAY_ROW parameter 2 has a mismatched row size. Expected: 3. Actual: 4.

How to overcome this? How do I properly combine two tables with unequal numbers of rows horizontally in Google Sheets?

Here is how to combine two tables with unequal numbers of rows horizontally.

Combine Two Tables Having Different Numbers of Rows Horizontally

Below you can find two types of formulas, and feel free to skip the old-school approach.

Old School Approach (Two Physical Tables)

The two physical tables to combine are in the C1:D3 and F1:G4 ranges/arrays.

Here are the steps to horizontally combine them in Google Sheets.

Steps

First of all, generate 100 rows with error values. We can use the Sequence function to get those error value rows.

=ArrayFormula(sequence(100,2)/0)

The number 100 represents a hundred rows, and the number 2 represents two columns. Remember, we have two columns in each table.

Generating n blank rows in sheets

Let’s add these 100 rows of error values below the tables. Before that, we should make the #DIV/0 errors blanks. How?

Wrap the above Sequence formula with IFERROR. Here is the table after adding 100 blank rows below.

Table # 1:

={C1:D3;iferror(ArrayFormula(sequence(100,2)/0))}

Table # 2:

={F1:G4;iferror(ArrayFormula(sequence(100,2)/0))}

Now let’s constrain the rows in each table to 10 using Array_Constrain. Here are those formulas.

Table # 1:

=array_constrain({C1:D3;iferror(ArrayFormula(sequence(100,2)/0))},10,2)

Table # 2:

=ARRAY_CONSTRAIN({F1:G4;iferror(ArrayFormula(sequence(100,2)/0))},10,2)

Now we can combine these two tables horizontally as both tables have ten rows each.

={
array_constrain({C1:D3;iferror(ArrayFormula(sequence(100,2)/0))},10,2),
array_constrain({F1:G4;iferror(ArrayFormula(sequence(100,2)/0))},10,2)
}

Let’s just fine-tune the formula.

FORMULA 1

=ArrayFormula(
     {
     array_constrain({C1:D3;iferror(sequence(100,2)/0)},10,2),
     array_constrain({F1:G4;iferror(sequence(100,2)/0)},10,2)
     }
)
Combining two physical tables with unequal numbers of rows

This way we can combine two physical tables having unequal numbers of rows horizontally in Google Sheets.

Note

When you combine tables horizontally, the number of columns won’t make any issue. But when generating error rows, do remember to use Sequence formulas accordingly.

For example, if Table 1 has two columns and Table 2 has five columns, use the Sequence formulas as follows.

Table # 1:

=ArrayFormula(sequence(100,2)/0)

Table # 2:

=ArrayFormula(sequence(100,5)/0)

There are no other changes in the formula.

New Approach (Two Physical Tables): Recommended

With the help of HSTACK, we can combine two or more tables having mismatching rows horizontally.

In the above example, we can use the following HSTACK formula.

It adds #N/A cells to match the number of rows. The IFERROR removes them.

=IFERROR(HSTACK(C1:D3,F1:G4))

Combine Two Query Results with Unequal Numbers of Rows Horizontally

Old School Approach (Two Virtual Tables)

The real purpose of this tutorial is not to help you combine two physical tables with different numbers of rows horizontally. Then?

We want to combine two Query formula results with unequal numbers of rows horizontally.

I know the above example may help you learn the logic. Now things are easy for you.

You want to change the two table references in the above formula (FORMULA 1), i.e., C1:D3 and F1:G4, with two Query formulas.

Sample Data: Two Tables and Two Summaries

G1 Formula:

=query(A1:B,"Select A, sum(B) where A is not null group by A",1)

J1 Formula:

=QUERY(D1:E,"Select D, sum(E) where D is not null group by D")
Sample data and query formulas as tables

Let’s consider the above two Query formula results in our test as two tables. We are unsure whether the Query formula results have an equal number of rows.

Here we can follow the same approach which we have chosen to combine two physical tables with unequal numbers of rows horizontally.

In that formula, replace C1:D3 with the G1 formula and F1:G4 with the J1 formula. It will be as follows.

=ArrayFormula(
     {array_constrain(
        {query(A1:B,"Select A, sum(B) where A is not null group by A",1);
        iferror(sequence(100,2)/0)},10,2),
     array_constrain(
        {QUERY(D1:E,"Select D, sum(E) where D is not null group by D");
        iferror(sequence(100,2)/0)},10,2)}
)
Combine two Query functions with unequal numbers of rows

New Approach (Two Virtual Tables): Recommended

It is pretty straightforward. Enter the two Query formulas within HSTACK as comma-separated and wrap with IFERROR.

That’s it.

=IFERROR(
     HSTACK(
        QUERY(A1:B,"Select A, sum(B) where A is not null group by A",1),     
        QUERY(D1:E,"Select D, sum(E) where D is not null group by D")
     )
)

Example_Sheet_9720

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Compare Two Tables for Differences in Excel

To compare two tables with similar data for differences, you can use the XLOOKUP...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

How to Retrieve the Last Record in Each Group in Excel

As part of data analysis, you may need to retrieve the last record from...

More like this

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

Filter Data with Multi-Select Drop-Downs in Google Sheets

If you've started using multi-select drop-downs in Google Sheets, you may encounter challenges when...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.