PowerQuery 将记录列表转换为分隔字符串
PowerQuery to Convert List of Records to Delimited String
鉴于以下 JSON 我正在尝试将其加载到 Excel。我想将 "Ratings" 部分格式化为单个分隔符 string/cell。我是 PowerQuery
的新手,所以我很难做到这一点。我已经设法将记录列表格式化为它自己的 table,但是将其连接成一个字符串并将其添加回我的源 table 是我画空白的地方。任何帮助将不胜感激。
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"
}
]
}
最终,像下面这样的东西是最理想的。
与竖线字符连接到列中。那是你想要的吗?
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"
这个怎么样?
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
不是“分组依据”对话框中的选项,因此我选择了 Max(在代码中变为 List.Max
)并将其替换为 Text.Combine
以连接作为分隔符的换行符。
鉴于以下 JSON 我正在尝试将其加载到 Excel。我想将 "Ratings" 部分格式化为单个分隔符 string/cell。我是 PowerQuery
的新手,所以我很难做到这一点。我已经设法将记录列表格式化为它自己的 table,但是将其连接成一个字符串并将其添加回我的源 table 是我画空白的地方。任何帮助将不胜感激。
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"
}
]
}
最终,像下面这样的东西是最理想的。
与竖线字符连接到列中。那是你想要的吗?
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"
这个怎么样?
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
不是“分组依据”对话框中的选项,因此我选择了 Max(在代码中变为 List.Max
)并将其替换为 Text.Combine
以连接作为分隔符的换行符。