Power Query 中哪一列的最大值 table

Which column has a max in Power Query table

有个table:

ID | Value1 | Value2 | Values3
A  |      1 |      2 |       3
B  |      2 |      4 |       3
C  |      3 |      2 |       1
D  |      1 |      2 |       3

是否有一个函数或一组函数返回哪一列具有最大值?

ID | Value1 | Value2 | Values3 | Max_in
A  |      1 |      2 |       3 | Values3
B  |      2 |      4 |       3 | Values2
C  |      3 |      2 |       1 | Values1
D  |      1 |      3 |       3 | Values2
or
D  |      1 |      3 |       3 | Values2;Values3

您可以逆透视和分组以找到最大和最大列名称,然后按如下方式合并回去。这适用于任意数量的列

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Value1", Int64.Type}, {"Value2", Int64.Type}, {"Value3", Int64.Type}}),

// unpivot and get column name of max value
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID"}, "Attribute", "Value"),
#"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"ID"}, {{"Data", each _, type table}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.FirstN(Table.Sort([Data],{{"Value", Order.Descending}}),1)),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Attribute"}, {"Max_in"}),

// merge into original data set
#"Merged Queries" = Table.NestedJoin(#"Changed Type",{"ID"},#"Expanded Custom",{"ID"},"zz",JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "zz", {"Max_in"}, {"Max_in"})
in #"Expanded Table1"

要在不取消透视的情况下执行此操作,请像这样编写自定义列:

= Table.AddColumn(
    #"Changed Type",
    "Max_In",
    (r) =>
        Text.Combine(
            List.Select(
                {"Value1", "Value2", "Value3"},
                each Record.Field(r, _) = List.Max({r[Value1], r[Value2], r[Value3]})
            ),
            ";"
        ),
    type text
)

您可以将完整查询传递到高级编辑器中:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIEYiMgNlaK1YlWcoLyTOAizmAWRNQQLOKCocsVKmIMEYkFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Value1 = _t, Value2 = _t, Value3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value1", Int64.Type}, {"Value2", Int64.Type}, {"Value3", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Max_In", (r) => Text.Combine(List.Select({"Value1", "Value2", "Value3"}, each Record.Field(r, _) = List.Max({r[Value1], r[Value2], r[Value3]})),";"), type text)
in
    #"Added Custom"