Power Query Uri.EscapeDataString: How to Use It
In this article
Use Uri.EscapeDataString when raw text must be placed safely inside a URI component in Power Query: it percent-encodes characters that should not appear literally in that part of the URL. It does not validate a whole URL, authenticate you, or make a secret safe to share. You need a query with the raw text, a clear idea of the target URI component, and a test value that is not sensitive.
What You'll Need
- Excel, Power BI Desktop, or Power Query in a supported Microsoft app, with the Advanced Editor available.
- A query containing the raw text to encode, such as a column, parameter, or literal string.
- Knowledge of where the text will go: path-like data segment versus query parameter value.
- A non-sensitive test value, such as a sample label, not a token, password, or private identifier.
Step-by-Step
- Open the query in the Power Query Editor and identify the raw value you want to encode. If it is already in a column, note the column name. If it is a literal, keep it short and safe.
- Decide which URI part will receive the value. Use Uri.EscapeDataString when the text is meant as data for a URI component, such as a path-like segment. Use a query-specific escape, such as Uri.EscapeQuery, when the text is a query parameter value.
- In the formula bar or Advanced Editor, enter a simple expression such as = Uri.EscapeDataString("sample value") or = Uri.EscapeDataString([ColumnName]). Labels and exact menu names can differ slightly by app, version, device, or region.
- Run the step and inspect the result. Safe characters should remain readable while unsafe characters become percent sequences. Confirm the output is not the same as the input when the input contains spaces, ampersands, slashes, or other URI-sensitive characters.
- Add the encoded value to the URL only after choosing the right component. For example, build a path segment from an encoded value and add query parameters separately; do not paste a raw query string into the path.
- Keep the raw and encoded values in separate columns while testing. This makes it easy to see what changed and to undo an incorrect transformation.
- Refresh the query and validate the final URL in a controlled test. If the source rejects the URL, compare the encoded segment with the expected format and check whether the final URL is valid, not merely whether the encoded text looks plausible.
Common Mistakes
- Using Uri.EscapeDataString for a query parameter value when a query-specific escape is more appropriate.
- Encoding an already encoded string, which creates double encoding such as percent signs becoming percent-encoded percent signs.
- Assuming the function validates the whole URL or proves the endpoint is safe.
- Encoding secrets and then placing them into logs, formulas, or shared queries where they can be copied.
- Treating spaces, slashes, or ampersands as interchangeable; they are handled differently depending on the URI component.
Tips
- Test with a tiny sample first, then apply the same logic to the full column.
- Keep the raw value visible for debugging, but remove or protect it before publishing if it contains private data.
- Document whether each encoded value is for a path, query string, fragment, or another component.
- Compare before and after values side by side so reviewers can see exactly what the function changed.
Troubleshooting
If the formula errors, check the spelling of the function, the column name, and the parentheses. If the output looks wrong, confirm the input is not already encoded and confirm the target component is correct. If the web source still rejects the URL, verify the full URL structure, check that parameters are joined with the right separators, and test in a non-production environment. If the result is null or unexpected for blank input, filter or replace blanks with an explicit empty string before encoding. Do not paste production credentials into the query just to test; use a sandbox or a documented test endpoint.
Frequently Asked Questions
Does Uri.EscapeDataString validate the URL?
No. It prepares text for use in a URI component; it does not prove that the final URL is reachable, authorized, or safe to call.
Is it the same as Uri.EscapeQuery?
No. They are designed for different URI roles. Use the one that matches the component you are building.
What happens to spaces?
Spaces are not safe literal characters in many URI contexts, so they are percent-encoded or otherwise represented instead of left as raw spaces.
Should I encode a full URL?
Usually no. Encode individual components, then assemble the URL deliberately. Encoding an entire URL can corrupt separators and make debugging harder.
Can I use it for secrets?
You can encode a value, but encoding is not a security control. Keep secrets out of shared queries, logs, and samples.