Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
francofava
New Member

MAXIF - remove rows

Hello, I have a table with the following informations, the column unique_id is just the merge of scenario & year:

PeriodAmountscenarioyearunique_id
202001100FCT12020FCT12020
202101150FCT12021FCT12021
202002300FCT12020FCT12020
202103400FCT12021FCT12021
202001250FCT22020FCT22020
202101120FCT22021FCT22021
202002485FCT22020FCT22020
202103695FCT22021FCT22021

 

I would like to identify the rows for each unique_id where the biggest period.

 

PeriodAmountscenarioyearunique_idCheck
202001100FCT12020FCT12020FALSE
202101150FCT12021FCT12021FALSE
202002300FCT12020FCT12020TRUE
202103400FCT12021FCT12021TRUE
202001250FCT22020FCT22020FALSE
202101120FCT22021FCT22021FALSE
202002485FCT22020FCT22020TRUE
202103695FCT22021FCT22021TRUE

 

To end up with the following table

 

PeriodAmountscenarioyearunique_id
202002300FCT12020FCT12020
202103400FCT12021FCT12021
202002485FCT22020FCT22020
202103695FCT22021FCT22021

 

Thanks!

1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

Paste the code below into a blank query.

Read the comments and explore the Applied Steps to understand the algorithm.

 

  • Group by Unique ID
  • Filter each subtable to select the Maximum amount
  • Re-expand the grouped subtables and set the columns back to original order

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMjAwVNJRMjQwAJJuziEgDkgUygMzY3XAKg0hKk1RVRoiVBrCVBoYGAFFjYky0xgoamJAjJlgSZjtRihmGmFxpxGqSkOESjR3mliYEmEmyJ1mlqZ4zIwFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Period = _t, Amount = _t, scenario = _t, year = _t, unique_id = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Period", Int64.Type}, {"Amount", Int64.Type}, {"scenario", type text}, {"year", Int64.Type}, {"unique_id", type text}}),

//group by unique id
//  then Select the row with the maximum Amount
    #"Grouped Rows" = Table.Group(#"Changed Type", {"unique_id"}, {
        {"Amount", (t)=>Table.SelectRows(t, each [Amount]=List.Max(t[Amount])),
            type table[Period=Int64.Type, Amount=number, scenario=text,year=Int64.Type]}
        
        }),

//Expand the subtables
    #"Expanded Amount" = Table.ExpandTableColumn(#"Grouped Rows", "Amount", 
        {"Period", "Amount", "scenario", "year"}, 
        {"Period", "Amount", "scenario", "year"}),

//Return columns to original order
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded Amount",Table.ColumnNames(Source))
in
    #"Reordered Columns"

 

Data

ronrsnfld_0-1646658518388.png

 

Results

ronrsnfld_1-1646658552177.png

 

 

 

View solution in original post

3 REPLIES 3
ronrsnfld
Super User
Super User

Paste the code below into a blank query.

Read the comments and explore the Applied Steps to understand the algorithm.

 

  • Group by Unique ID
  • Filter each subtable to select the Maximum amount
  • Re-expand the grouped subtables and set the columns back to original order

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMjAwVNJRMjQwAJJuziEgDkgUygMzY3XAKg0hKk1RVRoiVBrCVBoYGAFFjYky0xgoamJAjJlgSZjtRihmGmFxpxGqSkOESjR3mliYEmEmyJ1mlqZ4zIwFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Period = _t, Amount = _t, scenario = _t, year = _t, unique_id = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Period", Int64.Type}, {"Amount", Int64.Type}, {"scenario", type text}, {"year", Int64.Type}, {"unique_id", type text}}),

//group by unique id
//  then Select the row with the maximum Amount
    #"Grouped Rows" = Table.Group(#"Changed Type", {"unique_id"}, {
        {"Amount", (t)=>Table.SelectRows(t, each [Amount]=List.Max(t[Amount])),
            type table[Period=Int64.Type, Amount=number, scenario=text,year=Int64.Type]}
        
        }),

//Expand the subtables
    #"Expanded Amount" = Table.ExpandTableColumn(#"Grouped Rows", "Amount", 
        {"Period", "Amount", "scenario", "year"}, 
        {"Period", "Amount", "scenario", "year"}),

//Return columns to original order
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded Amount",Table.ColumnNames(Source))
in
    #"Reordered Columns"

 

Data

ronrsnfld_0-1646658518388.png

 

Results

ronrsnfld_1-1646658552177.png

 

 

 

THanks! 

Anonymous
Not applicable

try this

 

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors