如何从Power BI中的API获取分页数据

问题描述 投票:2回答:1

假设我们有这个端点https://reqres.in/api/users

回应是

{
    "page": 1,
    "per_page": 3,
    "total": 12,
    "total_pages": 4,
    "data": [
        {
            "id": 1,
            "first_name": "George",
            "last_name": "Bluth",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
        },
        {
            "id": 2,
            "first_name": "Janet",
            "last_name": "Weaver",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
        },
        {
            "id": 3,
            "first_name": "Emma",
            "last_name": "Wong",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg"
        }
    ]
}

这是Power BI的结果

enter image description here

所以我的问题是:

  • 我如何玩data的内容,而不是只显示字符串"[List]"
  • PowerBI可以处理分页吗?它可以改变page param吗? ?page=X
powerbi powerquery m
1个回答
9
投票

如何播放数据内容,而不是仅显示字符串“[List]”?

Power BI实际上提供了一个用户友好的UI来导航和构建查询,因此您只需单击链接/按钮即可展开和深入查询并获取所需的数据:

点击List

list

转换为表格:

convert to table

展开专栏:

expand the column

结果:results

这相当于以下M / Power查询(Query -> Advanced Editor):

let
    Source = Json.Document(Web.Contents("https://reqres.in/api/users")),
    data = Source[data],
    #"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "first_name", "last_name", "avatar"}, {"id", "first_name", "last_name", "avatar"})
in
    #"Expanded Column1"

PowerBI可以处理分页吗?它可以改变页面参数吗? ?页= X

是。您实际上可以将上述查询转换为函数,并将页码传递给每个页面以获取数据。

首先,您可以从上面的查询中获取total_pages

右键单击total_pagesAdd as New queryadd as new query

您将在Query Editor中看到以下查询:

let
    Source = Json.Document(Web.Contents("https://reqres.in/api/users")),
    total_pages1 = Source[total_pages]
in
    total_pages1

更改最后一行以生成数字列表:

let
    Source = Json.Document(Web.Contents("https://reqres.in/api/users")),
    List = {1..Source[total_pages]}
in
    List

将其转换为表格:convert to table

现在对于原始查询,您可以在查询之前添加() =>以将其转换为函数,并将参数传递给它(API端点也需要更改以进行分页):

(page as text) =>
let
    Source = Json.Document(Web.Contents("https://reqres.in/api/users?page=" & page)),
    data = Source[data],
    #"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "first_name", "last_name", "avatar"}, {"id", "first_name", "last_name", "avatar"})
in
    #"Expanded Column1"

function

将该函数重命名为getPage以便更好地理解:

getPage

现在回到total_pages表。将Column1更改为文本,以便以后可以将其传递给getPagetext

然后Invoke Custom Function并用getPage调用Column1

invoke

invoked

你会看到一张桌子旁边的列表:

tables

展开它,您将在一个表中看到所有数据页面:

all data

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.