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
tencentgolf
New Member

Filter for lowest n rows by category

Hello,

 

I am trying to figure out how to obtain up to the lowest 4 rows by category on Power Query.  I am trying to use sort, select and firstN but failing miserably. Here is an example:

 

NameR1
Court67
Court68
Court67
Court72
Court85
Tom73
Tom65
Tom66
Mike66
Mike67
Mike66
Mike80
Mike65

 

Where output would be:

 

NameR1
Court67
Court68
Court67
Court72
Tom73
Tom65
Tom66
Mike66
Mike67
Mike66
Mike65

 

Filtered out are:

 

Court

85

Mike80

 

Any thoughts? Thank you in advance.

 

2 ACCEPTED SOLUTIONS
AlB
Super User
Super User

Hi @tencentgolf 

Place the following M code in a blank query to see the steps.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs4vLSpR0lEyM1eK1UHiWqByUWXNjVC4FqZgbkh+LkjOGIljhixjZgbm+GZmp2LyzHHLWRigyAGNjAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, R1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"R1", Int64.Type}}),

    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name"}, {{"R1", each List.FirstN(List.Sort([R1]),4) }}),
    #"Expanded Count" = Table.ExpandListColumn(#"Grouped Rows", "R1")
in
    #"Expanded Count"

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

 

View solution in original post

CNENFRNL
Community Champion
Community Champion

Hi, @tencentgolf , you might want to try

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs4vLSpR0lEyM1eK1UHiWqByUWXNjVC4FqZgbkh+LkjOGIljhixjZgbm+GZmp2LyzHHLWRigyAGNjAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, R1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"R1", Int64.Type}}),

    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name"}, {{"Lowest 4", each List.MinN([R1],4)}}),
    #"Expanded All" = Table.ExpandListColumn(#"Grouped Rows", "Lowest 4")
in
    #"Expanded All"

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

4 REPLIES 4
CNENFRNL
Community Champion
Community Champion

Hi, @tencentgolf , you might want to try

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs4vLSpR0lEyM1eK1UHiWqByUWXNjVC4FqZgbkh+LkjOGIljhixjZgbm+GZmp2LyzHHLWRigyAGNjAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, R1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"R1", Int64.Type}}),

    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name"}, {{"Lowest 4", each List.MinN([R1],4)}}),
    #"Expanded All" = Table.ExpandListColumn(#"Grouped Rows", "Lowest 4")
in
    #"Expanded All"

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

AlB
Super User
Super User

Hi @tencentgolf 

Place the following M code in a blank query to see the steps.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs4vLSpR0lEyM1eK1UHiWqByUWXNjVC4FqZgbkh+LkjOGIljhixjZgbm+GZmp2LyzHHLWRigyAGNjAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, R1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"R1", Int64.Type}}),

    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name"}, {{"R1", each List.FirstN(List.Sort([R1]),4) }}),
    #"Expanded Count" = Table.ExpandListColumn(#"Grouped Rows", "R1")
in
    #"Expanded Count"

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

 

Hi Sorry, what is the Json piece?

The json piece just means they copied your example data into their query to demonstrate how to do it.  Create a new blank query, open the Advanced Editor, and paste their M code over the default text there.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


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