Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Uri.Parts Function: Guide and Examples

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Uri.Parts function in Power Query is used to decompose a Uniform Resource Identifier (URI), such as a website URL, into its individual structural components. Instead of using complex text-splitting formulas or manual delimiters to find a domain or a query string, this function returns a record containing the scheme, host, port, path, query, and fragment of the URI. This allows you to isolate specific parts of a web address—such as the domain name or a specific tracking parameter—without risking errors caused by varying URL lengths or formats.

What You'll Need

  • Microsoft Excel (with Power Query installed) or Power BI Desktop.
  • A dataset containing a column of URIs or URLs (e.g., a list of website links from a CSV, web scrape, or database).
  • Basic familiarity with the Power Query Editor interface.

Step-by-Step

  1. Load Your Data: Open Power BI Desktop or Excel. Go to the Data tab (Excel) or Home tab (Power BI) and select Get Data to import your source. Once the data is loaded, click Transform Data to open the Power Query Editor window.
  2. Create a Custom Column: Navigate to the Add Column tab on the top ribbon. Select Custom Column. This opens a dialog box where you can enter your M code formula.
  3. Enter the Uri.Parts Formula: In the Custom column formula box, type the following: Uri.Parts([YourColumnName]). Replace [YourColumnName] with the actual name of the column that contains your URLs. Ensure the column name is enclosed in square brackets.
  4. Verify the Record Result: Click OK. You will notice a new column added to your table. Every cell in this column will now contain the word Record. This is because Uri.Parts does not return a single piece of text, but a structured record containing multiple fields.
  5. Expand the Record: To access the actual data, click the Expand icon (two diverging arrows) located at the top right of the new column header. A list of available fields will appear: Scheme, Host, Port, Path, Query, and Fragment.
  6. Select Desired Components: Uncheck Use original column name as prefix to keep your headers clean. Select only the parts you need. For example, if you only want to know the website domain, select Host. If you need to analyze URL parameters, select Query.
  7. Finalize and Load: Once the columns are expanded, you can rename them for clarity (e.g., renaming Host to Website Domain). Go to the Home tab and click Close & Apply to bring the parsed data into your report or spreadsheet.

Common Mistakes

One of the most frequent mistakes is attempting to use Uri.Parts on a column that contains null values or non-URL text. If a cell is empty or contains a random string that does not follow URI formatting, the function may return an error for that specific row. To avoid this, it is best to filter out nulls or use a conditional column to ensure only valid-looking strings are passed to the function.

Another common error is forgetting to expand the record. New users often see the word Record in their cells and assume the function has failed or that they need a different tool. It is important to remember that Uri.Parts is a record-generating function; the data is there, it is simply nested within a record object that must be expanded via the UI or through the Record.Field function in M code.

Users also occasionally confuse Uri.Parts with Uri.Base. While Uri.Base returns the root of the URI, Uri.Parts provides a comprehensive breakdown. Using the wrong function can lead to missing critical data, such as query strings or fragments, which are essential for marketing attribution analysis.

Tips

To handle errors gracefully, wrap your function in a try...otherwise block. For example, try Uri.Parts([URL]) otherwise null. This ensures that if a row contains a malformed URL that would normally crash the refresh process, Power Query will simply leave that cell blank and continue processing the rest of the dataset.

If you are specifically looking for query parameters (the part after the ? in a URL), remember that Uri.Parts returns the query as a single string. To turn that string into a table of keys and values, you can further process the Query result using Uri.Query. This allows you to pivot the parameters into their own columns, making it much easier to analyze UTM codes or search terms.

For those working with very large datasets, expanding the record immediately after creation is more efficient than creating multiple custom columns for each part of the URI. Expanding a single record column is a vectorized operation in Power Query, which generally performs better than calling the Uri.Parts function six separate times for the same row to get the host, path, and scheme individually.

Troubleshooting

If the function returns an Expression.Error stating that the value cannot be converted to a Text type, check your source column. Uri.Parts requires a text input. If your column is typed as Any or Number, right-click the column header, select Change Type, and choose Text before applying the custom column formula.

If you find that the Host column is returning unexpected results, such as including the port number (e.g., localhost:8080), verify if the URI is a standard web URL. The Uri.Parts function follows RFC standards; if the port is explicitly defined in the URI string, it will be separated into the Port field. If the Port field is null, it means the URI is using the default port for its scheme (80 for HTTP, 443 for HTTPS).

If the Expand button is missing or greyed out, ensure that the custom column was created correctly. If you accidentally wrote the formula as a text string (by putting quotes around the function name), Power Query treats the result as text rather than a record. Double-check the formula bar to ensure it reads Uri.Parts([Column]) and not "Uri.Parts([Column])".

Frequently Asked Questions

Can Uri.Parts handle relative URLs?

No, Uri.Parts requires an absolute URI (including the scheme like http:// or https://). If you provide a relative path like /products/item1, the function will likely return an error because it cannot identify the scheme or host.

What is the difference between Path and Query?

The Path is the specific location of the resource on the server (e.g., /blog/article-1), while the Query contains the parameters passed to the page, usually starting after a question mark (e.g., ?id=123&source=email).

Does this function work with FTP links?

Yes, Uri.Parts works with any valid URI scheme, including ftp://, mailto:, or file://, as it parses the string based on general URI standards rather than just web protocols.

How do I remove the 'www.' from the Host result?

The Uri.Parts function returns the host exactly as it appears in the URI. To remove www., you can right-click the expanded Host column and use Replace Values to replace www. with an empty string.

Can I use this to extract the protocol?

Yes, the Scheme field in the resulting record contains the protocol used, such as http, https, or ftp.