gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:54:27 24 4
gpt4 key购买 nike

鉴于以下 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"
}
]
}

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

enter image description here

最佳答案

这个怎么样?

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"

Result

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

关于PowerQuery 将记录列表转换为分隔字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56702972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com