Can a Power Query get the M code of another query?

41 Views Asked by At

Can a Power Query return the M code for another query? I'm not talking about manually selecting Reference or Duplicate or Advanced Editor from the Power Query editor. I want the result of Query2 to have the M code of Query1 (let Source =...) that is refreshable when Query1 changes.

In Excel, I can get the query code from VBA via ActiveWorkbook.Queries(x).Formula but then I have to load that to a table and pull it into a query, and then have to rerun the VBA every time a query is updated.

In Power BI, I can use DMVs to get the query code and even the dependencies.

select * from $SYSTEM.TMSCHEMA_PARTITIONS;
select * from $SYSTEM.TMSCHEMA_EXPRESSIONS;
select * from $SYSTEM.DISCOVER_CALC_DEPENDENCY;

But in Power BI Desktop, to use the DMVs I have to connect to localhost:12345 with a random port number. As far as I know, there's no good easy, repeatable way to use DMVs against the current file opened in Power BI Desktop without manually updating the port # each time.

I can get a list of all the queries in the current file with #shared or better yet #sections, but these commands seem to return the result of the queries, not the query code itself.

Is there any way a query can get the M code of another query?

1

There are 1 best solutions below

3
horseyride On

No, but you could create a shared repository of M code and have every query just do a pull for latest version

let Source = Text.FromBinary(File.Contents("C:\directory\MyMCode.txt")),
Results = Expression.Evaluate(Source, #shared)    
in Results