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

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

Reply
Saadii_360o
Helper I
Helper I

Rest API Pagination Query (List. Generate)

HI . sorrry for posting this second or third time but could'nt find any solutions from the community. let me know if some who can help me out. Any power query expert here with Rest API's who can help me with List. Generate function query for pagination?
Issue: Pagination of API URL that allows 50 rows of data per Agent.
URL: https://vcc-na4.8x8.com/api/stats/agents/{Agent-ID}/Activities?n=1
Background: Created a function below which allows me to extract all Agent ID's(But only 50 rows per ID).
(id as text, n as number) as table=>

let

Source = Xml.Tables(Web.Contents("https://vcc-na8.8x8.com",

[RelativePath="/api/stats/agents/" &(id)&"/activities?n="&number.totext(n) ) ),

in
Source

Than a created a query below to extract all the rows as well watching curbal's video,
Video Link: https://www.youtube.com/watch?v=05yhwnuCjRw
Query:
let
Source = List.Generate(()=>
[result = try all_rows (1) otherwise try null, n=1],
each [result]<> null,
each [result = try all_rows ([n]+50) otherwise null, n=[n]+50],
each [result]

)
in
Source
but it shows this error,

Saadii_360o_0-1646507639671.png
Let me know if someone can solve my issue.
I am available for skype or zoom call anytime.if someone wants to understand the issue or resolve m,y issue that would be a huge help.

7 REPLIES 7
mahoneypat
Microsoft Employee
Microsoft Employee

Here is an example function with the syntax I believe you need. Since I can't access your API, I used the Star Wars API that supports paginated responses. To simulate your scenario with searching for different AgentIDs, I just changed the topic I was querying on (people, planets, vehicles, films). A different number of pages exist for each of those. You could update the function with your url and use AgentID in place of search.

 

mahoneypat_0-1646527459904.png

 

mahoneypat_1-1646527467844.png

 

(search as text)=>
let
    Source = List.Generate(
        ()=> [Result = try Json.Document(Web.Contents("https://swapi.py4e.com/api/" & search & "?page=1")) otherwise null, Page = 1],
        each [Result] <> null,
        each [Result =try Json.Document(Web.Contents("https://swapi.py4e.com/api/" & search & "?page="& Number.ToText([Page]+1))) otherwise null, Page = [Page] + 1],
        each [Result]
    )
in
    Source

 

Pat

 

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


If you are available on skype or zoom for 10 just minutes that would be great.

Hi, Thanks for the reply.
I have tried your way but it shows the error. KIndly let me know how to tackle it.

(id as text)=>
let
Source = List.Generate (
()=> [result = try Xml.Tables(Web.Contents("https://vcc-na8.8x8.com",[RelativePath="/api/stats/agents/" &(id)& "/activities?n=1"])) otherwise null,n=1],
each [result]<> null,
each [result = try Xml.Tables(Web.Contents("https://vcc-na8.8x8.com",[RelativePath="/api/stats/agents/" &(id)& "/activities?n="& number.totext([n]+50)])) otherwise null, n=[n]+50],
each [result]
)

in
Source


Error

Saadii_360o_0-1646610337615.png

Thanks in advance.

 

Couple things.

1. M is case sensitive, so you need to use Number.ToText

2. I am surprised that the id is wrapped in ( ). Is that needed? If so, I would think it would need to be inside the " ". Either remove the ( ), or update the code like this

 

[RelativePath="/api/stats/agents/(" & id & ")/activities?n=1"]

 

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube



Hi I have followed the exact steps you mentioned earlier. the function works fine as i want it to be but i am unable to convert it into table.
PFB the Query,

(id as text)=>
let
Source = List.Generate(
()=>[result = try Xml.Tables(Web.Contents("https://vcc-na8.8x8.com",[RelativePath ="/api/stats/agents/"&id&"/activities?n=1"])) otherwise null, n=1],
each [result]<> null,
each [result = try Xml.Tables(Web.Contents("https://vcc-na8.8x8.com",[RelativePath ="/api/stats/agents/"&id&"/activities?n="&Number.ToText([n]+50)])) otherwise null, n=[n]+50],
each [result]
)
in
Source

PFB the video that I have attached for you to see. you will be easily able to understand the situation.

https://drive.google.com/file/d/1-VhNR8C0CWIz5eKvXtg07MecwFmtdGJm/view?usp=sharing
Really looking forward for your reply.Thanks

The issue is that it is using the first table to decide which columns to expand. One way to fix it would be to update the code in the formula bar with the list of field names (from the example you shows that had data).

mahoneypat_0-1646693426336.png

 

Alternatively, you could use the approach shown in this video to keep all the fields from all the tables to be combined.

(2) The BEST way to combine files in Power BI so you don’t lose columns - YouTube

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Hi, I think the query is giving me infinite tables in repsonse and i am unable to expand the tables in proper way.
PFB the way for you to undertsnd where I am stuck. it will save your time as well.

URL: https://drive.google.com/file/d/1xbASl1EaziV3qp1ptZlzCtA-fbXnbJ3R/view?usp=sharing

Really Looking forward for your help
best,
Saad.

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.

Top Kudoed Authors