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

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
Super User
Super User

Dataflows


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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