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
saguado
Frequent Visitor

Connect and append multiple SQL tables programmatically/dinamically

Hi,

 

I have an SQL database where every monthly data is inserted into a table, so all tables in database have the same schema. And I would like to know if its possible to create M query to append all of them dinamically (like CSV folder). Below screenshot shows tables names. I'am able to capture table names with simple SQL SELECT from INFORMATION_SCHEMA.COLUMNS, this is giving me a table with all the table names, but don't know how to iterate, connect and append all of them. Thanks in advance for any help or support.

 

S.

 

saguado_0-1654763935667.png

 

1 ACCEPTED SOLUTION

 

Ok, fair enough, but there must still be someone managing the DB that can create a Stored Procedure or similar to combine all these tables.

 

Anyway, that aside, to do this in PQ you do the following:

 

1) Connect to any one of the tables from PQ to generate your Source and Navigation steps.

 

2) Once imported, delete your Navigation step from the Applied Steps list. This should leave you with a table list, something like this:

BA_Pete_0-1654769945814.png

 

3) Filter this list as you would any other table to end up with a list of all the tables you want to append. For your scenario, you would probably apply a filter to the [Name] column something like this:

Text.StartsWith([Name], "HE_")

Leaving the criteria generic like this ensures that the table selection is dynamic as new tables are added.

 

4) Create a custom step like this:

Table.Combine(previousStepName[Data])

 

This should result in a combined table, including all tables that featured in your filtered list.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

3 REPLIES 3
BA_Pete
Super User
Super User

Hi @saguado ,

 

I would very strongly suggest that you speak with your DBA and get them to store all of this information in a single table on your DB. It's pretty bad form to have tables containing similar data to be segmented like this on a DB, especially by date parameters.

 

I think trying to manage this situation via Power Query would be a coding and performance nightmare.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Hi Pete,

 

Thanks for yur prompt answer and I fully agree with your comments, but, the issue is that this database is created by a commercial software, and we cannot change this, so I need to find a way to merge them dinamically.

 

S.

 

Ok, fair enough, but there must still be someone managing the DB that can create a Stored Procedure or similar to combine all these tables.

 

Anyway, that aside, to do this in PQ you do the following:

 

1) Connect to any one of the tables from PQ to generate your Source and Navigation steps.

 

2) Once imported, delete your Navigation step from the Applied Steps list. This should leave you with a table list, something like this:

BA_Pete_0-1654769945814.png

 

3) Filter this list as you would any other table to end up with a list of all the tables you want to append. For your scenario, you would probably apply a filter to the [Name] column something like this:

Text.StartsWith([Name], "HE_")

Leaving the criteria generic like this ensures that the table selection is dynamic as new tables are added.

 

4) Create a custom step like this:

Table.Combine(previousStepName[Data])

 

This should result in a combined table, including all tables that featured in your filtered list.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




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.

Top Solution Authors
Top Kudoed Authors