【问题标题】:Compare columns return maximum power query比较列返回最大功率查询
【发布时间】:2021-04-18 11:50:05
【问题描述】:

我有来自多个供应商的数据,我想比较这些数据。下图中显示的数据之前已使用 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

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"

【问题讨论】:

    标签: excel comparison office365 powerquery customcolumn


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

    算法

    • 生成要汇总的相关列名列表。
      • 我们从该列表中排除前三列名称
      • 请注意,我们指的是为列名列表添加索引列之前的步骤。如果我们参考上一步添加 Index 列的实际步骤,我们还必须删除 Last 列名称
      • 选择相关列
      • {[Index]} 将返回与索引号对应的记录。
      • 将记录转换为列表并使用List.Max

    【讨论】:

    • 再次感谢。很抱歉很痛苦,但你认为你能告诉我如何转换数据,以便它保留/创建一个列来包含供应商信息吗?尽管您的回答效果很好,但我希望该表格最终会显示文档/来源以及供应商(X、Y、Z 等),以便能够轻松地将每个值追溯到原始数据。如果没有,请不要担心,无论如何,您都非常乐于助人。
    • @Nick 只需使用数据列表中最大数量的位置在列标题列表中查找公司即可。
    猜你喜欢
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 2012-11-29
    • 1970-01-01
    相关资源
    最近更新 更多