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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.