使用 power query 按特定顺序排列值
Arranging values in a specific order using power query
我希望清理 table 我一直致力于删除复杂的公式并选择使用 PQE 对我的数据进行排序。
下面是我正在处理的数据的虚构示例。
我希望输出列在 material 和子值之间交替,类似于书中的 headers 和子 headers。
M 目前代码:
let
Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Material ", type text}, {"Sub ", type text}, {"CAS", type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Material ", Text.Trim, type text}, {"Sub ", Text.Trim, type text}}),
#"Cleaned Text" = Table.TransformColumns(#"Trimmed Text",{{"Sub ", Text.Clean, type text}}),
#"Replaced Value" = Table.ReplaceValue(#"Cleaned Text","-","",Replacer.ReplaceText,{"Sub "}),
#"Merged Columns" = Table.CombineColumns(#"Replaced Value",{"Material ", "Sub "},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged"),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Merged Columns", {{"Merged", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Merged"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Merged", type text}})
in
#"Changed Type1"
此代码的问题是它在组合时没有正确列出 material,而且我也不知道如何正确排序 CAS 编号。
如果有人对如何实现所需的输出有任何想法,我们将不胜感激。
您可以使用我在上一个类似问题中向您展示的相同技术从您的输入中获得输出。
- 创建两个列列表;然后
List.Zip
将它们组合起来。
- 诀窍在于,对于列表 1,(例如输出列 1),您可能需要在子列列表的顶部添加 Material 列的内容(或替换
-
如果仅此而已;并且,如果是这种情况,请在 CAS
列表的开头添加一个 -
;这样事情就会排在最后。
M代码
let
Source = Excel.CurrentWorkbook(){[Name="Table13"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Material", type text}, {"Sub", type text}, {"CAS", type text}}),
//combine the columns
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each
let
L1 = if [Sub] = "-" then {[Material]}
else List.Combine({{[Material]},Text.Split([Sub],"#(lf)")}),
L2 = if [Sub] = "-" then {[CAS]}
else List.Combine({{"-"},Text.Split([CAS],"#(lf)")})
in
List.Zip({L1,L2})),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Material", "Sub", "CAS"}),
//split the combined columns
#"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns1", "Custom"),
#"Extracted Values" = Table.TransformColumns(#"Expanded Custom",
{"Custom", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values",
"Custom", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Material", "CAS"})
in
#"Split Column by Delimiter"
我希望清理 table 我一直致力于删除复杂的公式并选择使用 PQE 对我的数据进行排序。
下面是我正在处理的数据的虚构示例。
我希望输出列在 material 和子值之间交替,类似于书中的 headers 和子 headers。
M 目前代码:
let
Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Material ", type text}, {"Sub ", type text}, {"CAS", type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Material ", Text.Trim, type text}, {"Sub ", Text.Trim, type text}}),
#"Cleaned Text" = Table.TransformColumns(#"Trimmed Text",{{"Sub ", Text.Clean, type text}}),
#"Replaced Value" = Table.ReplaceValue(#"Cleaned Text","-","",Replacer.ReplaceText,{"Sub "}),
#"Merged Columns" = Table.CombineColumns(#"Replaced Value",{"Material ", "Sub "},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged"),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Merged Columns", {{"Merged", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Merged"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Merged", type text}})
in
#"Changed Type1"
此代码的问题是它在组合时没有正确列出 material,而且我也不知道如何正确排序 CAS 编号。
如果有人对如何实现所需的输出有任何想法,我们将不胜感激。
您可以使用我在上一个类似问题中向您展示的相同技术从您的输入中获得输出。
- 创建两个列列表;然后
List.Zip
将它们组合起来。 - 诀窍在于,对于列表 1,(例如输出列 1),您可能需要在子列列表的顶部添加 Material 列的内容(或替换
-
如果仅此而已;并且,如果是这种情况,请在CAS
列表的开头添加一个-
;这样事情就会排在最后。
M代码
let
Source = Excel.CurrentWorkbook(){[Name="Table13"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Material", type text}, {"Sub", type text}, {"CAS", type text}}),
//combine the columns
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each
let
L1 = if [Sub] = "-" then {[Material]}
else List.Combine({{[Material]},Text.Split([Sub],"#(lf)")}),
L2 = if [Sub] = "-" then {[CAS]}
else List.Combine({{"-"},Text.Split([CAS],"#(lf)")})
in
List.Zip({L1,L2})),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Material", "Sub", "CAS"}),
//split the combined columns
#"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns1", "Custom"),
#"Extracted Values" = Table.TransformColumns(#"Expanded Custom",
{"Custom", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values",
"Custom", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Material", "CAS"})
in
#"Split Column by Delimiter"