Json阵列查询-Power BI

时间:2019-08-13 11:31:28

标签: powerbi

嗨,我有下面的JSON,想在PowerBI Query中提取。我的查询无法在JSON中提取Array。我无法提取属性数组值,因为无法提取用户值。

任何帮助表示赞赏

  

Edit1:添加了其他列重命名,并且基于   @AnkUser解决方案

     

Edit2:在JSON以下

我想以超级查询形式返回

Workers      WorkCode    Place
-----------------------
Manager       134         UK
delegate      135         Europe 
Authority     etc

这些列之间没有关系。但是,它们将用作先前查询的其他过滤器数据 样本JSON

{
  "Data": [
    {
      "Type": "Workers",
      "Values": [
        "Manager",
        "Delegate",
        "Authority"
      ]
    },
    {
      "Type": "WorkCode",
      "Values": [
        "134",
        "135",
        "140",
        "141",
        "142",
        "143",
        "150"
      ]
    },
    {
      "Type": "Place",
      "Values": [
        "UK",
        "Europe"
      ]
    }
  ]
}

下面的样本功率查询:

let
    Source = Json.Document(Web.Contents("http:localhost")),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"User", "Properties"}, {"Column1.User", "Column1.Properties"}),
    #"Expanded Column1.User" = Table.ExpandRecordColumn(#"Expanded Column1", "Column1.User", {"recId", "Description", "Type", }, {"Column1.User.recId", "Column1.User.Description", "Column1.User.Type"}),
    #"Expanded Column1.Properties" = Table.ExpandListColumn(#"Expanded Column1.User", "Column1.Properties"),
    #"Expanded Column1.Properties1" = Table.ExpandRecordColumn(#"Expanded Column1.Properties", "Column1.Properties", {"PersonID", "HomeRef", "Designation", "EstateAgent", "Mortgage", "Broker", "Citizen"}, {"Column1.Properties.PersonID", "Column1.Properties.HomeRef", "Column1.Funds.Designation", Column1.Properties.EstateAgent", Column1.Properties.Mortgage", Column1.Properties.Broker",Column1.Properties.Citizen"})
    )
in
    #"Expanded Column1"

样本数据:

    [
        {
            "User": {
                "recId": "0154911",
                "Description": "Lindsay Properties ltd",
                "Type": "Organisation",
                "Properties": [
                    {
                        "PersonID": 5636,
                        "HomeRef": 149065,
                        "Designation":"Owner",
                        "EstateAgent": {
                            "Code": "8533",
                            "Description": "Hunters-properties"
                        },
                        "Mortgage": {
                            "Code": "natwide",
                            "Description": "Bank limited"
                        },
                        "Broker": {
                            "Description": "Managecentre"
                        },
                        "Citizen": {
                            "UK": true,
                            "USA": false,
                            "Europe": false
                        }
                    },
                    {
                        "PersonID": 5636,
                        "HomeRef": 149066,
                        "Designation":"Owner",
                        "EstateAgent": {
                            "Code": "8533",
                            "Description": "Hunters-properties"
                        },
                        "Mortgage": {
                            "Code": "natwide",
                            "Description": "Bank limited"
                        },
                        "Broker": {
                            "Description": "Managecentre"
                        },
                        "Citizen": {
                            "UK": false,
                            "USA": false,
                            "Europe": false
                        }
                    }
                ]
            }
        },


   {
            "User": {
                "recId": "0154912",
                "Description": "Mr Mortimier properties",
                "Type": "Person",
                "Properties": [
                    {
                        "PersonID": 1636,
                        "HomeRef": 199065,
                        "Designation":"Owner",
                        "EstateAgent": {
                            "Code": "9533",
                            "Description": "Whitegates-properties"
                        },
                        "Mortgage": {
                            "Code": "Yoskhire society",
                            "Description": "society limited"
                        },
                        "Broker": {
                            "Description": "Managecentre"
                        },
                        "Citizen": {
                            "UK": true,
                            "USA": true,
                            "Europe": false
                        }
                    },
                    {
                        "PersonID": 1636,
                        "HomeRef": 199066,
                        "Designation":"Authority",
                        "EstateAgent": {
                            "Code": "9533",
                            "Description": "Whitegates-properties"
                        },
                        "Mortgage": {
                            "Code": "Yoskhire society",
                            "Description": "society limited"
                        },
                        "Broker": {
                            "Description": "Managecentre"
                        },
                        "Citizen": {
                            "UK": true,
                            "USA": true,
                            "Europe": false
                        }
                    }
                ]
            }

        }]

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,则希望将属性的数组扩展为行的列。 为了测试您的用例,我使用了您的数据,并尝试从中创建行。下面的截图是结果。

enter image description here

如果这是您所需要的,下面是我从PowerBI获得的查询,它给出了结果。

注意:您可能要清除列名。

let
    Source = Json.Document(File.Contents("C:\Users\achikhale\Desktop\stackoverflowPowerBIJson.json")),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"User"}, {"Column1.User"}),
    #"Expanded Column1.User" = Table.ExpandRecordColumn(#"Expanded Column1", "Column1.User", {"recId", "Description", "Type", "Properties"}, {"Column1.User.recId", "Column1.User.Description", "Column1.User.Type", "Column1.User.Properties"}),
    #"Expanded Column1.User.Properties" = Table.ExpandListColumn(#"Expanded Column1.User", "Column1.User.Properties"),
    #"Expanded Column1.User.Properties1" = Table.ExpandRecordColumn(#"Expanded Column1.User.Properties", "Column1.User.Properties", {"PersonID", "HomeRef", "Designation", "EstateAgent", "Mortgage", "Broker", "Citizen"}, {"Column1.User.Properties.PersonID", "Column1.User.Properties.HomeRef", "Column1.User.Properties.Designation", "Column1.User.Properties.EstateAgent", "Column1.User.Properties.Mortgage", "Column1.User.Properties.Broker", "Column1.User.Properties.Citizen"}),
    #"Expanded Column1.User.Properties.EstateAgent" = Table.ExpandRecordColumn(#"Expanded Column1.User.Properties1", "Column1.User.Properties.EstateAgent", {"Code", "Description"}, {"Column1.User.Properties.EstateAgent.Code", "Column1.User.Properties.EstateAgent.Description"}),
    #"Expanded Column1.User.Properties.Mortgage" = Table.ExpandRecordColumn(#"Expanded Column1.User.Properties.EstateAgent", "Column1.User.Properties.Mortgage", {"Code", "Description"}, {"Column1.User.Properties.Mortgage.Code", "Column1.User.Properties.Mortgage.Description"}),
    #"Expanded Column1.User.Properties.Broker" = Table.ExpandRecordColumn(#"Expanded Column1.User.Properties.Mortgage", "Column1.User.Properties.Broker", {"Description"}, {"Column1.User.Properties.Broker.Description"}),
    #"Expanded Column1.User.Properties.Citizen" = Table.ExpandRecordColumn(#"Expanded Column1.User.Properties.Broker", "Column1.User.Properties.Citizen", {"UK", "USA", "Europe"}, {"Column1.User.Properties.Citizen.UK", "Column1.User.Properties.Citizen.USA", "Column1.User.Properties.Citizen.Europe"})
in
    #"Expanded Column1.User.Properties.Citizen"

如果这是您需要的,我可以添加一些有关如何实现这种数据模型的解释(步骤)

编辑: 数据的新查询注意:您的Json

let
    Source = Json.Document(File.Contents("C:\Users\achikhale\Desktop\stackoverflowPowerBIJson1.json")),
    #"Converted to Table" = Record.ToTable(Source),
    #"Expanded Value" = Table.ExpandListColumn(#"Converted to Table", "Value"),
    #"Expanded Value1" = Table.ExpandRecordColumn(#"Expanded Value", "Value", {"Type", "Values"}, {"Value.Type", "Value.Values"}),
    #"Expanded Value.Values" = Table.ExpandListColumn(#"Expanded Value1", "Value.Values")
in
    #"Expanded Value.Values"

enter image description here

但是如果我按如下方式编辑您的Json

[{
        "Data": [{
                "Type": "Workers",
                "Values": [
                    "Manager",
                    "Delegate",
                    "Authority"
                ]
            }, {
                "Type": "WorkCode",
                "Values": [
                    "134",
                    "135",
                    "140",
                    "141",
                    "142",
                    "143",
                    "150"
                ]
            }, {
                "Type": "Place",
                "Values": [
                    "UK",
                    "Europe"
                ]
            }
        ]
    }
]

然后,您将获得更加整洁的Table以及包含以下查询的行。

注意以下查询仅适用于上述我编辑过的Json。

let
    Source = Json.Document(File.Contents("C:\Users\achikhale\Desktop\stackoverflowPowerBIJson1.json")),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Data"}, {"Column1.Data"}),
    #"Expanded Column1.Data" = Table.ExpandListColumn(#"Expanded Column1", "Column1.Data"),
    #"Expanded Column1.Data1" = Table.ExpandRecordColumn(#"Expanded Column1.Data", "Column1.Data", {"Type", "Values"}, {"Column1.Data.Type", "Column1.Data.Values"}),
    #"Expanded Column1.Data.Values" = Table.ExpandListColumn(#"Expanded Column1.Data1", "Column1.Data.Values")
in
    #"Expanded Column1.Data.Values"

enter image description here