The Extension.Contents() M Function

Following on from my post last week about M functions that are only available in custom data extensions, here’s a quick explanation of one of those functions: Extension.Contents().

Basically, it allows you to access the contents of any file you include in the .mez file of your custom data connector. Say you have a text file called MyTextFile.txt:


If you create a new Power BI Custom Data Connector project using the SDK, you can add this file to your project in Visual Studio like any other file:


Next, select the file and in the Visual Studio Properties pane set the Build Action property to Compile:


Setting this property means that when your custom data connector is built, this file is included inside it (the .mez file is just a zip file – if you unzip it you’ll now find this file inside).

Next, in the .pq file that contains the M code for your custom data connector, you can access the contents of this file as binary data using Extension.Contents(“MyTextFile.txt”). Here’s an example function for use in a custom data connector that does this:

shared ExtensionContentsDemo.Contents = () =>
        GetFileContents = Extension.Contents("MyTextFile.txt"),
        ConvertToText = Text.FromBinary(GetFileContents)


In the let expression here the GetFileContents step returns the contents of the text file as binary data and the ConvertToText step calls Text.FromBinary() to turn the binary data into a text value.

When this function is, in turn, called it returns the text from the text file. Here’s a screenshot of a query run from Power BI Desktop (after the custom data connector has been compiled and loaded into Power BI) that does this:


6 responses

  1. Pingback: More bookmarks, Azure Analysis Services, and subscriptions with Power BI | Guy in a Cube

  2. Pingback: The Extension.Contents() M Function - SSWUG.ORG

  3. Pingback: Using OpenApi.Document() To Create A Power BI Custom Connector For The Power BI REST API « Chris Webb's BI Blog

  4. Hello, thank you so much for this explenation, in particular I found usefull the part where you specify how to include and compile “external” files in a Visual Studio project.

  5. How can we hide or encrypt client credentials inside .txt file. Once we change .mez extension to .zip it could read all files inside and even the credentials. Can you please suggest ?

Leave a Reply

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

%d bloggers like this: