Forum Discussion
Ranking by Category in M
Hello,
I am having difficulties with the M and I would like to know if anyone can help me
I have a table that looks like this :
| Index | Sales | Country | Name |
| 1 | 500 | France | X1 |
| 2 | 100 | Belgium | X2 |
| 3 | 120 | France | X3 |
| 4 | 70 | Italia | X4 |
| 5 | 250 | France | X5 |
And I would like to classify it like this by adding a formula in M under Power Query
| Index | Sales | Country | Name | Rank |
| 1 | 500 | France | X1 | 1 |
| 2 | 100 | Belgium | X2 | 1 |
| 3 | 120 | France | X3 | 3 |
| 4 | 70 | Italia | X4 | 1 |
| 5 | 250 | France | X5 | 2 |
I found this on the internet but I couldn't adapt it to my problem (https://blog.crossjoin.co.uk/2015/05/11/nested-calculations-in-power-query/)
Thank you in advance for your help
Paul
Hi LeroyPaul ,
Using below M codes:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI1MACSbkWJecmpQEaEoVKsTrSSEZBpCJZxSs1JzyzNBUkZgaWMQVJGqJqMwTImQKY5SMKzJDEnMxEkYQKWMAUyjUxRtZgqxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Sales = _t, Country = _t, Name = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Sales", Int64.Type}, {"Country", type text}, {"Name", type text}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Country"}, {{"allrows", each _, type table [Index=nullable number, Sales=nullable number, Country=nullable text, Name=nullable text]}}), RankFunction = (tabletorank as table) as table => let SortRows = Table.Sort(tabletorank,{{"Sales", Order.Descending}}), AddIndex = Table.AddIndexColumn(SortRows, "Rank", 1, 1) in AddIndex, Custom1 = Table.TransformColumns(#"Grouped Rows", {"allrows", each RankFunction(_)}), #"Expanded allrows" = Table.ExpandTableColumn(Custom1, "allrows", {"Sales", "Name", "Rank"}, {"allrows.Sales", "allrows.Name", "allrows.Rank"}) in #"Expanded allrows"And you will see:
Check my .pbix file attached for reference.
Best Regards,
KellyDid I answer your question? Mark my reply as a solution!
3 Replies
- LeroyPaul
Helper I
I also found this but is there a way to do it with less steps? https://www.myonlinetraininghub.com/dense-ranking-in-power-query
- Jakinta
Solution Sage
This should help.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI1MACSbkWJecmpQEaEoVKsTrSSEZBpCJZxSs1JzyzNBUkZgaWMQVJGqJqMwTImQKY5SMKzJDEnMxEkYQKWMAUyjUxRtZgqxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Sales = _t, Country = _t, Name = _t]), Grouped = Table.Group(Source, {"Country"}, {{"Gr", each Table.AddIndexColumn(Table.Sort(_,{"Sales" ,Order.Descending}) , "Rank",1,1), type table }}), Removed = Table.RemoveColumns(Grouped,{"Country"}), Expanded = Table.ExpandTableColumn(Removed, "Gr", {"Index", "Sales", "Country", "Name", "Rank"}, {"Index", "Sales", "Country", "Name", "Rank"}), Sorted = Table.Sort(Expanded,{{"Index", Order.Ascending}}) in Sorted
- v-kelly-msft
Community Support
Hi LeroyPaul ,
Using below M codes:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI1MACSbkWJecmpQEaEoVKsTrSSEZBpCJZxSs1JzyzNBUkZgaWMQVJGqJqMwTImQKY5SMKzJDEnMxEkYQKWMAUyjUxRtZgqxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Sales = _t, Country = _t, Name = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Sales", Int64.Type}, {"Country", type text}, {"Name", type text}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Country"}, {{"allrows", each _, type table [Index=nullable number, Sales=nullable number, Country=nullable text, Name=nullable text]}}), RankFunction = (tabletorank as table) as table => let SortRows = Table.Sort(tabletorank,{{"Sales", Order.Descending}}), AddIndex = Table.AddIndexColumn(SortRows, "Rank", 1, 1) in AddIndex, Custom1 = Table.TransformColumns(#"Grouped Rows", {"allrows", each RankFunction(_)}), #"Expanded allrows" = Table.ExpandTableColumn(Custom1, "allrows", {"Sales", "Name", "Rank"}, {"allrows.Sales", "allrows.Name", "allrows.Rank"}) in #"Expanded allrows"And you will see:
Check my .pbix file attached for reference.
Best Regards,
KellyDid I answer your question? Mark my reply as a solution!