Forum Discussion
jerryr125
Helper IV
1 year agoPower Query - Create column - Ranking within multiple columns
Hi - I have the following table and I will like to rank using multiple columns. table-abc StoreID DivisionID ProductID Qualtity Cost A 1 100 5 45 B 1 100 15 55 C 1 ...
- 1 year ago
OwenAuger
Super User
1 year agoHi jerryr125
I would suggest using Table.Group to create nested tables grouped by DivisionID and ProductID with Ranking columns added, then expanding the nested tables.
Something like this:
let
Source = #table(
type table [StoreID = text, DivisionID = Int64.Type, ProductID = Int64.Type, Quality = Int64.Type, Cost = Int64.Type],
{
{"A", 1, 100, 5, 45},
{"B", 1, 100, 15, 55},
{"C", 1, 200, 25, 65},
{"D", 1, 200, 44, 25},
{"E", 1, 200, 36, 15},
{"F", 2, 100, 38, 12},
{"G", 2, 100, 32, 5},
{"H", 2, 100, 65, 8},
{"I", 2, 100, 77, 2}
}
),
TableTypeWithRanking = Value.Type(Table.AddColumn(Source, "Ranking", each null, Int64.Type)),
// Add Ranking column to nested table
#"Nested TableWithRanking" = Table.Group(
Source,
{"DivisionID", "ProductID"},
{{"TableWithRanking", each Table.AddRankColumn(_, "Ranking", {"Cost", Order.Ascending}), TableTypeWithRanking}}
),
#"Final TableWithRanking" = Table.Combine(#"Nested TableWithRanking"[TableWithRanking])
in
#"Final TableWithRanking"
Here's a video showing a similar method:
https://www.youtube.com/watch?v=ysDBHMbtXsk
Does this work for you?