Forum Discussion

ASG25's avatar
ASG25
Regular Visitor
2 years ago
Solved

How to add two separate Rank Column based on two criteria?

Hi,
In below table, I need value base ranking by country & cust state wise in power query... 

Customer No.Customer NameProduct CodeProduct DescriptionValueCountyCust StateCountry RankState Rank
98717232Abishek Enterprises3457652IMBUSH 10x4X250 ML238339INMP  
98742618NATARAJA STORES3457144CARATE 4x25X100 ML 2034283INMP  
98742618NATARAJA STORES3493457FANTIS 10X1 L     324930INMP  
98743861BALAJI TRADER3437662CARATE 2x5X1 L     349834INGJ  
98743861BALAJI TRADER3457646IMBUSH 5x2X1 L    3487INGJ  
98718113Meenashi Services3459175SIFLEX 25X400 ML 346837INGJ  
98735175BRUNA Co.Pvt. Ltd3469469AMISTAR TOP 20X500 ML56089INTG  
98735178amarapuri Enterprises3457361ABION 40X250 ML  349837INTG  
98735178amarapuri Enterprises3457362ABION 20X500 ML  546089INTG  
98735178amarapuri Enterprises3457363ABION 10X1 L     2465322INTG  
98735178amarapuri Enterprises3457653IMBUSH 25x4X100 ML3095867INTG  
98744355PAVITHRA AGENCY3457483ARAIFIT 50 EC 20x500 ML4985495INTG  
98744355PAVITHRA AGENCY3457579ABION 2X5 L      348734INTG  

 

I have tried for solution by Grouping option in power query result shows only country ranking.

 

Just for Example:- below First 4 Column is summarized with Pivot & then last 2 column added manually

Customer No.Customer NameCust State ValueCountry RankState Rank
98735178amarapuri EnterprisesTG645711511
98744355PAVITHRA AGENCYTG533422922
98742618NATARAJA STORESMP235921331
98743861BALAJI TRADERGJ35332141
98718113Meenashi ServicesGJ34683752
98717232Abishek EnterprisesMP23833962
98735175BRUNA Co.Pvt. LtdTG5608973




 

  • Hi ASG25, it is also possible in Power Query, but it is not the fastest one...

     

    Result:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZNba9tAEIX/yuLnEPYys1o9rl3ZWSPJRtoElZAHtxFEhKZBdoN+fmcVXRJiKEmFEMMM+zE65+zt7SI2kYikkouLhf3RHB/qR5Y8ner2uW2O9ZG6CjDSGOYuW16XV0zwDiqJnGUpNaUySsVhmtMn2y/uLnooSC0MdXLrbWG3lpV+VyTlABQAVK1o4hMGncRK8ABkgcgVSKM+h4wDlaq1zb0racdKsJSFJ4wlzfkZoDJaUGdpU7t1zBf2W1L0OBVpLecNZYfveBAbBSNvs/03jyQEPUuInRx5/dhEH2HCCBFEyOr66XB8aFhZty/Nz9GTWERIVenWaVIxEhAmARVoo84QFb6eWRbXuWWr35f7l9MlS0/3r2diekMKMleSvszv9kzyCvlgNGpuJp/95i00mHL4dWgPz3/a5kx8VK+KXbpdzoAP2ZmVjL5KlRN1WrSnIvzPrmqivk+RBI1Kyq9h6ehsv0S6QWIUVvEYjT4jAoDC4Nfe3jh/VVhmN0m++j4Qob8ilE+3dp6RpMmKZOgmv0hZhBg/i8UonlWtcPj9MaZz6gPv7i8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer No." = _t, #"Customer Name" = _t, #"Product Code" = _t, #"Product Description" = _t, Value = _t, Country = _t, #"Cust State" = _t]),
        ChangedType = Table.TransformColumnTypes(Source,{{"Value", type number}}),
        GroupedRows1 = Table.Group(ChangedType, {"Customer No.", "Customer Name", "Country", "Cust State"}, {{"Value", each List.Sum([Value]), type number}}),
        // Added State Rank inside [All] tables.
        GroupedRows2 = Table.Group(GroupedRows1, {"Country", "Cust State"}, {{"All", each Table.AddIndexColumn(Table.Sort(_, {{"Value", Order.Descending}}), "State Rank", 1, 1, Int64.Type), type table}}),
        CombinedAll1 = Table.Combine(GroupedRows2[All]),
        // Added Country Rank inside [All] tables.
        GroupedRows3 = Table.Group(CombinedAll1, {"Country"}, {{"All", each Table.AddIndexColumn(Table.Sort(_, {{"Value", Order.Descending}}), "Country Rank", 1, 1, Int64.Type), type table}}),
        CombinedAll2 = Table.Combine(GroupedRows3[All])
    in
        CombinedAll2

2 Replies

  • dufoq3's avatar
    dufoq3
    Community Champion

    Hi ASG25, it is also possible in Power Query, but it is not the fastest one...

     

    Result:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZNba9tAEIX/yuLnEPYys1o9rl3ZWSPJRtoElZAHtxFEhKZBdoN+fmcVXRJiKEmFEMMM+zE65+zt7SI2kYikkouLhf3RHB/qR5Y8ner2uW2O9ZG6CjDSGOYuW16XV0zwDiqJnGUpNaUySsVhmtMn2y/uLnooSC0MdXLrbWG3lpV+VyTlABQAVK1o4hMGncRK8ABkgcgVSKM+h4wDlaq1zb0racdKsJSFJ4wlzfkZoDJaUGdpU7t1zBf2W1L0OBVpLecNZYfveBAbBSNvs/03jyQEPUuInRx5/dhEH2HCCBFEyOr66XB8aFhZty/Nz9GTWERIVenWaVIxEhAmARVoo84QFb6eWRbXuWWr35f7l9MlS0/3r2diekMKMleSvszv9kzyCvlgNGpuJp/95i00mHL4dWgPz3/a5kx8VK+KXbpdzoAP2ZmVjL5KlRN1WrSnIvzPrmqivk+RBI1Kyq9h6ehsv0S6QWIUVvEYjT4jAoDC4Nfe3jh/VVhmN0m++j4Qob8ilE+3dp6RpMmKZOgmv0hZhBg/i8UonlWtcPj9MaZz6gPv7i8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer No." = _t, #"Customer Name" = _t, #"Product Code" = _t, #"Product Description" = _t, Value = _t, Country = _t, #"Cust State" = _t]),
        ChangedType = Table.TransformColumnTypes(Source,{{"Value", type number}}),
        GroupedRows1 = Table.Group(ChangedType, {"Customer No.", "Customer Name", "Country", "Cust State"}, {{"Value", each List.Sum([Value]), type number}}),
        // Added State Rank inside [All] tables.
        GroupedRows2 = Table.Group(GroupedRows1, {"Country", "Cust State"}, {{"All", each Table.AddIndexColumn(Table.Sort(_, {{"Value", Order.Descending}}), "State Rank", 1, 1, Int64.Type), type table}}),
        CombinedAll1 = Table.Combine(GroupedRows2[All]),
        // Added Country Rank inside [All] tables.
        GroupedRows3 = Table.Group(CombinedAll1, {"Country"}, {{"All", each Table.AddIndexColumn(Table.Sort(_, {{"Value", Order.Descending}}), "Country Rank", 1, 1, Int64.Type), type table}}),
        CombinedAll2 = Table.Combine(GroupedRows3[All])
    in
        CombinedAll2