没有聚合的数据透视表-Power BI

时间:2019-08-15 11:15:42

标签: powerbi

我已使用上一篇文章中的功耗查询将JSON数组转换为PowerBI数据。 旋转列会在转换时引发错误

  

错误:Expression.Error:元素过多   枚举完成操作。详细信息:列表

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

预期输出:

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

查询:

let
    Source = Json.Document(Web.Contents("http://127.0.0.1:8083/api/v1/rthb-e-powerBI-client-login-report/refData")),
   #"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"),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Value.Values",{"Name"})

in
    #"Removed Columns" 

赞赏任何投入

1 个答案:

答案 0 :(得分:2)

在指定的JSON文件中,“ Workers”,“ WorkCode”和“ Place”之间没有关系,因此出错。 可以根据职位假设特定的工作人员代码属于特定的工作人员。

要在Power Bi中获得理想的结果,需要在工人的代码和位置之间建立关系。可以完成以下操作:

1)添加索引列(例如“索引”) enter image description here

2)按工作者enter image description here

对行进行分组

3)添加另一个索引列(子索引)

= Table.AddColumn(#"Grouped Rows", "subindex", each Table.AddIndexColumn([codes], "wcode", 1,1))

4)现在可以删除不需要的列(此示例中为“代码”)

5)展开“子索引”列enter image description here

6)现在,可以删除列“ colname”,“ subindex.Index”。最终结果应该与此enter image description here

类似

7)旋转表格enter image description here

这是完整的Power Query代码

let
    Source = Json.Document(File.Contents("<<PATH TO YOUR JSON FILE>>")),
    #"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"),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Value.Values",{{"Value.Type", "colname"}, {"Value.Values", "colvalue"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Name"}),
    #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1),
    #"Grouped Rows" = Table.Group(#"Added Index", {"colname"}, {{"codes", each _, type table [colname=text, colvalue=text, Index=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "subindex", each Table.AddIndexColumn([codes], "wcode", 1,1)),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"codes"}),
    #"Expanded subindex" = Table.ExpandTableColumn(#"Removed Columns1", "subindex", {"colname", "colvalue", "Index", "wcode"}, {"subindex.colname", "subindex.colvalue", "subindex.Index", "subindex.wcode"}),
    #"Removed Columns2" = Table.RemoveColumns(#"Expanded subindex",{"colname", "subindex.Index"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns2", List.Distinct(#"Removed Columns2"[subindex.colname]), "subindex.colname", "subindex.colvalue")
in
    #"Pivoted Column"