Uri.Combine

Updated on

Uri.Combine is a Power Query M function that merges an input baseUri and relativeUri to create an absolute URI. The function returns the combined absolute URI.

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

Syntax

Uri.Combine(
   baseUri as text,
   relativeUri as text,
) as text

Description

The Uri.Combine function constructs a new URL by combining a base URL with an additional path, either relative or absolute.

The behavior of Uri.Combine depends on two main factors:

  1. Whether the path being combined is relative or absolute:
    • Relative Path: A path that does not start with a / is appended to the base URL, following folder-like or file-like rules.
    • Absolute Path: A path that starts with a / replaces everything after the domain in the base URL.
  2. Whether the base URL ends with a forward slash (/):
    • With a trailing slash: Indicates that the base URL represents a “folder.” Relative paths are appended directly after the base URL.
    • Without a trailing slash: The last segment of the base URL is treated as a “file.” A relative path replaces this segment.

Examples

Combining a Relative Path

When combining a base URL and a relative path, the behavior changes depending on whether the base URL ends with a /:

// Base URL ends with a slash (folder behavior)
// Returns: https://powerquery.how/articles/uri-parts
Uri.Combine("https://powerquery.how/articles/", "uri-parts")

Alternatively we can see a URL that does not end with a slash:

// Base URL does not end with a slash (file behavior)
// Returns: https://powerquery.how/uri-parts
Uri.Combine("https://powerquery.how/articles", "uri-parts")

Combining an Absolute Path

An absolute path (starting with /) overrides everything after the domain in the base URL:

// Returns: https://powerquery.how/absolute/path
Uri.Combine("https://powerquery.how/articles/", "/absolute/path")

Putting it all together

Suppose you have a base URL and want to combine different paths. Here’s a table that shows what this would look like for different URLs:

Uri.Combine function combines base and relative Uri in Power Query M

Here are some observations from above table:

  1. Base URL without a trailing slash:
    • When the base URL does not end with a trailing slash, the last segment (after the final forward slash) of the base URL is treated as a “file” or final component.
    • If a relative path is combined, the relative path replaces the last segment of the base URL.
    • If an absolute path (starting with /) is combined, it replaces everything after the domain, disregarding the last segment of the base URL.
  2. Base URL with a trailing slash:
    • When the base URL ends with a trailing slash, the trailing slash indicates that the URL represents a “folder.”
    • A relative path is appended directly to the base URL, following the folder-like behavior.
    • An absolute path (starting with /) again replaces everything after the domain.

To try this yourself you can use the code:

let
    paths = { "relative/path", "/absolute/path" },
    SourceTable = 
        #table(
            type table [Base = Text.Type, Path = {text} ],
            {
                {"https://powerquery.how",                  paths },
                {"https://powerquery.how/",                 paths },
                {"https://powerquery.how/articles",         paths },
                {"https://powerquery.how/articles/",        paths },
                {"https://powerquery.how/articles/tips",    paths },
                {"https://powerquery.how/articles/tips/",   paths }
            }
        ),
    expandPaths = Table.ExpandListColumn( SourceTable, "Path"),
    uriCombine = Table.AddColumn(expandPaths, "Uri.Combine", each Uri.Combine([Base], [Path]), type text)
in
    uriCombine

Learn more about Uri.Combine in the following articles:

Other functions related to Uri.Combine are:

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

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