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:


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.

11 responses

  1. Pingback: Exporting All M Code From Power Query In Excel 2013 – Chris Webb's BI Blog

  2. 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.

  3. 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.

  4. 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.

  5. Pingback: Extracting Power Queries | Query Power 📊

  6. Pingback: #Excel Super Links #8 – shared by David Hager | Excel For You

  7. Pingback: Thoughts On The New Power Query Source In SSIS « Chris Webb's BI Blog

  8. Pingback: Thoughts On The New Power Query Source In SSIS - Beyond Group

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: