Binary.FromText

Updated on

Binary.FromText is a Power Query M function that converts a text value to a binary value using the specified encoding (Base64 or Hex). The function returns a binary representation of the text value.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Binary.FromText(
   text as nullable text,
   optional encoding as nullable number,
) as nullable binary
ArgumentAttributeDescription
textThe text to convert into a binary value.
encodingoptionalSpecifies the BinaryEncoding.Type that determines the encoding type applied to binary data.. By default, if this argument is not specified, the function uses BinaryEncoding.Base64, suitable for base-64 encoding. The alternative option available is BinaryEncoding.Hex, used for hexadecimal encoding.

Description

The Binary.FromText function in Power Query converts a text value into a binary format (a list of numbers). An optional encoding parameter can be specified to indicate the encoding method used for the text value. The function supports the following encoding types BinaryEncoding.Base64 and BinaryEncoding.Hex.

Examples

A useful scenario for using binary values is for encoding. For instance, when creating a table with the column ‘Column1’ and the values “a”, “b” and “c”, Power Query generates the following code:

Table.FromRows (
  Json.Document (
    Binary.Decompress (
      Binary.FromText ( "i45WSlSK1YlWSgKTyUqxsQA=", BinaryEncoding.Base64 ), 
      Compression.Deflate
    )
  ), 
  let
    _t = ( ( type nullable text ) meta [ Serialized.Text = true ] )
  in
    type table [ Column1 = _t ]
)

Notice the text i45WSlSK1YlWSgKTyUqxsQA=? It is the binary representation of the created table. Encoding a value to binary and then compressing it to a text value can improve security, reduce storage space and potentially increase the speed of data retrieval.

Basic Decoding

In its most basic form, you can turn text into binary by using the Binary.FromText function. Let’s say we try that for the text value “2203”. Here’s how you can do it:

// Output: Binary.FromText( "2203", BinaryEncoding.Base64 )
Binary.FromText( "2203" )

Decoding this value by default happens using BinaryEncoding.Base64.

In case you want to make use of hex encoding, you can make use of the enumerations BinaryEncoding.Hex.

// Output: Binary.FromText( "EBE=", BinaryEncoding.Base64 )
Binary.FromText( "1011", BinaryEncoding.Hex )

Other functions related to Binary.FromText are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/binary-fromtext

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy