Uri.EscapeDataString

D

T

The M Code Behind the Power Query M function Uri.EscapeDataString

What is Uri.EscapeDataString?

Before we dive into the M code behind this function, let’s first understand what Uri.EscapeDataString is. This function is used to escape special characters in a URL before sending it to a web server. It takes a string as input and returns the escaped string as output. For example, if you have a URL that contains spaces, you can use the Uri.EscapeDataString function to replace the spaces with %20 so that the URL can be properly interpreted by the web server.

The M Code Behind Uri.EscapeDataString

Now that we understand what Uri.EscapeDataString is, let’s take a look at the M code behind this function. The M code for Uri.EscapeDataString is relatively simple and consists of just a few lines of code:


(text as text) as text =>

Text.Replace(

Text.Replace(

Text.Replace(

Text.Replace(

Text.Replace(

Text.Replace(

Text.Replace(

Text.Replace(

Text.Replace(

Text.Replace(

text, " ", "%20"),

"!", "%21"),

"'", "%27"),

"(", "%28"),

")", "%29"),

"", "%2A"),

"-", "%2D"),

".", "%2E"),

"_", "%5F"),

"~", "%7E")


In this code, the input text is first replaced with %20 for spaces, %21 for exclamation marks, %27 for apostrophes, %28 for open parentheses, %29 for closed parentheses, %2A for asterisks, %2D for hyphens, %2E for periods, %5F for underscores, and %7E for tildes.

How to Use Uri.EscapeDataString

Now that we know the M code behind Uri.EscapeDataString, let’s take a look at how it can be used in Power Query. To use this function, simply open a new query in Power Query and enter the following M code:


let

textToEscape = "Enter text to escape here",

escapedText = Uri.EscapeDataString(textToEscape)

in

escapedText


In this code, replace “Enter text to escape here” with the text you want to escape. The function will then escape any special characters in the text and return the escaped text.

In conclusion, Uri.EscapeDataString is a powerful function in Power Query that allows users to escape special characters in a URL before sending it to a web server. The M code behind this function is relatively simple and consists of just a few lines of code. By understanding how this function works, you can improve your Power Query skills and create more efficient queries.

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)