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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Executing list of SQL queries stored in database table

Hello Everyone,

 

I'm looking for an apporach to load data into Power BI from list of SQL queries stored in Postgres database table.

 

SrQuery
1Select ....
2Select ....
3Select ....
4Select ....

 

Essentially, we need to automate query loading process as list of query will increase/decrease with time & manual addition would not be permanent solution.

 

Any inputs if this can be achived & appreciate if you can share your experiences in such scenarios.

1 REPLY 1
OwenAuger
Super User
Super User

Hi @Anonymous 

You can certainly do this sort of thing by first querying the table containing the queries, then using a function like Value.NativeQuery to execute each query.

 

I have attached a small PBIX with parameters you could update to test against your database. I have tested this successfully with a PostgreSQL database on my local machine.

 

I have assumed you want to union the results of the queries. If you want to do something different, you would need to change the final steps of CombinedQueries.

 

Below is the M code for the queries in the PBIX. The four parameters should be changed as needed for your data source.

 

 

// Server
"localhost" meta [IsParameterQuery=true, Type="Text", IsParameterQueryRequired=true]

// Database
"OwenSandbox" meta [IsParameterQuery=true, Type="Text", IsParameterQueryRequired=true]

// Schema
"public" meta [IsParameterQuery=true, Type="Text", IsParameterQueryRequired=true]

// Table
"MyQueries" meta [IsParameterQuery=true, Type="Text", IsParameterQueryRequired=true]

// DatabaseConnection
let
    Source = PostgreSQL.Database(Server, Database)
in
    Source

// CombinedQueries
let
    Source = DatabaseConnection,
    QueryTable = Source{[Schema=Schema,Item=Table]}[Data],
    #"Add Query Result" = Table.AddColumn(QueryTable, "Query Result", each Value.NativeQuery(DatabaseConnection,[Query],null,[EnableFolding = true])),
    #"Select Query Result" = Table.SelectColumns(#"Add Query Result",{"Query Result"}),
    // Union the query results. Change as required.
    Combine = Table.Combine(#"Select Query Result"[Query Result])
in
    Combine

 

 

 

Hopefully that helps a bit! Please post back if needed 🙂

 

Regards

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors