Binary.InferContentType

D

T

The M Code Behind the Power Query M function Binary.InferContentType

What is Binary.InferContentType?

Binary.InferContentType is a function in the M language that is used to infer the content type of a binary value. In other words, it is used to determine the file type of a binary value, such as a document, image, or audio file. This function is particularly useful when working with files that do not have a file extension or when the file extension is incorrect or missing.

The syntax of the Binary.InferContentType function is as follows:


Binary.InferContentType(binary as binary) as text


The function takes a binary value as input and returns the content type as text. The content type is returned in the form of a MIME type, which is a standard way of identifying the type of a file.

How does Binary.InferContentType work?

The Binary.InferContentType function works by examining the first few bytes of a binary value to determine its content type. Each file type has a unique signature, which is a specific sequence of bytes at the beginning of the file. By examining these bytes, the function can determine the content type of the file.

For example, a JPEG image file always starts with the bytes “FF D8 FF”. By examining the first few bytes of a binary value, the Binary.InferContentType function can determine whether it is a JPEG image file or not.

How to use Binary.InferContentType in Power Query

To use the Binary.InferContentType function in Power Query, you need to create a custom function that calls the Binary.InferContentType function. The custom function takes a binary value as input and returns the content type as text.

Here is an example of a custom function that uses the Binary.InferContentType function to determine the content type of a binary value:


let

GetContentType = (binaryValue as binary) =>

let

contentType = Binary.InferContentType(binaryValue)

in

contentType

in

GetContentType


To use this custom function, you can create a new column in Power Query and apply the function to the binary column. For example, if you have a column named “File” that contains binary values representing different files, you can create a new column named “ContentType” and apply the custom function to the “File” column:

1. Right-click on the “File” column and select “Add Column” -> “Custom Column”.

2. In the “New Column” dialog box, enter “ContentType” as the column name.

3. In the “Custom Column Formula” dialog box, enter the following formula:


GetContentType([File])


4. Click “OK” to create the new column.

The custom function will then be applied to each row in the “File” column, and the content type of each file will be determined and displayed in the “ContentType” column.

The Binary.InferContentType function is a powerful tool in the Power Query M language that enables users to determine the content type of a binary value. By examining the first few bytes of a binary value, the function can determine the file type of a document, image, or audio file. This function is particularly useful when working with files that do not have a file extension or when the file extension is incorrect or missing. By creating a custom function that calls the Binary.InferContentType function, users can easily determine the content type of binary values in Power Query.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)