Search code examples
excelpowerquerym

Is there a way to link a single M Code document to multiple workbooks?


I have several workbooks linked to the same set of CSV files. I use the same Power Query transform steps for each CSV file in each workbook. Instead of copying M code from the advanced editor of one workbook and pasting it into the advanced editor of another, is there somewhere I can store a "master" M code document? I would like to be able to link all workbooks to a single text file containing M code so that when I make changes in this master document the Power Query transformations are made in each linked workbook - similar to style sheets in css.


Solution

  • See https://blog.crossjoin.co.uk/2014/02/04/loading-power-query-m-code-from-text-files/

    let
    //Load M code from text file
    // https://blog.crossjoin.co.uk/2014/02/04/loading-power-query-m-code-from-text-files/
    Source = Text.FromBinary(File.Contents("C:\directory\MyMCode.txt")),
    //Evaluate the code from the file as an M expression
    EvaluatedExpression = Expression.Evaluate(Source, #shared)    
    in
    EvaluatedExpression