用其他网址的内容扩展列

时间:2018-08-10 09:19:29

标签: json powerbi m

我在自己的powerBI Data连接器中使用以下代码从json文档获取日期:

{
  "Customers": [
    {
      "CustomerId": "8cd72f16-8d7b-48b0-90d9-71df011502c8",
      "CustomerTitle": "Test Customer",
    }
}

代码:

GetCustomerTable = (url as text) as table =>
    let
        source = Test.Feed(url & "/overview"),
        value = source[Customers],
        toTable = Table.FromList(value, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"expandColumn" = Table.ExpandRecordColumn(toTable, "Column1", {"CustomerId", "CustomerTitle"}, {"CustomerId", "CustomerTitle"}),
        #"ChangedType" = Table.TransformColumnTypes(#"expandColumn",{{"CustomerTitle", type text}, {"CustomerId", type text})
    in
        ChangedType;

“ CustomerId”列引用了另一个网址,其中以json格式提供了有关客户的实际数据:

URL: /Details/8cd72f16-8d7b-48b0-90d9-71df011502c8

{
  "Category": "B",
}

通过ExpandRecordColumn函数使用来自另一个URL的数据的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

因此,您需要的是另一个自定义函数,以获取每个CustomerId的客户详细信息,作为以下步骤之一:

GetCustomerDetails = (url as text, customer_id as text) =>
let
    Source = Json.Document(Web.Contents(url & "/Details/" & customer_id)),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Category"}, {"Category"})
in
    #"Expanded Column"

然后您可以通过传递urlCustomerId列来在原始代码中调用此函数:

GetCustomerTable = (url as text) as table =>
    let
        source = Test.Feed(url & "/overview"),
        value = source[Customers],
        toTable = Table.FromList(value, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        "expandColumn" = Table.ExpandRecordColumn(toTable, "Column1", {"CustomerId", "CustomerTitle"}, {"CustomerId", "CustomerTitle"}),
        "ChangedType" = Table.TransformColumnTypes(#"expandColumn",{{"CustomerTitle", type text}, {"CustomerId", type text}),
        #"Invoked Custom Function" = Table.AddColumn(#"ChangedType", "GetCustomerDetails", each GetCustomerDetails("http://testing.com/", [CustomerId]))
    in
        #"Invoked Custom Function"

您可能需要对代码进行一些调整,具体取决于代码的外观,但我希望您明白这一点。