Uri.Parts is a Power Query M function that extracts the parts of an input absoluteUri as a record, containing values such as Scheme, Host, Port, Path, Query, Fragment, UserName, and Password. The function returns a record with the various parts of the input absoluteUri.
Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365
Syntax
Uri.Parts( absoluteUri as text ) as record
Description
The Uri.Parts function decomposes a URL into its individual components, such as the scheme, host, port, path, query parameters, fragment, username, and password. Its purpose is to simplify the process of extracting specific parts of a URL, making it particularly useful for working with web data, APIs, or decoding percent-encoded strings.
Examples
Extracting URL Components
Suppose you want to analyze the structure of the URL: https://powerquery.how/uri-parts
. You can extract all the components of this URL using the following code:
Uri.Parts( "https://powerquery.how/uri-parts/#examples" )
The output of this code is:
[
Scheme = "https",
Host = "powerquery.how",
Port = 443,
Path = "/uri-parts",
Query = [],
Fragment = "examples",
UserName = "",
Password = ""
]
This result breaks the URL into its distinct parts:
- Scheme: Protocol used (e.g.,
http
,https
). - Host: Domain name or IP address (e.g.,
powerquery.how
). - Port: Network port, defaulted to 443 for HTTPS.
- Path: The specific resource being accessed (e.g.,
/uri-parts
). - Query: Contains any parameters passed in the URL.
- Fragment: The section after
#
in a URL (not present here). - UserName/Password: Credentials for accessing the resource, if included.
Extracting API Response Parameters
When working with APIs, you might receive a redirect URL containing query parameters. For example, the Discord API might redirect you with a URL like this:
"http://localhost/discord/redirect?code=GvcZLibSs0vhn0CxDbY0yqxfPgpJcX"
You can extract the query parameter (code
) using Uri.Parts
:
let
myRequest = "http://localhost/discord/redirect?code=GvcZLibSs0vhn0CxDbY0yqxfPgpJcX",
Result = Uri.Parts( myRequest )
in
Result
The output of this code is:
[
Scheme = "http",
Host = "localhost",
Port = 80,
Path = "/discord/redirect",
Query = [ code = "GvcZLibSs0vhn0CxDbY0yqxfPgpJcX" ],
Fragment = "",
UserName = "",
Password = ""
]
By breaking down the URL using the Uri.Parts function, you can easily access the code
parameter in the Query
section.
Decoding Percent-Encoding in URLs
The Uri.Parts
function can also help decode percent-encoded strings in query parameters. For example, you receive a string like %2Bmoney%24
, which represents +money$
. To decode it:
// Output: "+money$"
let
UriUnescapeDataString = ( data as text ) as text => Uri.Parts( "http://contoso?a=" & data )[Query][a]
in
UriUnescapeDataString( "%2Bmoney%24" )
Here, %2B
is decoded as +
, and %24
is decoded as $
.
Related functions
Other functions related to Uri.Parts are:
2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy