比较多个表中的数据
Comparing data across multiple tables
背景:
在左下图中,我有 materials A、B...C 等。这些 materials 中的每一个都可以来自 3 个供应商 (1-3),但因为它们各自是不同的供应商material 包含各种杂质(金属)每 material。
Problem/So远:
为了简化,我使用了 power query 将数据拆分为 3 个独立的表,以便数据被划分(如下所示)。
现在,对于每种杂质(金属),我希望比较 3 个表和 return 列表之间的金属,该列表仅包含最高值以及 Table 名称(供应商)如果可以的话。
这被证明是非常具有挑战性的,尽管我确信以前已经实现了类似的分析。本质上,我只是想将多个表中的值与 return 找到的最高值及其来源进行比较。如果任何做过类似事情的人有任何advice/solutions,我们将不胜感激。
假设我们从 powerquery 中的顶部 table 开始,您可以使用此代码转换为第二个图像:
它做一个向上旋转,在换行时拆分,在 space 拆分,然后重命名
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"1", type text}, {"2", type text}, {"3", type text}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Unpivoted Other Columns", {{"Value", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Value"),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Split Column by Delimiter", "Value", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.None, false), {"Metal", "Amount"}),
#"Renamed Columns" = Table.RenameColumns(#"Split Column by Delimiter1",{{"Attribute", "Supplier"}, {"Column1", "Material"}})
in #"Renamed Columns"
从那里您可以添加更多行以根据分组和取最大值生成您想要的 table,然后合并供应商名称
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"1", type text}, {"2", type text}, {"3", type text}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Unpivoted Other Columns", {{"Value", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Value"),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Split Column by Delimiter", "Value", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.None, false), {"Metal", "Amount"}),
#"Renamed Columns" = Table.RenameColumns(#"Split Column by Delimiter1",{{"Attribute", "Supplier"}, {"Column1", "Material"}}),
// generate max table
#"Grouped Rows" = Table.Group(#"Renamed Columns", {"Material", "Metal"}, {{"Highest Amount", each List.Max([Amount]), type text}}),
#"Merged Queries" = Table.NestedJoin(#"Grouped Rows",{"Material", "Metal", "Highest Amount"},#"Renamed Columns" ,{"Material", "Metal", "Amount"},"Table0",JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table0", {"Supplier"}, {"Supplier"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Table1",{{"Highest Amount", type number}})
in #"Changed Type1"
有点不同的 PQ 方法
- 拆分原始 table 为每个项目创建一行,并为金属和数量单独列
- 使用Table.Group函数为每个Material/metal组合提取最高金额和对应的供应商
M代码
let
Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
#"Renamed Columns1" = Table.RenameColumns(Source,{{"Column1", "Material"}}),
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns1",
{{"Material", type text}, {"1", type text}, {"2", type text}, {"3", type text}}),
//Unpivot to create three columns
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Material"}, "Supplier", "Value"),
//Split the Metals column into rows by line feed to => one row per metal
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Unpivoted Other Columns", {
{"Value", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.Csv),
let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Value"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Value", type text}}),
//Split Metals column at the space to => two columns -- one for the name; the other for the amount
#"Split Column by Delimiter1" = Table.SplitColumn(#"Changed Type1", "Value",
Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Metal", "Amount"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Metal", type text}, {"Amount", Int64.Type}}),
//Group rows by Material and Metal
//Extract the highest amount and corresponding suppler
#"Grouped Rows" = Table.Group(#"Changed Type2", {"Material", "Metal"}, {
{"Amount", each List.Max([Amount]),type number},
{"Supplier", (t) => t[Supplier]{List.PositionOf(t[Amount],List.Max(t[Amount]))}, type text}
})
in
#"Grouped Rows"
背景: 在左下图中,我有 materials A、B...C 等。这些 materials 中的每一个都可以来自 3 个供应商 (1-3),但因为它们各自是不同的供应商material 包含各种杂质(金属)每 material。
Problem/So远: 为了简化,我使用了 power query 将数据拆分为 3 个独立的表,以便数据被划分(如下所示)。
现在,对于每种杂质(金属),我希望比较 3 个表和 return 列表之间的金属,该列表仅包含最高值以及 Table 名称(供应商)如果可以的话。
这被证明是非常具有挑战性的,尽管我确信以前已经实现了类似的分析。本质上,我只是想将多个表中的值与 return 找到的最高值及其来源进行比较。如果任何做过类似事情的人有任何advice/solutions,我们将不胜感激。
假设我们从 powerquery 中的顶部 table 开始,您可以使用此代码转换为第二个图像:
它做一个向上旋转,在换行时拆分,在 space 拆分,然后重命名
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"1", type text}, {"2", type text}, {"3", type text}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Unpivoted Other Columns", {{"Value", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Value"),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Split Column by Delimiter", "Value", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.None, false), {"Metal", "Amount"}),
#"Renamed Columns" = Table.RenameColumns(#"Split Column by Delimiter1",{{"Attribute", "Supplier"}, {"Column1", "Material"}})
in #"Renamed Columns"
从那里您可以添加更多行以根据分组和取最大值生成您想要的 table,然后合并供应商名称
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"1", type text}, {"2", type text}, {"3", type text}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Unpivoted Other Columns", {{"Value", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Value"),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Split Column by Delimiter", "Value", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.None, false), {"Metal", "Amount"}),
#"Renamed Columns" = Table.RenameColumns(#"Split Column by Delimiter1",{{"Attribute", "Supplier"}, {"Column1", "Material"}}),
// generate max table
#"Grouped Rows" = Table.Group(#"Renamed Columns", {"Material", "Metal"}, {{"Highest Amount", each List.Max([Amount]), type text}}),
#"Merged Queries" = Table.NestedJoin(#"Grouped Rows",{"Material", "Metal", "Highest Amount"},#"Renamed Columns" ,{"Material", "Metal", "Amount"},"Table0",JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table0", {"Supplier"}, {"Supplier"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Table1",{{"Highest Amount", type number}})
in #"Changed Type1"
有点不同的 PQ 方法
- 拆分原始 table 为每个项目创建一行,并为金属和数量单独列
- 使用Table.Group函数为每个Material/metal组合提取最高金额和对应的供应商
M代码
let
Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
#"Renamed Columns1" = Table.RenameColumns(Source,{{"Column1", "Material"}}),
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns1",
{{"Material", type text}, {"1", type text}, {"2", type text}, {"3", type text}}),
//Unpivot to create three columns
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Material"}, "Supplier", "Value"),
//Split the Metals column into rows by line feed to => one row per metal
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Unpivoted Other Columns", {
{"Value", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.Csv),
let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Value"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Value", type text}}),
//Split Metals column at the space to => two columns -- one for the name; the other for the amount
#"Split Column by Delimiter1" = Table.SplitColumn(#"Changed Type1", "Value",
Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Metal", "Amount"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Metal", type text}, {"Amount", Int64.Type}}),
//Group rows by Material and Metal
//Extract the highest amount and corresponding suppler
#"Grouped Rows" = Table.Group(#"Changed Type2", {"Material", "Metal"}, {
{"Amount", each List.Max([Amount]),type number},
{"Supplier", (t) => t[Supplier]{List.PositionOf(t[Amount],List.Max(t[Amount]))}, type text}
})
in
#"Grouped Rows"