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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply

Pagination over my API calls to get all data

I have this query to get data from external api, currently the api only allow to get the first 10,000 items:-

 

 

let
    body=Text.Combine({"sAPIKey=",ApiKey}),
	SourceAPILogin=Json.Document(
					Web.Contents(
							SmartAPI,
								[
								RelativePath="apilogin",
								Headers=[Accept="application/json", #"Content-Type"="application/x-www-form-urlencoded",#"API-Key"=Token],
								Content=Text.ToBinary(body)
								]
							)
					),
    tblGetUID = Table.FromList(SourceAPILogin, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    expGetUID = Table.ExpandRecordColumn(tblGetUID, "Column1", {"UID", "LanguageCode", "DatabaseVersion"}, {"Column1.UID", "Column1.LanguageCode", "Column1.DatabaseVersion"}),
    GetUID = expGetUID{0}[Column1.UID],
    Source=Json.Document(
				Web.Contents(
					SmartAPI,
							[
							RelativePath = "timeledgerentry", //RESTAPI endpoint
							//Query = [#"Filter" ="WorkTypeCode LIKE 'Normal%'"], // QueryParameters
								Headers=[Accept="application/json", #"Content-Type"="application/json",#"API-Key"=Token, UID=GetUID, DeviceID=ApiKey] //Headers
							]
					)
				),
    tblAll = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
	tableHasRecords = Table.RowCount(tblAll) > 0,
	tblTimeLedgerEntries=
		if tableHasRecords then
			let
			Result=Table.ExpandRecordColumn(tblAll,"Column1", Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(tblAll,"Column1"), each _ <> "" and _ <> null))),Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(tblAll,"Column1"), each _ <> "" and _ <> null)))),
			#"Changed Type" = Table.TransformColumnTypes(Result,{{"StartTime", type datetime}, {"EndTime", type datetime}, {"LogStart", type datetime}, {"LogEnd", type datetime}})
			in
			#"Changed Type"
		else
				let
					strColumnNames={
						"EntryID","EmployeeCode","Name","Date","LogStart","LogEnd","WorkTypeCode","BreakTime","StartTime","EndTime","TotalTime","WorkCalendarCode","Type","Remark","DepartmentCode",
						"WorkFunctionCode","Relative","PeriodName","JournalCode","EmployeeGroupCode","DistrictCode","TeamLeaderCode","Canceled","PayrollTypeCode","JournalLineEntryID",
						"OverTimeCode","DimensionCode1","DimensionCode2","DimensionCode3","DimensionCode4","DimensionCode5","DimensionCode7","DimensionCode6","DimensionCode8","DimensionCode9","DimensionCode10",
						"PayrollReference","JournalLineID"
					},
					// Create an empty table with the defined column names
					emptyTable = #table(strColumnNames, {})
				in
					emptyTable
   
in
    tblTimeLedgerEntries

 

 

so to get all items i can do pagination using the offset and limit paramters, so i modified my above query as follow:-

 

 

let
    // Function to fetch data with pagination
    FetchTimeLedgerEntries = (Offset as number, Limit as number) as table =>
        let
            body = Text.Combine({"sAPIKey=", ApiKey}),
            SourceAPILogin = Json.Document(
                                Web.Contents(
                                    SmartAPI,
                                    [
                                        RelativePath="apilogin",
                                        Headers=[Accept="application/json", #"Content-Type"="application/x-www-form-urlencoded",#"API-Key"=Token],
                                        Content=Text.ToBinary(body)
                                    ]
                                )
                            ),
            tblGetUID = Table.FromList(SourceAPILogin, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
            expGetUID = Table.ExpandRecordColumn(tblGetUID, "Column1", {"UID", "LanguageCode", "DatabaseVersion"}, {"Column1.UID", "Column1.LanguageCode", "Column1.DatabaseVersion"}),
            GetUID = expGetUID{0}[Column1.UID],
            Source = Json.Document(
                        Web.Contents(
                            SmartAPI,
                            [
                                RelativePath = "timeledgerentry",
                                Query = [#"offset"=Text.From(Offset), #"limit"=Text.From(Limit)], // Adding Offset and Limit parameters
                                Headers=[Accept="application/json", #"Content-Type"="application/json",#"API-Key"=Token, UID=GetUID, DeviceID=ApiKey] 
                            ]
                        )
                    ),
            tblAll = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
            tableHasRecords = Table.RowCount(tblAll) > 0,
            Result = if tableHasRecords then
                        Table.ExpandRecordColumn(tblAll,"Column1", Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(tblAll,"Column1"), each _ <> "" and _ <> null))),Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(tblAll,"Column1"), each _ <> "" and _ <> null))))
                     else
                        #table(Table.ColumnNames(tblAll), {})
        in
            Result,

    // Loop through pages to get all records
    Offset = 0,
    Limit = 10000,
    PageSize = 10000,
    AllPages = List.Generate(
                    () => [Result = FetchTimeLedgerEntries(Offset, Limit), Offset = Offset + PageSize],
                    each Table.RowCount([Result]) > 0,
                    each [Result = FetchTimeLedgerEntries([Offset], Limit), Offset = [Offset] + PageSize],
                    each [Result]
                ),
    tblTimeLedgerEntries = Table.Combine(AllPages),
    #"Changed Type" = Table.TransformColumnTypes(tblTimeLedgerEntries,{{"StartTime", type datetime}, {"EndTime", type datetime}, {"LogStart", type datetime}, {"LogEnd", type datetime}})
in
    #"Changed Type"

 

 

so is my modification valid for doing the pagination?

 

Thanks

2 REPLIES 2

@Sahir_Maharajnow my code is working well, so what is the is dfferences between your code and mine? can you explore more please?

Sahir_Maharaj
Super User
Super User

Hello @mvcsharepointde,

 

Can you please try the following:

 

let
    // Function to fetch data with pagination
    FetchTimeLedgerEntries = (Offset as number, Limit as number) as table =>
        let
            body = Text.Combine({"sAPIKey=", ApiKey}),
            SourceAPILogin = Json.Document(
                                Web.Contents(
                                    SmartAPI,
                                    [
                                        RelativePath="apilogin",
                                        Headers=[Accept="application/json", #"Content-Type"="application/x-www-form-urlencoded",#"API-Key"=Token],
                                        Content=Text.ToBinary(body)
                                    ]
                                )
                            ),
            tblGetUID = Table.FromList(SourceAPILogin, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
            expGetUID = Table.ExpandRecordColumn(tblGetUID, "Column1", {"UID", "LanguageCode", "DatabaseVersion"}, {"Column1.UID", "Column1.LanguageCode", "Column1.DatabaseVersion"}),
            GetUID = expGetUID{0}[Column1.UID],
            Source = Json.Document(
                        Web.Contents(
                            SmartAPI,
                            [
                                RelativePath = "timeledgerentry",
                                Query = [#"offset"=Text.From(Offset), #"limit"=Text.From(Limit)], // Adding Offset and Limit parameters
                                Headers=[Accept="application/json", #"Content-Type"="application/json",#"API-Key"=Token, UID=GetUID, DeviceID=ApiKey] 
                            ]
                        )
                    ),
            tblAll = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
            tableHasRecords = Table.RowCount(tblAll) > 0,
            Result = if tableHasRecords then
                        Table.ExpandRecordColumn(tblAll,"Column1", Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(tblAll,"Column1"), each _ <> "" and _ <> null))),Table.ColumnNames(Table.FromRecords(List.Select(Table.Column(tblAll,"Column1"), each _ <> "" and _ <> null))))
                     else
                        #table({}, {})
        in
            Result,

    // Loop through pages to get all records
    Offset = 0,
    Limit = 10000,
    PageSize = 10000,
    AllPages = List.Generate(
                    () => [Result = FetchTimeLedgerEntries(Offset, Limit), Offset = Offset + PageSize],
                    each Table.RowCount([Result]) > 0,
                    each [Result = FetchTimeLedgerEntries([Offset], Limit), Offset = [Offset] + PageSize],
                    each [Result]
                ),
    tblTimeLedgerEntries = Table.Combine(AllPages),
    #"Changed Type" = Table.TransformColumnTypes(tblTimeLedgerEntries,{{"StartTime", type datetime}, {"EndTime", type datetime}, {"LogStart", type datetime}, {"LogEnd", type datetime}})
in
    #"Changed Type"

Hope this helps!


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?
➤ 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

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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

Top Solution Authors