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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
tommy_8
Regular Visitor

Paginate API (Facebook Graph API) call

Hi API experts,

 

I need some hints regarding paginating through my API Call.

I'm retrieving data from Facebook Graph API, which gives me the first 25 rows and the "next page" as another column.

 

What I want is to iterate through all next pages, get the data until the next page is null.

 

My Query:

 

let
    Source = Json.Document(Web.Contents("https://graph.facebook.com/v11.0/***/insights?" & "level=ad&" & "fields=campaign_name,ad_name,impressions,inline_link_clicks,clicks&" & "time_increment=1&" & "date_preset=last_3d", [Headers=***]), 65001),
    #"Converted to Table" = Table.FromRecords({Source}),
    #"Expanded paging" = Table.ExpandRecordColumn(#"Converted to Table", "paging", {"cursors", "next"}, {"paging.cursors", "paging.next"}),
    
in
    #"Expanded paging"

 

 

This gives me:

tommy_8_0-1627542683032.png

Is it possible to loop over this and retrieve all "next pages"?

 

Who can help me?

thanks in advance

Tommy

1 ACCEPTED SOLUTION
tommy_8
Regular Visitor

I've not been successful using List.Accumulate().

Instead List.Generate() gives me the list of pages, which can be converted to Table.

 

For any interested user:

let
    API_Bearer = "Bearer XXX",
    Source = List.Generate( ()=> 
        [
            // API Config and paramters
            facebook_account = "act_XXX",
            Facebook_API_URL = "https://graph.facebook.com/v11.0/" & facebook_account & "/insights?",
            level = "level=ad&",
            fields = "fields=campaign_name,ad_name,impressions,inline_link_clicks,clicks&",
            time_increment = "time_increment=1&",
            date_preset = "date_preset=last_30d&",
            
            URL = Facebook_API_URL & level & fields & time_increment & date_preset,
            Result = Json.Document(Web.Contents(URL, [Headers=[Authorization=API_Bearer]]), 65001)
        ],
        each [URL]<>null,
        each [
            URL = try [Result][paging][next] otherwise null,
            Result = Json.Document(Web.Contents(URL, [Headers=[Authorization=API_Bearer]]), 65001)
        ]
    ),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Result"}, {"Column1.Result"}),
    #"Expanded Column1.Result" = Table.ExpandRecordColumn(#"Expanded Column1", "Column1.Result", {"data"}, {"Column1.Result.data"}),
    #"Expanded Column1.Result.data" = Table.ExpandListColumn(#"Expanded Column1.Result", "Column1.Result.data"),
    #"Expanded Column1.Result.data1" = Table.ExpandRecordColumn(#"Expanded Column1.Result.data", "Column1.Result.data", {"campaign_name", "ad_name", "impressions", "inline_link_clicks", "clicks", "date_start"}, {"campaign_name", "ad_name", "impressions", "inline_link_clicks", "clicks", "date"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1.Result.data1",{{"campaign_name", type text}, {"ad_name", type text}, {"impressions", Int64.Type}, {"inline_link_clicks", Int64.Type}, {"clicks", Int64.Type}, {"date", type date}})
in
    #"Changed Type"

Best regards, Tommy

View solution in original post

4 REPLIES 4
tommy_8
Regular Visitor

I've not been successful using List.Accumulate().

Instead List.Generate() gives me the list of pages, which can be converted to Table.

 

For any interested user:

let
    API_Bearer = "Bearer XXX",
    Source = List.Generate( ()=> 
        [
            // API Config and paramters
            facebook_account = "act_XXX",
            Facebook_API_URL = "https://graph.facebook.com/v11.0/" & facebook_account & "/insights?",
            level = "level=ad&",
            fields = "fields=campaign_name,ad_name,impressions,inline_link_clicks,clicks&",
            time_increment = "time_increment=1&",
            date_preset = "date_preset=last_30d&",
            
            URL = Facebook_API_URL & level & fields & time_increment & date_preset,
            Result = Json.Document(Web.Contents(URL, [Headers=[Authorization=API_Bearer]]), 65001)
        ],
        each [URL]<>null,
        each [
            URL = try [Result][paging][next] otherwise null,
            Result = Json.Document(Web.Contents(URL, [Headers=[Authorization=API_Bearer]]), 65001)
        ]
    ),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Result"}, {"Column1.Result"}),
    #"Expanded Column1.Result" = Table.ExpandRecordColumn(#"Expanded Column1", "Column1.Result", {"data"}, {"Column1.Result.data"}),
    #"Expanded Column1.Result.data" = Table.ExpandListColumn(#"Expanded Column1.Result", "Column1.Result.data"),
    #"Expanded Column1.Result.data1" = Table.ExpandRecordColumn(#"Expanded Column1.Result.data", "Column1.Result.data", {"campaign_name", "ad_name", "impressions", "inline_link_clicks", "clicks", "date_start"}, {"campaign_name", "ad_name", "impressions", "inline_link_clicks", "clicks", "date"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1.Result.data1",{{"campaign_name", type text}, {"ad_name", type text}, {"impressions", Int64.Type}, {"inline_link_clicks", Int64.Type}, {"clicks", Int64.Type}, {"date", type date}})
in
    #"Changed Type"

Best regards, Tommy

What does API_bearer stand for?

See the top of the code. A text string with the word Bearer, a space, and the token you received during authentication.

lbendlin
Super User
Super User

Familiarize yourself with List.Accumulate()

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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