Friday, February 7, 2025

How to use Encode and Decode methods for binary-to-text conversions in Power Query

How to Encode and Decode the binary-to-text conversions using M-Query in Power BI
In Power Query, Base64 Encoding is a standard method for encoding binary data into Base64 encoded text format using ASCII characters.

1) Encoding Text:
Base64 Encoding is a way to represent binary data (like images or files) in a text format using only ASCII characters. It's called Base64 because it uses 64 different characters to represent the data.

Please Note: 
This method should not be used as a secure method for protecting sensitive information. It is simply a way to represent binary data in an ASCII string format. Anyone who can decode Base64 can easily get the original data.

How Base64 Encoding Works:
a) Convert to Binary: First, the text is converted to binary data.
b) Split into Groups: The binary data is split into groups of 6 bits.
c) Map to Characters: Each group is mapped to one of the 64 characters in the Base64 alphabet (A-Z, a-z, 0-9, +, and /).

Example:
let
    text2Encode = "Sample_1#X2$y#3&Z_9@a8#B&7C_Key",
    binaryData = Text.ToBinary(text2Encode),
    base64Encoded = Binary.ToText(binaryData, BinaryEncoding.Base64)
in
    base64Encoded

Result:
U2FtcGxlXzEjWDIkeSMzJlpfOUBhOCNCJjdDX0tleQ==

2) Decoding Text:
Base64 Decoding is the reverse process of Base64 encoding. It converts the Base64 encoded text back into its original binary form(like an image, file, or text).

How Base64 Decoding Works:
a) Map to Binary: Each character in the Base64 encoded text is mapped back to its 6-bit binary representation.
b) Combine Groups: The binary groups are combined into the original binary data.
c) Convert to Text: Finally, the binary data is converted back to the original text or file.

Example:
let
    base64Text = "U2FtcGxlXzEjWDIkeSMzJlpfOUBhOCNCJjdDX0tleQ==",
    binaryData = Binary.FromText(base64Text, BinaryEncoding.Base64),
    decodedText = Text.FromBinary(binaryData)
in
    decodedText

Result:
Sample_1#X2$y#3&Z_9@a8#B&7C_Key

#--------------------------------------------------------------Thanks--------------------------------------------------------------#

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog