Forked from MarkTiedemann/power-query-pagination.m
Created
September 14, 2020 18:24
-
-
Save gustavoleo/aa7ea36f93a5eda6db989e103968124c to your computer and use it in GitHub Desktop.
Power Query Pagination Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
BaseUrl = "https://fake-odata-api.com/v1/Entities?", | |
Token = "F4K3-T0K3N-D0NT-U5E-L0L", | |
EntitiesPerPage = 1000, | |
GetJson = (Url) => | |
let Options = [Headers=[ #"Authorization" = "Bearer " & Token ]], | |
RawData = Web.Contents(Url, Options), | |
Json = Json.Document(RawData) | |
in Json, | |
GetEntityCount = () => | |
let Url = BaseUrl & "$count=true&$top=0", | |
Json = GetJson(Url), | |
Count = Json[#"@odata.count"] | |
in Count, | |
GetPage = (Index) => | |
let Skip = "$skip=" & Text.From(Index * EntitiesPerPage), | |
Top = "$top=" & Text.From(EntitiesPerPage), | |
Url = BaseUrl & Skip & "&" & Top, | |
Json = GetJson(Url), | |
Value = Json[#"value"] | |
in Value, | |
EntityCount = List.Max({ EntitiesPerPage, GetEntityCount() }), | |
PageCount = Number.RoundUp(EntityCount / EntitiesPerPage), | |
PageIndices = { 0 .. PageCount - 1 }, | |
Pages = List.Transform(PageIndices, each GetPage(_)), | |
Entities = List.Union(Pages), | |
Table = Table.FromList(Entities, Splitter.SplitByNothing(), null, null, ExtraValues.Error) | |
in | |
Table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment