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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Iterate Queries Based On Other Query

Hello,

I am creating a Power BI query for a system running on on-prem SQL Server (2016).  Basically, the system has multipe departments that has its own database with identical schema. I need to create a query that runs through all departments (databases) and get rows from a table (office).  So I was able to get the list of departments (databases) with the following:

 

let
    Source = Sql.Database("dbserver01/db01", "master", [Query="SELECT name FROM sys.databases"])

 

But now I need to loop through this result and query other databases in the following manner:

 

<some looping with results above>
    let
        offices = Sql.Database("dbserver01/db01", Source[name], [Query="SELECT officeName FROM Offices"]

 

Is there a way to loop through Source and create sub-queries for each databases and return the result of combined officeName across the databases?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Anyways, the solution is to create the query with parameter as the database name and return the office names for a given department. Then you can convert this to a function with the parameters getOffices(DepartmentName).

 

Then create another query to the master database to get the department name and use Table.TransformRows(Departments, each getOffices(_[dbName])) then convert the resulting list to Table.  

 

This is much shorter version of what I've originally typed. Kinda not sure if I should mark this as the answer...

View solution in original post

5 REPLIES 5
camargos88
Community Champion
Community Champion

@Anonymous ,

 

Creating a view querying these databases is not an option ?



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Anonymous
Not applicable

Technically, I could hardcode all the databases and append queries, but we have over 30 departments and needs to account for new departments being added. It would be ideal to do it programmatically if possible.

@Anonymous ,

 

You can create a dynamic select inside a procedure to execute it.

Also it's possible to do it using Power Query by creating a function to select in each database.

I'm gonna work on an example here.



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Anonymous
Not applicable

Seriously, Microsoft, after taking a long time and great effort to type a painfully long solution to my question, and your forum software just ditches the work and tells me I am not signed in?

Anonymous
Not applicable

Anyways, the solution is to create the query with parameter as the database name and return the office names for a given department. Then you can convert this to a function with the parameters getOffices(DepartmentName).

 

Then create another query to the master database to get the department name and use Table.TransformRows(Departments, each getOffices(_[dbName])) then convert the resulting list to Table.  

 

This is much shorter version of what I've originally typed. Kinda not sure if I should mark this as the answer...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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