【问题标题】:Power BI pagination :: How to pull all data from API using "NextPageLink"Power BI 分页 :: 如何使用“NextPageLink”从 API 中提取所有数据
【发布时间】:2022-11-10 20:47:12
【问题描述】:

我想从官方Azure Retail Prices overview拉出所有Azure资源价格。

使用 Power BI,我可以通过这种方式轻松地从 URL https://prices.azure.com/api/retail/prices 检索前 100 条记录:

let
    Source = Json.Document(Web.Contents("https://prices.azure.com/api/retail/prices")),
    #"Converted to Table" = Table.FromRecords({Source}),
    #"Expanded Items" = Table.ExpandListColumn(#"Converted to Table", "Items"),
    #"Expanded Items1" = Table.ExpandRecordColumn(#"Expanded Items", "Items", {"currencyCode", "tierMinimumUnits", "reservationTerm", "retailPrice", "unitPrice", "armRegionName", "location", "effectiveStartDate", "meterId", "meterName", "productId", "skuId", "availabilityId", "productName", "skuName", "serviceName", "serviceId", "serviceFamily", "unitOfMeasure", "type", "isPrimaryMeterRegion", "armSkuName"}, {"Items.currencyCode", "Items.tierMinimumUnits", "Items.reservationTerm", "Items.retailPrice", "Items.unitPrice", "Items.armRegionName", "Items.location", "Items.effectiveStartDate", "Items.meterId", "Items.meterName", "Items.productId", "Items.skuId", "Items.availabilityId", "Items.productName", "Items.skuName", "Items.serviceName", "Items.serviceId", "Items.serviceFamily", "Items.unitOfMeasure", "Items.type", "Items.isPrimaryMeterRegion", "Items.armSkuName"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Items1",{{"BillingCurrency", type text}, {"CustomerEntityId", type text}, {"CustomerEntityType", type text}, {"Items.currencyCode", type text}, {"Items.tierMinimumUnits", Int64.Type}, {"Items.reservationTerm", type any}, {"Items.retailPrice", type number}, {"Items.unitPrice", type number}, {"Items.armRegionName", type text}, {"Items.location", type text}, {"Items.effectiveStartDate", type datetime}, {"Items.meterId", type text}, {"Items.meterName", type text}, {"Items.productId", type text}, {"Items.skuId", type text}, {"Items.availabilityId", type any}, {"Items.productName", type text}, {"Items.skuName", type text}, {"Items.serviceName", type text}, {"Items.serviceId", type text}, {"Items.serviceFamily", type text}, {"Items.unitOfMeasure", type text}, {"Items.type", type text}, {"Items.isPrimaryMeterRegion", type logical}, {"Items.armSkuName", type text}, {"NextPageLink", type text}, {"Count", Int64.Type}})
in
    #"Changed Type" 

但是页面以以下结尾:

"NextPageLink": "https://prices.azure.com:443/api/retail/prices?$skip=100","Count": 100

我怎样才能让 Power BI 单击该链接并传递到下一页......等等......直到没有更多页面?

【问题讨论】:

标签: rest pagination powerbi powerquery m


【解决方案1】:

干得好:

我只检索了前 500 行。要检索所有内容,请删除以下内容:和 [偏移] < 500

let
    Query1 = let
    
        
a = 

List.Generate( () => [ Offset = 0, data = pagingFunction( 100 ) ],
                  each [data][NextPageLink] <> null and [Offset] < 500, 
                  each [ data = pagingFunction( [Offset] ),
                         Offset = [Offset] + 100 ], 
                  each [data]
),

    pagingFunction = (offset) =>
    let
        Source = Json.Document(Web.Contents("https://prices.azure.com/api/retail/prices?$skip="& Number.ToText( offset ))),
        Convert = Table.FromRecords({Source}),
        #"Removed Other Columns" = Table.SelectColumns(Convert,{"Items", "NextPageLink"}),
        #"Expanded Items" = Table.ExpandListColumn(#"Removed Other Columns", "Items"),
        #"Expanded Items1" = Table.ExpandRecordColumn(#"Expanded Items", "Items", {"currencyCode", "tierMinimumUnits", "retailPrice", "unitPrice", "armRegionName", "location", "effectiveStartDate", "meterId", "meterName", "productId", "skuId", "availabilityId", "productName", "skuName", "serviceName", "serviceId", "serviceFamily", "unitOfMeasure", "type", "isPrimaryMeterRegion", "armSkuName"}, {"Items.currencyCode", "Items.tierMinimumUnits", "Items.retailPrice", "Items.unitPrice", "Items.armRegionName", "Items.location", "Items.effectiveStartDate", "Items.meterId", "Items.meterName", "Items.productId", "Items.skuId", "Items.availabilityId", "Items.productName", "Items.skuName", "Items.serviceName", "Items.serviceId", "Items.serviceFamily", "Items.unitOfMeasure", "Items.type", "Items.isPrimaryMeterRegion", "Items.armSkuName"})
    in
        #"Expanded Items1"
, b= Table.FromList({a})

in a,
    #"Converted to Table" = Table.FromList(Query1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandTableColumn(#"Converted to Table", "Column1", {"Items.currencyCode", "Items.tierMinimumUnits", "Items.retailPrice", "Items.unitPrice", "Items.armRegionName", "Items.location", "Items.effectiveStartDate", "Items.meterId", "Items.meterName", "Items.productId", "Items.skuId", "Items.availabilityId", "Items.productName", "Items.skuName", "Items.serviceName", "Items.serviceId", "Items.serviceFamily", "Items.unitOfMeasure", "Items.type", "Items.isPrimaryMeterRegion", "Items.armSkuName", "NextPageLink"}, {"Items.currencyCode", "Items.tierMinimumUnits", "Items.retailPrice", "Items.unitPrice", "Items.armRegionName", "Items.location", "Items.effectiveStartDate", "Items.meterId", "Items.meterName", "Items.productId", "Items.skuId", "Items.availabilityId", "Items.productName", "Items.skuName", "Items.serviceName", "Items.serviceId", "Items.serviceFamily", "Items.unitOfMeasure", "Items.type", "Items.isPrimaryMeterRegion", "Items.armSkuName", "NextPageLink"})
in
    #"Expanded Column1"

【讨论】:

  • 谢谢@David,但没有and [Offset] &lt; 500 它会永远运行
  • 有超过 10 万条记录,因此需要一段时间。
  • 1 小时过去了,PowerQuery 仍在迭代。这正常吗?
  • 您正在以 100 行块的形式从网络中检索 > 100k 行,因此这将需要很长时间......
  • 100.000 行/100 个块 = 1000 个查询。如果每个查询需要 1 秒,我们只需要 1000 秒来解析整个 API。所以应该在 16 分钟内完成。这里有什么鱼腥味...
猜你喜欢
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多