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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
vha_labo
Frequent Visitor

Filter out all rows other than the ones with a max value for the corresponding ID

Trying to determine how to return only the rows where the value for that ID number is a maximum.  Example:

 

RowIDValue
113
217
315
4221
5212
6317
739
833
9332
1044
1149

 

In this case, I'd be looking for only rows 2, 4, 9, and 11 to remain when the query is applied.  Any thoughts on how to accomplish would be appreciated.  Thanks.

1 ACCEPTED SOLUTION
edhans
Super User
Super User

Use this forumula:

 

let 
     varID = [ID],
     varValue = [Value]
in 
Table.Max(
    Table.SelectRows(#"Changed Type", each [ID] = varID),
    "Value"
)[Value] = varValue

It will return this table:

2020-05-13 11_43_14-Untitled - Power Query Editor.png

 

Then just filter on the true/false to keep what you want. Full M code below:

1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Nc3BDcAwCAPAXXjnUZOkaWZB2X+NUuM+kHxChgiDNU6308JcWlSXJjUyfXsHOUU4efNGsqpL3NQj1ZP9q5q4Mg8OCTGr5wU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Row = _t, ID = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Row", Int64.Type}, {"ID", Int64.Type}, {"Value", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let 
     varID = [ID],
     varValue = [Value]
in 
Table.Max(
    Table.SelectRows(#"Changed Type", each [ID] = varID),
    "Value"
)[Value] = varValue)
in
    #"Added Custom"


Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

3 REPLIES 3
edhans
Super User
Super User

Use this forumula:

 

let 
     varID = [ID],
     varValue = [Value]
in 
Table.Max(
    Table.SelectRows(#"Changed Type", each [ID] = varID),
    "Value"
)[Value] = varValue

It will return this table:

2020-05-13 11_43_14-Untitled - Power Query Editor.png

 

Then just filter on the true/false to keep what you want. Full M code below:

1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Nc3BDcAwCAPAXXjnUZOkaWZB2X+NUuM+kHxChgiDNU6308JcWlSXJjUyfXsHOUU4efNGsqpL3NQj1ZP9q5q4Mg8OCTGr5wU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Row = _t, ID = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Row", Int64.Type}, {"ID", Int64.Type}, {"Value", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let 
     varID = [ID],
     varValue = [Value]
in 
Table.Max(
    Table.SelectRows(#"Changed Type", each [ID] = varID),
    "Value"
)[Value] = varValue)
in
    #"Added Custom"


Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

With a little tweaking to the process of getting it there, this worked.  Thanks!

Glad I was able to help @vha_labo 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors