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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Reusing power query m code for multiple databases

Hi,

 

I was wondering whether it's possible to reuse power query M code in multiple databases if they have the same or similar data structure?

 

Currently I have a Power BI project which contains a dataset generated from a SQL server database. I used power query editor to construct the data model and it seems to work fine. the various visualizations work fine. I have to create another Power BI project which queries a different SQL server database, but they have the same tables and structure as the original. I want to check whether there's an easy way to reuse the logic in the first project and apply it to the second, rather than copying and pasting the code.

 

Jason

 

 

2 ACCEPTED SOLUTIONS
v-frfei-msft
Community Support
Community Support

Hi @Anonymous ,

 

We can user parameter to work on it. Please refer to the case as below.

https://community.powerbi.com/t5/Desktop/Changing-Data-Source/td-p/497753

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

you can create a function out of your query, and using parameters with the database as input... something like that:

(yourdatabase) =>

let
    Source = Sql.Database(yourdatabase........


If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi all,

 

thanks for your help. Adding the parameters appear to do the trick. 

 

Sincerely,

 

Jason

 

Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

you can create a function out of your query, and using parameters with the database as input... something like that:

(yourdatabase) =>

let
    Source = Sql.Database(yourdatabase........


If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

v-frfei-msft
Community Support
Community Support

Hi @Anonymous ,

 

We can user parameter to work on it. Please refer to the case as below.

https://community.powerbi.com/t5/Desktop/Changing-Data-Source/td-p/497753

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Greg_Deckler
Community Champion
Community Champion

Dataflows



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.