Forum Discussion
Power Query - Create column - Ranking within multiple columns
- 1 year ago
Hi jerryr125 ,
Thank you for reaching out to the Microsoft Community Forum.
As per your inputs, you want to create column, to rank using multiple columns in power query.
Please follow below steps.
1. Created M code with below code in advanced editor.
let
Source = Table.FromRows({
{"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}
}, {"StoreID", "DivisionID", "ProductID", "Qualtity", "Cost"}),
ChangedTypes = Table.TransformColumnTypes(Source,{
{"DivisionID", Int64.Type},
{"ProductID", Int64.Type},
{"Qualtity", Int64.Type},
{"Cost", Int64.Type}
}),
Sorted = Table.Sort(ChangedTypes, {
{"DivisionID", Order.Ascending},
{"ProductID", Order.Ascending},
{"Cost", Order.Ascending}
}),
Grouped = Table.Group(Sorted, {"DivisionID", "ProductID"}, {
{"AllData", each Table.AddIndexColumn(_, "Ranking", 1, 1, Int64.Type)}
}),
Expanded = Table.ExpandTableColumn(Grouped, "AllData", {
"StoreID", "Qualtity", "Cost", "Ranking"
}),
Final = Table.SelectColumns(Expanded, {
"StoreID", "DivisionID", "ProductID", "Qualtity", "Cost", "Ranking"
})
in
Final
2. Please refer below output snap and attached PBIX file.
If my response has resolved your query, please mark it as the "Accepted Solution" to assist others. Additionally, a "Kudos" would be appreciated if you found my response helpful.
Thank you