The April 2018 release of Power BI Desktop included a new M function: Binary.InferContentType. There’s no online documentation for it yet but the built-in documentation is quite helpful:
I tested it out by pointing it at the following simple CSV file:
…and with the following M code:
[sourcecode language=”text” padlinenumbers=”true”]
let
Source = File.Contents("C:\01 JanuarySales.csv"),
Test = Binary.InferContentType(Source)
in
Test
[/sourcecode]
Got the following output:
It has successfully detected that it’s looking at a CSV file; the table in the lower half of the screenshot above is the table returned by the Csv.PotentialDelimiters field, and that shows that with a comma as a delimiter three columns can be found (my recent blog post on Csv.Document might also provide some useful context here).
I also pointed it at a few other file types such as JSON and XML and it successfully returned the correct MIME type, but interestingly when I changed the file extension of my JSON file to .txt it thought the file was a text/CSV file, so I guess it’s not that smart yet. I also could not get it to return the Csv.PotentialPositions field mentioned in the documentation for fixed width files so it may still be a work in progress…?
One thought on “The Binary.InferContentType M Function”