【问题标题】:PowerQuery to Convert List of Records to Delimited StringPowerQuery 将记录列表转换为分隔字符串
【发布时间】:2019-06-21 11:55:16
【问题描述】:

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

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

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"
        }
    ]
}

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

【问题讨论】:

    标签: powerquery m


    【解决方案1】:

    这个怎么样?

    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"
    

    这里的大部分步骤从名称上看都很清楚,并且是通过 GUI 控件生成的。一个更棘手的步骤是我在进行分组时使用自定义聚合器。如果您使用 GUI,Text.Combine 不是 Group By 对话框中的选项,因此我选择 Max(在代码中变为 List.Max)并将其替换为 Text.Combine 以与换行符连接作为分隔符。

    【讨论】:

    • 太好了!谢谢。
    • 也感谢您提供更多详细信息。我想知道是否可以动态添加列并将所有列视为文本?评级是安全的,但我的意思是如果有更多的列以及标题、年份和评级。
    【解决方案2】:

    用竖线字符连接到列中。这就是你想要的吗?

    let
    Source = Json.Document(File.Contents("C:\temp\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" }),
    #"Added Custom" = Table.AddColumn(LastStep, "Concat", each [Source]&"|"&[Value]),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Concat"})
    in #"Removed Other Columns"
    

    【讨论】:

    • 它更接近了,谢谢。请参阅我的更新答案以获得理想结果。
    猜你喜欢
    • 1970-01-01
    • 2010-10-19
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 2013-03-09
    • 2011-06-07
    相关资源
    最近更新 更多