Tommi
Tommi

Reputation: 11

Power Query - Appending two tables but the other table might be empty depending on the situation - throws an error in that case

I am working on a solution that involves merging two queries in Power Query to retrieve a single data table back to Excel. The first query is always populated but the other query comes from an ERP and might be empty (empty table) from time to time.

Appending the two queries involves making the header names the same in the two queries before the appending takes place. As the second query sometimes results in an empty table, the error arises in the steps when Power Query is modifying the header names in the second table (it cannot modify the header names as there are no headers).

"Error message: Expression.Error: The column 'PartMtl_Company' of the table wasn't found. Details: PartMtl_Company" where the PartMtl_Company is the leftmost column in my table.

I am kind of thinking that I would need to evaluate whether the second table is empty and skip the renaming steps if that is the case. I assume merging the populated first table with an empty table would cause no problem and would only result in the first table. I have tried to look around for a suitable M-code but have not come across such.

Upvotes: 0

Views: 2187

Answers (2)

Tommi
Tommi

Reputation: 11

Thank you Marc! That did the trick.

In the end, I wrote some in the lines of

= if Table.RowCount(Table2) > 0 then... (code that works on a non-empty table) ...else Table2

, which returns the empty table if it is empty to begin with. Appending the second table into the first table did not throw an error but returned only the first table like planned.

Upvotes: 0

Marc Pincince
Marc Pincince

Reputation: 5202

I'm thinking you might be able to use Table.RowCount to solve this. Something along the lines of:

= if Table.RowCount(Table2) > 0 then... 

You would modify the headers only if there is data in the second table. Same goes for the appending of the tables: you would only append if there is data in the second table, since you won't have renamed any headers otherwise.

Upvotes: 1

Related Questions