Lesson Learnt: Avoid Web.BrowserContents()

πŸ“Œ Key Takeaway:

Always use Web.Contents() when connecting to authenticated web resources or APIs, especially if publishing to the Power BI Service.
Reserve Web.BrowserContents() for desktop-only scenarios like scraping HTML pages with dynamic content β€” and expect no scheduled refresh capability.

⁉️ Scenario

We encountered a frustrating authentication issue when trying to refresh a published Power BI report in the Service. Despite working fine in Desktop, the dataset failed to refresh online, with no option to set credentials.

πŸ” Root cause:

The query used Web.BrowserContents() β€” either directly or implicitly chosen by Power BI based on the URL entered when using Get Data β†’ Web.

🚨 Gotcha:

Power BI automatically chooses between Web.Contents() and Web.BrowserContents() based on the URL style:

  • If the URL looks like a file (e.g., ends in .json, .csv, etc.), it typically uses Web.Contents().
  • If the URL looks like a web page (e.g., no file extension, or points to a homepage), it may use Web.BrowserContents().

Unlike Web.Contents(), the Web.BrowserContents() function:

  • Does not support credential management in the Power BI Service
  • Cannot be refreshed on a schedule, even with anonymous access
  • Does not handle privacy levels or authentication headers

βœ… Resolution:

We replaced Web.BrowserContents() with Web.Contents() and structured the call to include the necessary headers (e.g., Bearer token), enabling proper credential handling and refresh support.


πŸ”Ή Web.Contents(url) – Preferred in Power BI Service

This is the standard method to fetch data from the web (or APIs) in Power Query M. It supports:

  • Credential storage: Power BI Desktop and Power BI Service both allow you to set credentials for the URL or root domain (Basic, OAuth, API key, etc.).
  • Scheduled refresh: Supported and reliable in the Power BI Service.
  • Privacy Levels: Respects privacy level settings (Public, Organisational, Private).

βœ… Recommended for API calls and web data sources where credentials or headers are required.

Example:

    Source = Json.Document(Web.Contents("https://api.example.com/data", [
Headers = [Authorization="Bearer abc123"]
]))
in
Source

πŸ”Έ Web.BrowserContents(url) – Interactive Browsing Only

This simulates a browser session and is typically used for scraping data from web pages rendered in HTML+JavaScript.

  • No credential storage: Power BI cannot manage credentials for Web.BrowserContents in the Service β€” it’s designed for interactive use in Desktop only.
  • Not supported for refresh: Power BI Service does not support scheduled refresh using this function.
  • Authentication: You cannot pass headers or tokens directly.
  • Privacy: Does not handle privacy levels or security isolation in the same way.

❌ Not recommended for APIs or authenticated services if you intend to publish and refresh in the Power BI Service.

Example:

    Source = Web.BrowserContents("https://example.com/dashboard")
in
Source

πŸ” Summary: Credential Management & Publishing

FeatureWeb.ContentsWeb.BrowserContents
Scheduled refresh (Service)βœ… Supported❌ Not supported
Credentials in Serviceβœ… Stored & managed securely❌ Not available
API access supportβœ… Yes❌ No (HTML scraping only)
Privacy level handlingβœ… Yes❌ No
Intended useAPIs, REST calls, static filesInteractive pages, JavaScript sites

βœ… Recommendation

Always use Web.Contents() when building reports that will be published to the Power BI Service, especially if:

  • You’re calling APIs
  • You need to authenticate
  • You want scheduled refreshes to work

Use Web.BrowserContents() only for web scraping in Desktop where the page content must be rendered before accessing data (and manual refresh is acceptable).

Leave a Comment