Rank With Multiple Criteria

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Hi,

This has been asked a lot but I can't adapt solutions to my needs.

I need to rank by brand the top & bottom variance but only if the sales & plan <> 0.

Sample data below with expected results

BrandSalesPlanVarTop RankBottom Rank
TS7,3746,2871,087212
TS15,84916,798-94986
TM3,0853,549-46436
TM9,49811,716-2,21872
TS3,6664,165-499410
TS5,0266,091-1,06595
TS12,04212,987-94577
TS4,0595,677-1,618113
TM2,2944,121-1,82763
TS14,60315,349-74668
TM000--
TM8421,219-37718
TM000--
TS11,81413,996-2,182122
TS6,9307,996-1,066104
TS000--
TM3,3745,832-2,45881
TS13,09112,184907311
TS41,37540,0401,335113
TM5,8026,408-60645
TM4,3254,702-37727
TM2,0822,830-74854
TS21,47022,101-63159
TS7,11512,438-5,323131

<colgroup><col width="64" span="6" style="width:48pt"> </colgroup><tbody>
</tbody>
 
For extra credit, here is a Power Query version to get the same results.

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Brand", type text}, {"Sales", Int64.Type}, {"Plan", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"Index", "Brand", "Sales", "Plan"}),
    #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Index", "Order"}}),
    #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Var", each [Sales]-[Plan]),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each [Sales]=0 and [Plan] =0),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([Custom] = false)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Brand", Order.Ascending}, {"Var", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Brand"}, {{"Count", each _, type table}, {"Rows", each Table.RowCount(_), type number}}),
    #"Added Custom2" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"Bottom",1,1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom2", "Custom", {"Order", "Sales", "Plan", "Var", "Bottom"}, {"Order", "Sales", "Plan", "Var", "Bottom"}),
    #"Reordered Columns1" = Table.ReorderColumns(#"Expanded Custom",{"Order", "Brand", "Count", "Rows", "Sales", "Plan", "Var", "Bottom"}),
    #"Added Custom3" = Table.AddColumn(#"Reordered Columns1", "Top", each [Rows]-[Bottom]+1),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom3",{"Count", "Rows"}),
    #"Sorted Rows1" = Table.Sort(#"Removed Columns1",{{"Order", Order.Ascending}}),
    #"Reordered Columns2" = Table.ReorderColumns(#"Sorted Rows1",{"Order", "Brand", "Sales", "Plan", "Var", "Top", "Bottom"}),
    #"Removed Columns2" = Table.RemoveColumns(#"Reordered Columns2",{"Order"})
in
    #"Removed Columns2"

BrandSalesPlanVarTopBottom
TS737462871087212
TS1584916798-94986
TM30853549-46436
TM949811716-221872
TS36664165-499410
TS50266091-106595
TS1204212987-94577
TS40595677-1618113
TM22944121-182763
TS1460315349-74668
TM8421219-37727
TS1181413996-2182122
TS69307996-1066104
TM33745832-245881
TS1309112184907311
TS41375400401335113
TM58026408-60645
TM43254702-37718
TM20822830-74854
TS2147022101-63159
TS711512438-5323131

<tbody>
</tbody>


The first step of adding the table to Power Query, I only added Brand, Sales, and Plan. Var is a column that is created in the code.
 
Last edited:
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,216,028
Messages
6,128,399
Members
449,446
Latest member
CodeCybear

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top