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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.