Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
crejo
Regular Visitor

Loading multiple tables from one SQL query

Take the following PowerQuery M code:

 

 

 

let
  QueryResult = Sql.Database("Server", "database", [Query="select * from MyTable1; select * from MyTable2;"])
in
  QueryResult

 

 

 

This will output One table (the result of select * from MyTable1). Is there a way to get the result from both tables, maybe in a list?

 

Thank you

1 ACCEPTED SOLUTION

Then you would have to package them (for example as JSON or XML) and send them over as a string that you would then untangle in Power Query.  And this is where the fun stops - Power Query cannot create multiple tables from a single query.

 

What's your business case?

View solution in original post

8 REPLIES 8
PwerQueryKees
Super User
Super User

You input comes from 2 tables. In terms of query cost, the difference between 1 query over 2 tables or 2 queries each over 1 table will be negligible with sufficiently large tables. At the end of the day all data needs to be retrieved. 

PwerQueryKees
Super User
Super User

Why not have 2 sql queries? What are you trying to achieve?

The 2 output tables are based on the same collected data. I want to avoid having to rerun the same expensive query twice.

The SQL Server Query cache should take care of that.

lbendlin
Super User
Super User

Do these tables have the same structure? In which case you could say

 

let
  QueryResult = Sql.Database("Server", "database", [Query="select * from MyTable1 union all  select * from MyTable2;"])
in
  QueryResult

The tables do not have the same structure unfortunately.

Then you would have to package them (for example as JSON or XML) and send them over as a string that you would then untangle in Power Query.  And this is where the fun stops - Power Query cannot create multiple tables from a single query.

 

What's your business case?

The query collects data from the database, then based on the data it collected outputs a few tables. This is for temporary reporting purposes. I use power query to read the script, execute it and load it into excel.

 

My current approach to this is the following:

declare @report int = 0

-- REPLACE THIS IN PowerQuery with set @report = x --

-- CODE --

if @report = 1
  select * from Table1

if @report = 2
  select * from Table2

Then I make a PowerQuery query where @report = 1 and one where @report = 2. This will execute the query twice, which is not ideal.

 

Converting the output to XML as you suggested might be a better idea

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.