Exporting Your Queries’ M Code From Power Query And Power BI Using Copy/Paste

Two years ago I blogged about a method to export all the M code for all of your queries in Power Query using the Send A Frown button – useful if you need the code for documentation purposes. This trick doesn’t work with Power BI Desktop, unfortunately, but the good news is that there’s a better way to do this now in Power Query and Power BI Desktop using copy/paste. It’s pretty simple really: when you copy a query from the Power Query or Power BI Desktop Query Editor you can not only paste the query to another Query Editor (pasting from Power Query to Power BI and vice versa works too) but you can also paste the query to a text editor like Notepad and get the M code for the query. What’s more, you can also select more than one query in the Query Editor and when you paste you get all the code for all of the selected queries:

image

Remember that because the properties of each step in a query become comments in your M code, they get copied too.

Thanks to VossF for telling me about this on this thread.

6 thoughts on “Exporting Your Queries’ M Code From Power Query And Power BI Using Copy/Paste

  1. What is the best way to replicate an entire data model (containing Power Queries, Power Pivot tables, measures, linked date tables, Pivot Tables, cube formulas, etc.)? I recently created a data model for one company using one data base. I then needed to duplicate the exact same thing for another company that used the exact same structure (same table and field names) in another data base. I was hoping all I needed to do was change the name of the data base in the Connection of the Excel file. My attempt was not successful.

  2. I was thrilled when I received your reply on the TechNet forum knowing I passed a useful tip on to someone whose work I greatly admire but opening your site and reading the headline of your latest post and being thanked for the tip just filled me with a sincere feeling of belonging to the thriving Power BI community where people like you and many others openly share your knowledge. Thank you.

  3. Really interesting.

    It would make a lot of sense to be able to do the same for the DAX part where all custom columns and measures are easily copy/pastable, both for documentation and reproducibility purposes.

    I hope MS gets there eventually.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s