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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
patoduck
Helper III
Helper III

Rank over partition multiple columns in Power Query NOT DAX!

I have the following table 

documenttopicgamma
110.2890625
120.2578125
130.2265625
140.2265625
210.2358491
220.2547170
230.2358491
240.273584

 

And I need to return  only the topic with the highest gamma per document so it will look like this

documenttopicgamma
110.2890625
240.273584

 

In case of ties really doesnt matter, so it will be like a row_number over partition in SQL.

@ImkeFpiece of cake? 🙂

 

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hi @patoduck ,

yes, please try the following code (paste into the advanced editor and check what it does):

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAWMDPSMLSwMzI1OlWB2IqBFE1NTcwhBJ1BgiamRmiqzWBEPUCGGusamFiaUhXBRmrom5oTlc0BirUqix5iBhpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [document = _t, topic = _t, gamma = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"document", Int64.Type}, {"topic", Int64.Type}, {"gamma", type number}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"document"}, {{"All", each Table.SelectRows(_, (x) => (x[gamma] = List.Max(_[gamma]))){0}}}),
    #"Expanded All" = Table.ExpandRecordColumn(#"Grouped Rows", "All", {"topic", "gamma"}, {"topic", "gamma"})
in
    #"Expanded All"

If your raw data has all rows for one document in a sequence, you can even use GroupKind.Local to speed up the calculation considerably:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAWMDPSMLSwMzI1OlWB2IqBFE1NTcwhBJ1BgiamRmiqzWBEPUCGGusamFiaUhXBRmrom5oTlc0BirUqix5iBhpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [document = _t, topic = _t, gamma = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"document", Int64.Type}, {"topic", Int64.Type}, {"gamma", type number}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"document"}, {{"All", each Table.SelectRows(_, (x) => (x[gamma] = List.Max(_[gamma]))){0}}}, GroupKind.Local),
    #"Expanded All" = Table.ExpandRecordColumn(#"Grouped Rows", "All", {"topic", "gamma"}, {"topic", "gamma"})
in
    #"Expanded All"

But this only works if they are all nicely in one sequence each.

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

6 REPLIES 6
ImkeF
Super User
Super User

Hi @patoduck ,

yes, please try the following code (paste into the advanced editor and check what it does):

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAWMDPSMLSwMzI1OlWB2IqBFE1NTcwhBJ1BgiamRmiqzWBEPUCGGusamFiaUhXBRmrom5oTlc0BirUqix5iBhpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [document = _t, topic = _t, gamma = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"document", Int64.Type}, {"topic", Int64.Type}, {"gamma", type number}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"document"}, {{"All", each Table.SelectRows(_, (x) => (x[gamma] = List.Max(_[gamma]))){0}}}),
    #"Expanded All" = Table.ExpandRecordColumn(#"Grouped Rows", "All", {"topic", "gamma"}, {"topic", "gamma"})
in
    #"Expanded All"

If your raw data has all rows for one document in a sequence, you can even use GroupKind.Local to speed up the calculation considerably:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAWMDPSMLSwMzI1OlWB2IqBFE1NTcwhBJ1BgiamRmiqzWBEPUCGGusamFiaUhXBRmrom5oTlc0BirUqix5iBhpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [document = _t, topic = _t, gamma = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"document", Int64.Type}, {"topic", Int64.Type}, {"gamma", type number}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"document"}, {{"All", each Table.SelectRows(_, (x) => (x[gamma] = List.Max(_[gamma]))){0}}}, GroupKind.Local),
    #"Expanded All" = Table.ExpandRecordColumn(#"Grouped Rows", "All", {"topic", "gamma"}, {"topic", "gamma"})
in
    #"Expanded All"

But this only works if they are all nicely in one sequence each.

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Hi,

Will you be displaying this data on report or you want it as a table in the report?

Thanks.

It will be a dimension in the data model.
Anonymous
Not applicable

Hi,

Try using the below DAX to create the new table from the existing table.

In the below example 'Data' is the name of my existing table and 'Filtered Data' is the new derived dimension.

Filtered Data = 
FILTER(Data, 
RANKX(
        FILTER(
                Data,
                Data[document]=EARLIER(Data[document])
                ),
        Data[gamma]
        ) == 1 )

Thanks.

Thanks, but I need a Power Query, ETL side solution, not DAX as this will affect performance, table contains a large number of rows.

Just to clarify DAX and M language (Power Query) are different languages , therefore the title in my question.


@Anonymous wrote:

Hi,

Try using the below DAX to create the new table from the existing table.

In the below example 'Data' is the name of my existing table and 'Filtered Data' is the new derived dimension.

Filtered Data = 
FILTER(Data, 
RANKX(
        FILTER(
                Data,
                Data[document]=EARLIER(Data[document])
                ),
        Data[gamma]
        ) == 1 )

Thanks.



@Anonymous wrote:

Hi,

Try using the below DAX to create the new table from the existing table.

In the below example 'Data' is the name of my existing table and 'Filtered Data' is the new derived dimension.

Filtered Data = 
FILTER(Data, 
RANKX(
        FILTER(
                Data,
                Data[document]=EARLIER(Data[document])
                ),
        Data[gamma]
        ) == 1 )

Thanks.


 

Anonymous
Not applicable

Oh.Apologies. I missed it.

Can you check if the below video is what you need?

https://www.youtube.com/watch?v=Y7paK0yS5ic

Thanks.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.