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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
alexmoller
Frequent Visitor

Looping Through API with Next Link Parameter

Hello All,

 

I am trying to loop through a Graph API endpoint, using the next link value present in the response to get the next paginated list of values. 

 

I am using list.generate in order to do this. The only problem is I get an error on the last row in the table. I think this is because on the final api call - there is no next link value present. Is there a way to handle this?

 

I cannot remove the error from the query. Here is the code I am using:

 

let
token_uri = "https://login.microsoftonline.com/" & #"TenantID" & "/oauth2/v2.0/token",
resource="https://graph.microsoft.com",
tokenResponse = Json.Document(Web.Contents(token_uri,
[
Content = Text.ToBinary(Uri.BuildQueryString(
[
client_id = #"ClientID",
scope = "https://graph.microsoft.com/.default",
grant_type = "client_credentials",
client_secret = #"Secret"
]
)),
Headers = [Accept = "application/json"], ManualStatusHandling = {400}
])),
access_token = tokenResponse[access_token],
List = List.Generate( () =>
[URL = "https://graph.microsoft.com/v1.0/communications/callRecords",
Result = Json.Document(Web.Contents("https://graph.microsoft.com/v1.0/communications/callRecords", [ Headers = [ Authorization = "Bearer " & access_token ], ExcludedFromCacheKey = {"Authorization"} ] ))],
each [URL] <> null,
each [URL = [Result][#"@odata.nextLink"],
Result = Json.Document(Web.Contents(URL,[ Headers = [ Authorization = "Bearer " & access_token ], ExcludedFromCacheKey = {"Authorization"} ]))]
),
#"Converted to Table" = Table.FromList(List, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"URL", "Result"}, {"URL", "Result"}),
#"Expanded Result" = Table.ExpandRecordColumn(#"Expanded Column1", "Result", {"@odata.context", "@odata.nextLink", "value"}, {"@odata.context", "@odata.nextLink", "value"})
in
#"Expanded Result"

 

Here is the result:

Screenshot 2025-04-01 143446.png

 

Many Thanks in advance!

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

use try ... otherwise ... and return an empty list for the latter.

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

use try ... otherwise ... and return an empty list for the latter.

Thanks for your reply, I'm not sure I follow what you mean, could you explain in more detail?

Ah my apologies, I have worked it out - I just needed to add this code: 

= List.Generate( () =>
[URL = "https://graph.microsoft.com/v1.0/communications/callRecords",
Result = Json.Document(Web.Contents("https://graph.microsoft.com/v1.0/communications/callRecords", [ Headers = [ Authorization = "Bearer " & access_token ], ExcludedFromCacheKey = {"Authorization"} ] ))],
each [URL] <> null,
each [URL = try [Result][#"@odata.nextLink"] otherwise null,
Result = Json.Document(Web.Contents(URL,[ Headers = [ Authorization = "Bearer " & access_token ], ExcludedFromCacheKey = {"Authorization"} ]))]
)

 

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