PowerBI:将对象列表串联为列

时间:2019-06-07 22:24:48

标签: powerbi powerbi-desktop

我正在创建一个PowerBI仪表板,该仪表板显示项目和项目成员的列表。我希望列表的每一行都有一个项目,并有一个名为“ Members”的列,该列将以逗号分隔的文本形式列出成员。不幸的是,我的数据源具有作为对象数组的成员,而PowerBI似乎仅支持扩展字符串数组。

我尝试了查询编辑器和高级编辑器,但是似乎无法正确使用格式/命令。

采用JSON格式,我的数据将像这样输入:

[
    {
        projectName: "A",
        members: [
                    {
                        firstName: "John",
                        lastName: "Doe"
                    },
                    {
                        firstName: "Jane",
                        lastNahe: "Doe"
                    }
                ]
    }      
]

我希望能够这样显示它:

|  Project |  Members                 |
---------------------------------------
|    A     | John Doe, Jane Doe       |

有什么想法吗?看起来很简单,但是我遇到的麻烦要多得多。谢谢!

1 个答案:

答案 0 :(得分:1)

我带了您JSon文件,添加了一个额外的项目以进行正确的测试,并使用M-Query获得了您想要的结果:

let
    Source = Json.Document(File.Contents("C:\yourpath\ProMem.json")),
    #"Converted to Table1" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table1", "Column1", {"projectName", "members"}, {"projectName", "members"}),
#"Expanded members" = Table.ExpandListColumn(#"Expanded Column1", "members"),
#"Expanded members1" = Table.ExpandRecordColumn(#"Expanded members", "members", {"firstName", "lastName"}, {"firstName", "lastName"}),
#"Merged Columns" = Table.CombineColumns(#"Expanded members1",{"firstName", "lastName"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Name"),
#"Grouped Rows" = Table.Group(#"Merged Columns", {"projectName"}, {{"Members", each Text.Combine([Name],", "), type text}})

in
    #"Grouped Rows"

enter image description here