比较列return最大功率查询

Compare columns return maximum power query

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

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

谢谢

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

M代码:

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

算法

  • 生成要求和的相关列名列表。
    • 我们从该列表中排除前三列名称
    • 请注意,我们参考了添加列名称列表的索引列之前的步骤。如果我们参考上一步添加索引列的实际步骤,我们还必须删除最后一列名称
    • Select相关栏目
    • {[Index]}会return一个对应Index编号的记录。
    • 将记录转换为列表并使用List.Max