0

我有来自多个供应商的数据,我想比较这些数据。下图中显示的数据之前已使用 power query 通过一系列步骤进行了转换。最后一步是旋转供应商列(在此示例中由 X、Y、Z 组成),以便可以比较这些新列并返回最大值。

在此处输入图像描述

如何比较 X、Y 和 Z 列中的值来执行此操作?重要的是,XY 和 Z 不一定是唯一的供应商。如果我将 Say A 添加为原始数据的新供应商,则会生成一个新列 A,我希望将其包含在比较中,以便在最后的列输出为每一行找到的最高值。因此,从上到下阅读在此示例中将显示为:3,3,1,1,5,0.04,10 等。

谢谢

链接到文件https://onedrive.live.com/?authkey=%21AE_6NgN3hnS6MpA&id=8BA0D02D4869CBCA%21763&cid=8BA0D02D4869CBCA

码:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Material", type text}, {"Residual Solvents", type text}, {"RMQ", type text}, {"COA", type text}, {"Technical Data Sheet", type text}}),
    
    //Replace Time and null with blank
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type","00:00:00","",Replacer.ReplaceText,{"Material", "RMQ", "Residual Solvents", "Technical Data Sheet", "COA"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",null,"",Replacer.ReplaceValue,{"Material", "RMQ", "Residual Solvents", "Technical Data Sheet", "COA"}),
    
    //Trims all whitespace from user
    #"Power Trim" = Table.TransformColumns(#"Replaced Value1",{{"Material", #"PowerTrim", type text}, {"Residual Solvents", #"PowerTrim", type text}, {"RMQ", #"PowerTrim", type text}, {"COA", #"PowerTrim", type text}, {"Technical Data Sheet",#"PowerTrim", type text}}),
  
    //Unpivot to develop a single column of solvent/metals/date data
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Power Trim", {"Material", "Supplier"}, "Attribute", "Value"),

    //split into rows by line feed
    #"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"),
    #"Trimmed Text" = Table.TransformColumns(#"Split Column by Delimiter",{{"Value", Text.Trim, type text}}),

    //filter out the blank rows
    #"Filtered Rows" = Table.SelectRows(#"Trimmed Text", each ([Value] <> "" and [Value] <> "Not Provided")),

    //Add custom column for separating the tables
    #"Added Custom" = Table.AddColumn(#"Filtered Rows", "Custom", each try Date.FromText([Value]) otherwise 
        if [Value] = "Heavy Metals" or [Value] = "Residual Solvents" or [Value] = "Other" then [Value] else null),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", type text}}),
    #"Filled Down" = Table.FillDown(#"Changed Type1",{"Custom"}),

    //Filter the value and custom columns to remove contaminant type from Value column and remove dates from Custom column
    #"Filtered Rows1" = Table.SelectRows(#"Filled Down", each ([Custom] = "Heavy Metals" or [Custom] = "Residual Solvents") and ([Value] <> "Heavy Metals" and [Value] <> "Residual Solvents")),

    //split substance from amount
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Filtered Rows1", "Value", 
        Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, true), {"Substance", "Amount"}),
    //Filter for Solvents Table
    #"Filtered Rows2" = Table.SelectRows(#"Split Column by Delimiter1", each ([Custom] = "Heavy Metals")),
    #"Changed Type2" = Table.TransformColumnTypes(#"Filtered Rows2",{{"Amount", type number}}),
    
    //Group by Material and Substance, then extract the Max contaminant and Source
    #"Grouped Rows" = Table.Group(#"Changed Type2", {"Substance","Material", "Supplier"}, {
        {"Amount", each List.Max([Amount]), type number},
        {"Source", (t) => t[Attribute]{List.PositionOf(t[Amount],List.Max(t[Amount]))}, type text}
        }),
    #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Substance", Order.Ascending}}),


    //PIVOT to compare suppliers
    #"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Supplier]), "Supplier", "Amount", List.Sum)
in
    #"Pivoted Column"
4

1 回答 1

1
  • 添加以零 (0) 开头的索引列。
  • 添加自定义列:
=List.Max(
Record.ToList(
Table.SelectColumns(#"Added Index",
List.RemoveFirstN(
    Table.ColumnNames(#"Pivoted Column"),3)){[Index]}))
  • 然后删除索引列

算法

  • 生成要求和的相关列名列表。
    • 我们从该列表中排除前三个列名
    • 请注意,我们指的是为列名列表添加索引列之前的步骤。如果我们提到我们添加索引列的实际上一步,我们还必须删除 Last 列名称
    • 选择相关列
    • {[Index]}将返回与索引号对应的记录。
    • 将记录转换为列表并使用List.Max
于 2021-04-18T20:20:26.050 回答