Forum Discussion
how to create a query that paginates?
Hi Anonymous ,
This can be sorted by following the below steps.
You'll require 4 add ons i.e., 1 parameter and 3 functions. Replace the information suiting your organization URL.
- Parameter: URL
e.g., https : // [Replace with your organization URL] / jira
- Function 01: FetchPage
let
FetchPage = (url as text, pageSize as number, skipRows as number) as table =>
let
//Here is where you run the code that will return a single page
contents = Web.Contents(URL&"/rest/api/2/search",[Query = [maxResults= Text.From(pageSize), startAt = Text.From(skipRows)]]),
json = Json.Document(contents),
Value = json[issues],
table = Table.FromList(Value, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
table meta [skipRows = skipRows + pageSize, total = 500]
in
FetchPage
- Function: FetchPages
let
FetchPages = (url as text, pageSize as number) =>
let
Source = GenerateByPage(
(previous) =>
let
skipRows = if previous = null then 0 else Value.Metadata(previous)[skipRows],
totalItems = if previous = null then 0 else Value.Metadata(previous)[total],
table = if previous = null or Table.RowCount(previous) = pageSize then
FetchPage(url, pageSize, skipRows)
else null
in table,
type table [Column1])
in
Source
in
FetchPages
- Function: GeneratebyPage
(getNextPage as function, optional tableType as type) as table =>
let
listOfPages = List.Generate(
() => getNextPage(null),
(lastPage) => lastPage <> null,
(lastPage) => getNextPage(lastPage)
),
tableOfPages = Table.FromList(listOfPages, Splitter.SplitByNothing(), {"Column1"}),
firstRow = tableOfPages{0}?,
keys = if tableType = null then Table.ColumnNames(firstRow[Column1])
else Record.FieldNames(Type.RecordFields(Type.TableRow(tableType))),
appliedType = if tableType = null then Value.Type(firstRow[Column1]) else tableType
in
if tableType = null and firstRow = null then
Table.FromRows({})
else
Value.ReplaceType(Table.ExpandTableColumn(tableOfPages, "Column1", keys), appliedType)
Once the above is created, the next step will be is to use the function and parameters to retrieve the content. you can start with the below and expand the fields of choice to create your report.
Main query:
let
Source = FetchPages("", 500),
#"Expanded Column1" = Table.ExpandRecordColumn(Source, "Column1", {"key", "fields"}, {"key", "fields"}),
in
#"Expanded Column1"
Hope this helps you to build the report. Please note this query is only the search information and if you'd need change log information you can modify that in the 2nd function listed above.
Good luck.
Cheers,
Anand
Hi Anonymous ,
Thanks a lot for your answer!!!
Unfortunately I still have a few because, because this topic overtaxes me at the moment a bit.
I added the parameter URL and it works.
The next step is to paste all the functions to my query, right?
I did it and got a failure on Function: FetchPages, there is a problem with “let”.
I don´t know why.
To understand a bit what your code does I just added 1.Function 01:FetchPage to my query.
I got three parameters: URL, SkipRows and pageSize.
Do I understood the first function right, that this one can give my table on one page, depending on the pageSize? If I add 2000 to this parameter, I get 2000 rows on one page. However, it´s just static and not dynamical?
Sorry for stupid questions, but M-language is difficult for me and try to understand it.
thanks a lot for your support and hopefully your patience!
freiburgc
- Anonymous5 years agoNot applicable
Hi Anonymous ,
I have tried to work with Multi-step function.
Unfortunately I got a problem - Expression.Error: The Name "GenerateByPage" wasn't recognized.
I tried "Table.GenerateByPage", doesn´t work.
let FetchPages = (url as text, pageSize as number) => let Source =GenerateByPage( (previous) => let skipRows = if previous = null then 0 else Value.Metadata(previous)[skipRows], totalItems = if previous = null then 0 else Value.Metadata(previous)[total], table = if previous = null or Table.RowCount(previous) = pageSize then FetchPage(url, pageSize, skipRows) else null in table, type table [Column1]) in Source in FetchPagesMaybe you have a look at it or someelse, that would great!!
Thanks again!
freiburgc
- Anonymous5 years agoNot applicable
Hi Anonymous ,
Instead of having them all listed in one query, could you please have them created as indicated by me i.e., to create 3 queries and give it a try?
Regards,
A!
- Anonymous5 years agoNot applicable
Hi Anonymous ,
I have created different queries and it almost works now.
Unfortunately another problem has emerged.
I have extracted a customfield of the query and i got a failure.
"Expression Error: Value "null" cannot be converted to Typ List.
Details:
Value=
Type=[Type]
= Table.TransformColumns(#"Erweiterte Column1.fields.customfield_13101", {"Column1.fields.customfield_13100", each Text.Combine(List.Transform(_, Text.From)), type text})Do have an idea how to solve it?
Thanks alot!
freiburgc