Uri.EscapeDataString

Updated on

Uri.EscapeDataString is a Power Query M function that encodes special characters in the input data according to RFC 3986 rules. The function returns a properly encoded data string.

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

Syntax

Uri.EscapeDataString( data as text ) as text

Description

The Uri.EscapeDataString function is used to encode a string so it can be safely included in a URL. This function replaces characters that are either not allowed in URLs or have special meanings (such as spaces, &, or ?) with their percent-encoded representations. Percent encoding ensures that the data remains intact and unambiguous when transmitted via a URL.

Examples

Encoding Special Characters

Suppose you need to encode the special characters in the string "+money$". The function converts the special characters (+ and $) into their percent-encoded equivalents:

// Output: "%2Bmoney%24"
Uri.EscapeDataString( "+money$" )

Encoding a Query String

Consider a scenario where you’re building a query string for a search operation that includes spaces and special characters. These characters need to be encoded to avoid conflicts in the URL. Here’s how you can use Uri.EscapeDataString to achieve this:

// Output: "Power%20Query%20M%20language%21%26examples"
let
    searchQuery = "Power Query M language!&examples",
    encodedQuery = Uri.EscapeDataString(searchQuery)
in
    encodedQuery

The encoded output replaces spaces with %20, the exclamation mark ! with %21, and the ampersand & with %26.

Encoding a File Path for a Web Request

In another use case, you may want to encode a file path to make it suitable for a web request. File paths often include spaces and backslashes, which require encoding:

// Output: "C%3A%5C%5CMy%20Files%5C%5CPower%20Query.txt"
let
    filePath = "C:\\My Files\\Power Query.txt",
    encodedFilePath = Uri.EscapeDataString(filePath)
in
    encodedFilePath

Here, backslashes \ are converted to %5C, and spaces are replaced with %20. This makes it so the file path can be safely transmitted as part of a URL.

Other functions related to Uri.EscapeDataString are:

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

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