Ravindra BagaleCourses & study guides

5. Web Data, APIs and Google Sheets

5.3 Many Pages: Pagination with a Parameter and a Custom Function

Some sites or APIs show data page by page: …/stores?page=1, …/stores?page=2 … Build one query for a single page, turn it into a function, and call it for a list of page numbers.

Steps in Power BI

  1. Build a query for page 1 and clean it fully (for example a fictional partner site https://example.com/darkstores?page=1).
  2. Home › Manage Parameters › New Parameter › Name PageNo › Type Decimal Number › Current Value 1.
  3. Open Home › Advanced Editor and replace the hard-coded 1 in the URL with the parameter (see the code below).
  4. Right-click the query in the Queries pane › Create Function… › name it fnGetStoresPage. Power Query links the function to the query, so editing the query updates the function.
  5. Home › New Source › Blank Query and type = {1..5} (a list of pages) › To Table (List Tools › Transform) › rename the column to Page › set type to Whole Number.
  6. Add Column › Invoke Custom Function › Function query fnGetStoresPage › PageNo = column Page › OK.
  7. Click the expand icon on the new column to expand all columns. You now have all pages in one table.
// fnGetStoresPage
(PageNo as number) as table =>
let
    Source = Web.Contents("https://example.com",
                [RelativePath = "darkstores", Query = [page = Text.From(PageNo)]]),
    Page   = Html.Table(Text.FromBinary(Source), {{"Store", ".store-name"}, {"City", ".store-city"}},
                [RowSelector = ".store-card"])
in
    Page

Keep the base URL fixed for Service refresh

Write Web.Contents("https://example.com", [RelativePath = …, Query = […]]) instead of joining the whole URL as text ("https://example.com/darkstores?page=" & Text.From(PageNo)). The Power BI Service must be able to see the base URL to validate a dynamic data source. Otherwise scheduled refresh can fail with a message that the dataset contains dynamic data sources.

Practice task

Convert a one-page query into a function and load pages 1–3. Then make the last page number a parameter (बदलता येणारे मूल्य, जे अनेक ठिकाणी वापरता येते) MaxPage and use {1..MaxPage}.

Ravindra Bagale's Tip

Mitrano, khup students write a custom function for pagination but invoke it on hard-coded page numbers, so new pages never appear. Generate the page list dynamically (udaharan mhanje {1..PageCount}) and test with two or three pages first before loading everything. Add a small delay or limit the number of requests to respect the website. He exam aani interview doghansathi important aahe.