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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

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
➤ About: https://sahirmaharaj.com/about.html
➤ 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
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors
Top Kudoed Authors