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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
djs1984
Frequent Visitor

Paginated Data Without Total Pages

Hi,

 

I've been trying to fetch dynamic data from an API with no known TotalPages parameter. There is a num_pages parameter, and a has_more one; if has_more is 1, there are more pages. So, I need to keep fetching pages until has_more = 0.

I've been trying variations of this:

 

let
    apiKey = "{my_key}",
    fnGetPage = (pageNum as number) =>
    let
        Source = Json.Document(Web.Contents("https://}myapi}", [
            Headers = [
                #"ApiKey" = apiKey
            ],
            Query = [
                num_page = pageNum
            ]
        ])),
        Data = Source[data] 
    in
        Data,

    firstPage = Json.Document(Web.Contents("https://{myapi}", [
        Headers = [
            #"ApiKey" = apiKey
        ],
        Query = [
            num_page = 1
        ]
    ])),

    hasMore = firstPage[has_more], 

    pageList = List.Generate(
        () => 1,
        each hasMore,
        each _ + 1,
        each (pageNum as number) => 
            let
                pageData = Json.Document(Web.Contents("https://{myapi}", [
                    Headers = [
                        #"ApiKey" = apiKey
                    ],
                    Query = [
                        num_page = pageNum
                    ]
                ])),
                hasMore = pageData[has_more]
            in
                hasMore
    ),

    pageNumbers = List.Transform(pageList, each { _ }), numbers
    allPages = List.Transform(pageNumbers, each fnGetPage(_)),
    combinedTable = Table.Combine(allPages)
in
    combinedTable

 

but it seems wrong, and it's returning an error about the type being unable to convert number to Text.

I think I'm going about it the wrong way. Is there are obviously easier way of doing this?


2 REPLIES 2
Sahir_Maharaj
Super User
Super User

Hello @djs1984,

 

Can you please try this updated approach:

let
    apiKey = "{my_key}",
    baseUrl = "https://{myapi}",

    fnGetPage = (pageNum as number) =>
    let
        Source = Json.Document(Web.Contents(baseUrl, [
            Headers = [#"ApiKey" = apiKey],
            Query = [num_page = Text.From(pageNum)]
        ])),
        Data = Source[data],
        HasMore = Source[has_more]
    in
        [Data = Data, HasMore = HasMore],

    AllPages = List.Generate(
        () => [Page = 1, HasMore = true, Result = fnGetPage(1)],
        each [HasMore],
        each [Page = [Page] + 1, Result = fnGetPage([Page]), HasMore = [Result][HasMore]],
        each [Result][Data]
    ),

    CombinedTable = Table.Combine(List.Transform(AllPages, Table.FromList(_, Splitter.SplitByNothing(), {"Column1"})))
in
    CombinedTable

Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Hi,

 

Thanks for responding. Unfortunately, I'm getting an error with that:

djs1984_0-1733851496716.png

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors