Uri.Parts

D

T

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

Understanding Uniform Resource Identifiers (URIs)

Before we delve into the M code behind Uri.Parts, let’s take a moment to understand what a uniform resource identifier (URI) is. Simply put, a URI is a string of characters that identifies a resource on the internet. This resource can be a web page, an image, a video, or any other type of data that can be accessed through a web browser.

A URI consists of several components, including the scheme, authority, path, query, and fragment. The scheme is the protocol used to access the resource (e.g., http, https, ftp). The authority is the domain name or IP address of the server hosting the resource. The path is the location of the resource on the server. The query is any additional parameters passed to the server. The fragment is an optional component that specifies a specific location within the resource.

The Power Query M function Uri.Parts

The Power Query M function Uri.Parts allows users to parse a URI into its various components. The function takes a single parameter, which is the URI to be parsed. The function then returns a record that contains the different components of the URI as separate fields.

The record returned by Uri.Parts contains the following fields:

– Scheme: The protocol used to access the resource (e.g., http, https, ftp)

– Host: The domain name or IP address of the server hosting the resource

– Path: The location of the resource on the server

– Query: Any additional parameters passed to the server

– Fragment: An optional component that specifies a specific location within the resource

The M Code Behind Uri.Parts

The M code behind Uri.Parts is relatively simple. The function is defined as follows:


let

uri = Text.Combine({uri, ""}),

scheme = Text.BeforeDelimiter(uri, "://"),

authority = Text.AfterDelimiter(uri, "://"),

path = Text.AfterDelimiter(authority, "/"),

query = if Text.Contains(path, "?") then Text.AfterDelimiter(path, "?") else "",

fragment = if Text.Contains(query, "#") then Text.AfterDelimiter(query, "#") else "",

host = Text.BeforeDelimiter(Text.Replace(path, fragment, ""), "/")

in

[Scheme = scheme, Host = host, Path = path, Query = query, Fragment = fragment]


Let’s break down this code into its individual components.

The first line of the code concatenates a blank string to the URI. This is done to ensure that the URI ends with a delimiter.


uri = Text.Combine({uri, ""}),


The second line extracts the scheme from the URI using the BeforeDelimiter function.


scheme = Text.BeforeDelimiter(uri, "://"),


The third line extracts the authority (i.e., the domain name or IP address) from the URI using the AfterDelimiter function.


authority = Text.AfterDelimiter(uri, "://"),


The fourth line extracts the path from the authority using the AfterDelimiter function.


path = Text.AfterDelimiter(authority, "/"),


The fifth line extracts the query from the path if it exists. If the path does not contain a query, an empty string is returned.


query = if Text.Contains(path, "?") then Text.AfterDelimiter(path, "?") else "",


The sixth line extracts the fragment from the query if it exists. If the query does not contain a fragment, an empty string is returned.


fragment = if Text.Contains(query, "#") then Text.AfterDelimiter(query, "#") else "",


The seventh line extracts the host (i.e., the domain name or IP address) from the path using the BeforeDelimiter function.


host = Text.BeforeDelimiter(Text.Replace(path, fragment, ""), "/")


The function then returns a record that contains the different components of the URI as separate fields.


[Scheme = scheme, Host = host, Path = path, Query = query, Fragment = fragment]


In conclusion, the Power Query M function Uri.Parts is a powerful tool that allows users to parse a URI into its various components. The M code behind Uri.Parts is relatively simple, but understanding how it works can help users to better manipulate and transform web-based data sources. By utilizing Uri.Parts and other Power Query M functions, users can streamline their data analysis and data transformation tasks and achieve more efficient workflows.

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)