New: free Power BI model health check. Find what's making your numbers wrong and your model slow. Check your model →
← Back to Blog

Power BI Web.Contents With a Dynamic URL Won't Refresh: Use RelativePath and Query

An API query built with Web.Contents refreshes in Desktop, but the Service says the dataset includes a dynamic data source and turns off scheduled refresh. The URL is being built at runtime. Keep the base URL fixed and move the changing parts into RelativePath and Query.

Power QueryTroubleshooting

You build a query against an API with Web.Contents, and it refreshes fine in Power BI Desktop. After publishing, the semantic model's settings in the Service show:

This dataset includes a dynamic data source. Since dynamic data sources aren't
refreshed in the Power BI service, this dataset won't be refreshed.

The cause is almost always the same: the URL passed to Web.Contents is built while the query runs.

Source = Json.Document(Web.Contents("https://api.example.com/products/" & ProductId))

Why the Service Cares

Before it schedules a refresh, the Service works out which data sources each query uses without running the query, so it can match each source to the credentials you've stored. A literal URL, or a Power Query parameter, can be read that way. A URL assembled from pieces at runtime can't, so the Service gives up on the whole dataset.

Web.Contents has a way around this. Its options record lets you keep the first argument fixed and put the changing parts in separate fields, which the Service reads as details of each call rather than as a different data source:

Web.Contents("https://api.example.com",          <- the data source: fixed
    [RelativePath = "products/" & ProductId,      <- varies per call: fine
     Query = [page = "2", limit = "50"]])          <- varies per call: fine

The other source functions have no equivalent, which is why a server or file path built from data stays non-refreshable. See the dynamic data source error for those.


Fix 1: Move the Path Into RelativePath

Source = Json.Document(
    Web.Contents(
        "https://api.example.com",
        [RelativePath = "products/" & ProductId]
    )
)

The first argument is now the same on every call. RelativePath is appended to it.

Fix 2: Move the Query String Into Query

Building ?date=...&limit=... by hand has the same problem, and it gets URL encoding wrong for values with spaces or &:

// Before: the whole URL is built at runtime
Source = Json.Document(Web.Contents(BaseUrl & "?date=" & DateText & "&limit=50"))
// After: fixed base, query string as a record
Source = Json.Document(
    Web.Contents(
        BaseUrl,
        [Query = [date = DateText, limit = "50"]]
    )
)

Query values must be text, so convert numbers with Text.From. Here BaseUrl is a Power Query parameter, which is fine: the Service reads its current value before refresh. It would not be fine if BaseUrl came from a table or another query.

Fix 3: The Same Inside a Custom Column

Calling an API once per row is common: details for each item ID, a page per date. The same rule applies inside each:

#"Added Details" = Table.AddColumn(
    Items,
    "Details",
    each Json.Document(
        Web.Contents("https://api.example.com", [RelativePath = "items/" & Text.From([ItemID])])
    )
)

If that column also mixes the API with another source, such as item IDs read from a database in the same query, you may hit the Formula.Firewall error next. That's a separate rule, about which data can flow between sources.


After Publishing: Credentials

With a fixed base URL, the Service lists https://api.example.com as a data source, and you set its credentials in the semantic model's settings: Anonymous with a key in the headers or query string, or the authentication the API uses.

Many APIs return an error for the bare base URL, which makes the Service's connection test fail even though the credentials are right. In that case tick Skip test connection in the credentials dialog. Then run Refresh now to confirm it really works; see the credentials error explained for what can go wrong there.

Common Mistakes

Moving the URL into a parameter and stopping there. A parameter is a fine base URL, but a path or query string glued onto it with & makes it dynamic again. The changing parts still belong in RelativePath and Query.

Putting the whole URL in RelativePath. The first argument has to be a real base URL: the scheme and host at least.

Different hosts. RelativePath only varies the path. If the host itself changes (api-eu vs api-us), you need one query per host, each with its own fixed base URL.

Fixing one call and missing another. Check every Web.Contents in the model, including those inside custom functions.

Next Steps

FAQ

+Why does Web.Contents refresh in Desktop but not in the Power BI Service?

Before a scheduled refresh, the Service works out which data sources a query uses without running it, so it can match each one to stored credentials. A URL built by joining text together at runtime can't be worked out that way, so the Service marks the dataset as having a dynamic data source and won't schedule refresh.

+How do I make a dynamic Web.Contents URL refreshable?

Keep the first argument of Web.Contents a fixed base URL, and put everything that changes into its options: RelativePath for the path, Query for the query string. The Service treats the base URL as the data source and the options as details of each call.

+Can the base URL come from a Power Query parameter?

Yes. A parameter has a fixed current value that the Service can read before refresh, and you can change it in the semantic model's settings. What doesn't work is a base URL computed from data, such as a value read from a table or another query.

+Why does the credentials dialog fail for my API after switching to RelativePath?

The Service tests the connection against the base URL on its own, which often returns an error for an API that expects a path. Tick Skip test connection in the credentials dialog, as long as the credentials themselves are right.