最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powerbi - Distinguish between two maximum values in a column with Power Query - Stack Overflow

programmeradmin1浏览0评论

I have a column of data in Excel/Power Bi. It has 2 maximum values. I wish to create a second column with the same values as the first column, except add 1 to only one of the maximum values, using power query (M Code). Acknowledge if I only had one maximum value, it would be a relatively simple List.Max/Table.AddColumn exercise. But in this case, I have 2 maximum values. How do I distinguish between the 2 maximum values. See link below for sample data.

I have a column of data in Excel/Power Bi. It has 2 maximum values. I wish to create a second column with the same values as the first column, except add 1 to only one of the maximum values, using power query (M Code). Acknowledge if I only had one maximum value, it would be a relatively simple List.Max/Table.AddColumn exercise. But in this case, I have 2 maximum values. How do I distinguish between the 2 maximum values. See link below for sample data.

Share Improve this question edited Feb 6 at 12:25 davidebacci 30.3k4 gold badges17 silver badges47 bronze badges asked Feb 6 at 10:47 DullesTowerDullesTower 374 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 1

You can do all this from the UI

  • Add an Index Column

  • Add a Custom column with the formula:

    if [Numbers] = List.Max(#"Added Index"[Numbers]) then [Index] else null

  • Add a second Custom column:

    if [Custom] = List.Min(#"Added Custom"[Custom]) then [Numbers] + 1 else [Numbers]

  • Remove the excess columns

M Code

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Numbers", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", 
        each 
            if [Numbers] = List.Max(#"Added Index"[Numbers]) 
                then [Index] else null),
    
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Add 1", 
        each if [Custom] = List.Min(#"Added Custom"[Custom]) 
            then [Numbers] + 1 else [Numbers]),
    
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index", "Custom"})
in
    #"Removed Columns"

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Numbers", Int64.Type}, {"Add 1 to only one of the max values", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each [
        a = List.Max(#"Changed Type"[Numbers]),
        b = List.PositionOf(#"Changed Type"[Numbers],a),
        c = if #"Changed Type"{b} = _ then [Numbers]+1 else [Numbers]
    ][c])
in
    #"Added Custom"
发布评论

评论列表(0)

  1. 暂无评论