PowerQuery 将记录列表转换为分隔字符串

win*_*sgm 2 m powerquery

鉴于下面的 JSON,我尝试将其加载到 Excel 中。我想将“评级”部分格式化为单个分​​隔字符串/单元格。我很新,PowerQuery所以我正在努力做到这一点。我已经设法将记录列表格式化为其自己的表,但是将其连接成一个字符串并将其添加回我的源表中是我绘制空白的地方。任何帮助,将不胜感激。

强大查询

let
    Source = Json.Document(File.Contents("C:\filename.json")),
    Ratings1 = Source[Ratings],
    #"Converted to Table" = Table.FromList(Ratings1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    LastStep = Table.ExpandRecordColumn(#"Converted to Table", "Column1", { "Source", "Value" })
in
    LastStep
Run Code Online (Sandbox Code Playgroud)

JSON

{
    "Title": "Iron Man",
    "Year": "2008",
    "Rated": "PG-13",
    "Ratings": [{
            "Source": "Internet Movie Database",
            "Value": "7.9/10"
        }, {
            "Source": "Rotten Tomatoes",
            "Value": "93%"
        }, {
            "Source": "Metacritic",
            "Value": "79/100"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

最终,像下面这样的东西将是理想的。

在此输入图像描述

Ale*_*son 5

这个怎么样?

let
    Source = Json.Document(File.Contents("C:\filename.json")),
    #"Converted to Table" = Record.ToTable(Source),
    #"Transposed Table" = Table.Transpose(#"Converted to Table"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Title", type text}, {"Rated", type text}, {"Year", Int64.Type}}),
    #"Expanded Ratings" = Table.ExpandListColumn(#"Changed Type", "Ratings"),
    #"Expanded Ratings1" = Table.ExpandRecordColumn(#"Expanded Ratings", "Ratings", {"Source", "Value"}, {"Source", "Value"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Ratings1", "Custom", each [Source] & "=" & [Value]),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Title", "Year", "Rated"}, {{"Ratings", each Text.Combine([Custom],"#(lf)"), type text}})
in
    #"Grouped Rows"
Run Code Online (Sandbox Code Playgroud)

结果

这里的大多数步骤从名称上就相当清楚,并且是通过 GUI 控件生成的。一个更棘手的步骤是我在进行分组时使用自定义聚合器。如果您使用 GUI,则Text.Combine“分组依据”对话框中没有选项,因此我选择“Max”(它会出现List.Max在代码中)并将其替换为Text.Combine与换行符作为分隔符连接。