Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
wskallmeyer
Advocate II
Advocate II

How to load m file from a text file

I am currently using Power BI Desktop without the publishing service.  Basically just sharing pbix files.  I would like to pull the M code out my pbix file so it is one less thing the end user can change.

 

I basically cut and paste the current file's M code and put in a txt file on my desktop alongside (eventually will be in a protected file share) with pbix file.  

 

I then put that text file on my c:\.

 

I get an error when attempting to apply & close the query editor with the pbix file.  The error as best I can tell is generic and not defined anywhere.

 

What am I missing?

 

Thanks in advance.

 

Cheers, Scott

1 ACCEPTED SOLUTION

Use the following code:

 

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

View solution in original post

3 REPLIES 3
dkay84_PowerBI
Microsoft Employee
Microsoft Employee

What is the error message?

 

You should be able to copy the M code (right click your highlighted queries in the query editor and select Copy) and paste to a text file.  Not sure why that would be causing an error when you try to close the query editor.  

Sorry should have included this part as well.  Is this correct?

 

let
Source = Text.FromBinary(File.Contents("C:\Mcode.txt"))
in

 

 

 

 

Use the following code:

 

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

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.