Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
venusura483
Frequent Visitor

How to retrieve all pages without knowing the number of pages from API as a web data source

Hi Experts !
 
I'm trying to retrive all apges from the web API source but the below power Query is keep loading infinitely with null values.
 
fUsers:
(Page as number)=>
let
    Source = Json.Document(Web.Contents("https://api.apples.com/api/v1/users?page="&Number.ToText(Page), [Headers=[Authorization="Bearer Token"]])),
    #"Converted to Table" = Table.FromRecords({Source}),
    #"Expanded Items" = Table.ExpandListColumn(#"Converted to Table", "Items"),
    #"Expanded Items1" = Table.ExpandRecordColumn(#"Expanded Items", "Items", {"Id", "Username", "FirstName", "Surname"}, {"Items.Id", "Items.Username", "Items.FirstName", "Items.Surname"}),
    #"Expanded Pagination" = Table.ExpandRecordColumn(#"Expanded Items1", "Pagination", {"PageNumber", "PageSizeRequested", "ItemsCount", "HasMore"}, {"Pagination.PageNumber", "Pagination.PageSizeRequested", "Pagination.ItemsCount", "Pagination.HasMore"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Pagination",{{"Items.Id", type text}, {"Items.Username", type text}, {"Items.FirstName", type text}, {"Items.Surname", type text}, {"Pagination.PageNumber", Int64.Type}, {"Pagination.PageSizeRequested", Int64.Type}, {"Pagination.ItemsCount", Int64.Type}, {"Pagination.HasMore", type logical}})
in
    #"Changed Type"
 
Query 1:
let
    Source = List.Generate( ()=>
    [Result = if Table.IsEmpty(fUsers(1)) then null else fUsers(1), n=0],
    each [Result] <> null and not Table.IsEmpty([Result]),
    each [Result = if Table.IsEmpty(fUsers([n]+1)) then null else fUsers([n]+1), n=[n]+1],
    each [Result]
),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandTableColumn(#"Converted to Table", "Column1", {"Items.Id", "Items.Username", "Items.FirstName", "Items.Surname", "Pagination.PageNumber", "Pagination.PageSizeRequested", "Pagination.ItemsCount", "Pagination.HasMore"}, {"Items.Id", "Items.Username", "Items.FirstName", "Items.Surname", "Pagination.PageNumber", "Pagination.PageSizeRequested", "Pagination.ItemsCount", "Pagination.HasMore"})
in
    #"Expanded Column1"
venusura483_0-1690550448099.png

 

Any help would be much appriciated.
thank you
3 REPLIES 3
lbendlin
Super User
Super User

Refactor your code to run repeatedly until Pagination.HasMore is false.

Thanks @lbendlin , Can you please help me to modify below code to run until Pagination.HasMore is false.

Query 1:
let
    Source = List.Generate( ()=>
    [Result = if Table.IsEmpty(fUsers(1)) then null else fUsers(1), n=0],
    each [Result] <> null and not Table.IsEmpty([Result]),
    each [Result = if Table.IsEmpty(fUsers([n]+1)) then null else fUsers([n]+1), n=[n]+1],
    each [Result]
),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandTableColumn(#"Converted to Table", "Column1", {"Items.Id", "Items.Username", "Items.FirstName", "Items.Surname", "Pagination.PageNumber", "Pagination.PageSizeRequested", "Pagination.ItemsCount", "Pagination.HasMore"}, {"Items.Id", "Items.Username", "Items.FirstName", "Items.Surname", "Pagination.PageNumber", "Pagination.PageSizeRequested", "Pagination.ItemsCount", "Pagination.HasMore"})
in
    #"Expanded Column1"

You can use whatever approach you like. As you can imagine it is close to impossible to advise without having access to the API.

 

Handling Paginated REST API Results with Power Query | John Dalesandro

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 2026 Power BI update to learn about new features.