TL;DR Another place to find your Power BI queries is in the data sources of the embedded tabular model. They are stored in a zip file which is encoded as a base64 string in the Mashup field of the connection string.
While we are waiting for Microsoft team to release new features that would allow exporting Power Query queries outside Power BI ecosystem, we might as well explore all of the other possibilities available to us. So far I’ve counted 3 ways of getting a hold of our precious M queries:
As I’ve been poking around this black box that is Power BI, I have discovered another place where M queries are stored — the data source connection string of the embedded tabular model. The steps that I took in order to get there are:
- Find the port number on which the $Embedded$ Tabular model is running on
- Connect to that Tabular instance and get the Connection Strings of the Tabular Schema Data Sources
- Parse the Connection Strings for Mashup field
- Convert the base64 Mashup field into a binary file
- Unzip the binary file and display Formulas/Section1.m file
Here is me trying to do everything in one go via PowerShell.
When it comes to scripting, the power of the Shell is far greater than that of the Query, but in this case all of the prerequisites are there to replicate the above steps in pure M.
Finding $Embedded$ Tabular port number
I’ve tried to solve this problem before via R scripts, however as Mim pointed out to me, there is a simpler Power Query way of doing this. Because Power BI stores its auto generated port number for the tabular model in a text file (msmdsrv.port.txt) under AnalysisServicesWorkspaces, the task is reduced to reading the contents of that file.
By the way my approach uses a short circuit logic, assuming that the NTFS Access Date records for C:/Users are up to date, apparently they might take up to an hour to update. So if you find this script misbehaving have a look at Imke’s blog post for the original script.
Get Tabular Schema Data Sources
You can read about the latest undocumented Tabular DMVs from Meagan’s blog, or fire up SQL Server Profiler, check the boxes for End Command events, and script out the tabular model from SSMS and you’ll discover a plethora of DMV being displayed in the profiler’s trace. DMV hiding all of the connection strings is TMSCHEMA_DATA_SOURCES.
Extract Mashup field into binary
I’ve encountered some difficulties parsing the connection string with a Text.Split function so I’ve used a Splitter function instead.
Unzip Formulas/Section1.m from Mashup binary
I’ve seen at least 2 functions for unzipping files in Power Query written by KenR and Mark White. Also I’ve described before how to call R unz function from an R.Execute wrapper. This is the result of combining the two.
Wrapping all of the functions above and adjusting them to support multiple instances of Power BI while applying some minification techniques we get the final M script.
Some of the use cases that are still left for me to explore with this approach:
- pushing M scripts to GitHub, thus replicating the query catalog functionality
- exporting the Power Queries to *.odc files and consequently importing M scripts from Power BI desktop into Excel
For the curious minds, here is the PowerShell code I used in Power Query reconnaissance.
Please note that this approach can be used for extracting Power Queries for already loaded data and only in Power BI desktop.