编辑: M 代码已更改,以允许 csv JSON 字符串中的列(产品)数量不同
从您输出的外观来看,我猜您使用了Power Query(又名Get & Transform)来输入数据。
如果是这种情况,您可以编辑查询以获得您正在寻找的输出。 (如果没有,你可以在整个过程中使用它)。
您希望从中解析输出的列是 JSON 格式,并且 PQ 有一个内置的解析器。
我根据您提供的原始 CSV 文件工作。
我们删除不相关的列和空白行,解析JSON字符串,然后重新排列数据。
除自定义列公式之外的所有步骤都可以从 GUI 完成。
自定义列公式从相关列中的 JSON 字符串中提取元素:=Json.Document([Answer.taskAnswers])
您可以将 M 代码粘贴到 PQ 的高级编辑器中,然后检查 GUI 中的步骤以查看发生了什么。
您还必须编辑 Source 行以反映您实际获取源数据的位置(可以是 URL 而不是文件)
M-代码
let
Source = Csv.Document(File.Contents("C:\Users\ron\Desktop\Stackoverflow data for question about cell formating (1).csv"),[Delimiter=",", Columns=31, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Removed Other Columns" = Table.SelectColumns(#"Promoted Headers",{"Answer.taskAnswers"}),
#"Removed Blank Rows" = Table.SelectRows(#"Removed Other Columns", each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null}))),
#"Added Custom" = Table.AddColumn(#"Removed Blank Rows", "strJSON", each Json.Document([Answer.taskAnswers])),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Answer.taskAnswers"}),
#"Expanded strJSON" = Table.ExpandListColumn(#"Removed Columns", "strJSON"),
#"Expanded strJSON1" = Table.ExpandRecordColumn(#"Expanded strJSON", "strJSON", List.Union(List.Transform(#"Expanded strJSON"[strJSON], each Record.FieldNames(_)))),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Expanded strJSON1", {"purchaseTime", "purchaseDate", "storeName"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByEachDelimiter({"-"}, QuoteStyle.Csv, true), {"Attribute.1", "Attribute.2"}),
#"Sorted Rows" = Table.Sort(#"Split Column by Delimiter",{{"Attribute.2", Order.Ascending}}),
#"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Attribute.1]), "Attribute.1", "Value"),
#"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Attribute.2"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns1",{"storeName", "purchaseDate", "purchaseTime", "product", "price", "weight", "quantity"}),
#"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns",{{"purchaseDate", type date}, {"purchaseTime", type time}, {"price", Currency.Type}, {"quantity", Int64.Type}})
in
#"Changed Type"
原始 GUI 生成的 M 代码有这行专门命名 JSON 列。它不会适应产品数量的变化。
#"Expanded strJSON1" = Table.ExpandRecordColumn(#"Expanded strJSON", "strJSON", {"price-1", "price-2", "price-3", "price-4", "price-5", "product-1", "product-2", "product-3", "product-4", "product-5", "purchaseDate", "purchaseTime", "quantity-1", "quantity-2", "quantity-3", "quantity-4", "quantity-5", "storeName", "weight-1", "weight-5", "weight-3"}, {"price-1", "price-2", "price-3", "price-4", "price-5", "product-1", "product-2", "product-3", "product-4", "product-5", "purchaseDate", "purchaseTime", "quantity-1", "quantity-2", "quantity-3", "quantity-4", "quantity-5", "storeName", "weight-1", "weight-5", "weight-3"}),
所以我修改了上面 M-Code 中的那一行,以解决这个问题。
输出
GUI 步骤