Extract Largest Values with Conditions

Graemea

Board Regular
Joined
Oct 30, 2015
Messages
116
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have sample data in A:E and would like to extract results to H:K.

I would like to create a formula that will extract the product names in column H. Lookups will return the values in I:J.

The conditions to return the product names are:

The top 5 products in each Region by Sales that have a positive (including zero) Q1 Change value and an associated negative Q2 Change, or vice-versa, value.

So, in Europe, products AAA, BBB and DDD have switched between negative and non-negative values between Q1 and Q2, and DDD has the highest level of Sales, followed by BBB and then AAA (400 vs 200 vs 100)

Can someone please suggest a formula to do this?

Thanks!

Excel3.PNG
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Here is a way using Power Query.

Book1
ABCDEFGHIJK
1PRODUCTREGIONSALESQ1 CHANGEQ2 CHANGEPRODUCTREGIONSALESQ1 CHANGEQ2 CHANGE
2AAAEUROPE100-2.310DDDEUROPE400-1.040.8
3BBBEUROPE200-3.092.31BBBEUROPE200-3.092.31
4CCCEUROPE300-2.91-3.82AAAEUROPE100-2.310
5DDDEUROPE400-1.040.8
6EEEEUROPE5003.683.18
7FFFASIA6004.51-4.42
8GGGASIA700-2.35-1.25
9HHHASIA8004.011.41
10IIIASIA9003.97-0.65
11JJJASIA10000.794.86
Sheet1


Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    TYPES = Table.TransformColumnTypes(Source,{{"PRODUCT", type text}, {"REGION", type text}, {"SALES", Int64.Type}, {"Q1 CHANGE", type number}, {"Q2 CHANGE", type number}}),
    SELECT = Table.SelectRows(TYPES, each _[Q1 CHANGE] <0 and _[Q2 CHANGE] >=0),
    SORT = Table.Sort(SELECT,{{"SALES", Order.Descending}})
in
    SORT
 
Upvote 0
That's great, thank you!

Currently I don't have access to Power Query - does anyone know a way to do this with an (array?) formula please?

Thanks!
 
Upvote 0
Here is another option using formulas. This assumes that you are populating column G (Region) with a region.
MrExcel_20200618.xlsx
ABCDEFGHIJK
1ProductRegionSalesQ1 changeQ2 changeRegionProductSalesQ1 changeQ2 change
2AAAEurope100-2.310AsiaIII9003.97-0.65
3BBBEurope200-3.092.31AsiaFFF6004.51-4.42
4CCCEurope300-2.91-3.82Asia    
5DDDEurope400-1.040.8Asia    
6EEEEurope5003.683.18Asia    
7FFFAsia6004.51-4.42    
8GGGAsia700-2.35-1.25
9HHHAsia8004.011.41
10IIIAsia9003.97-0.65
11JJJAsia10000.794.86
12
Graemea
Cell Formulas
RangeFormula
H2:H7H2=IFERROR(INDEX(A$2:A$11,AGGREGATE(14,6,(ROW($C$2:$C$11)-ROW($C$1))/(($C$2:$C$11=$I2)*($B$2:$B$11=$G2)),1)),"")
I2:I7I2=IFERROR(AGGREGATE(14,6,($C$2:$C$11)/(((SIGN($D$2:$D$11)>=0)<>(SIGN($E$2:$E$11)>=0))*($B$2:$B$11=G2)),ROWS(I$2:I2)),"")
J2:K7J2=IFERROR(INDEX(D$2:D$11,AGGREGATE(14,6,(ROW($C$2:$C$11)-ROW($C$1))/(($C$2:$C$11=$I2)*($B$2:$B$11=$G2)),1)),"")
 
Upvote 0
Another formula option. This will probably run into issues if 2 products have the exact same sales amounts though.

Book1
ABCDEFGHIJKLM
1PRODUCTREGIONSALESQ1 CHANGEQ2 CHANGETFProductRegionSalesQ1 ChangeQ2 Change
2AAAEUROPE100-2.310TRUEDDDEUROPE400-1.040.8
3BBBEUROPE200-3.092.31TRUEBBBEUROPE200-3.092.31
4CCCEUROPE300-2.91-3.82FALSEAAAEUROPE100-2.310
5DDDEUROPE400-1.040.8TRUE     
6EEEEUROPE5003.683.18FALSE   
7FFFASIA6004.51-4.42FALSE
8GGGASIA700-2.35-1.25FALSE
9HHHASIA8004.011.41FALSE
10IIIASIA9003.97-0.65FALSE
11JJJASIA10000.794.86FALSE
Sheet1
Cell Formulas
RangeFormula
I2:I6I2=IF(K2="","",INDEX(Table1[PRODUCT],MATCH($K2,Table1[SALES],0)))
J2:J6J2=IF(K2="","",INDEX(Table1[REGION],MATCH($K2,Table1[SALES],0)))
K2:K6K2=IF(LARGE((Table1[TF]=TRUE)*Table1[SALES],ROW(A1))>0,LARGE((Table1[TF]=TRUE)*Table1[SALES],ROW(A1)),"")
L2:L5L2=IF(K2="","",INDEX(Table1[Q1 CHANGE],MATCH(K2,Table1[SALES],0)))
M2:M5M2=IF(K2="","",INDEX(Table1[Q2 CHANGE],MATCH(L2,Table1[Q1 CHANGE],0)))
F2:F11F2=AND([@[Q1 CHANGE]]<0,[@[Q2 CHANGE]]>=0)
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
Here is another option that relies on some helper cells (see col M:O) to establish a unique list of Regions and the number of results that will satisfy the conditions established. The helper cells are used to construct column G in the results table. This set of formulas finds the largest Sales amounts, even duplicates, and correctly reports matching data for Product and Q1/Q2 changes. The only downside is that it does not sort the Sales figures, but instead reports them from greatest row number to least row number. A separate sort operation could be performed on the table afterward if order is important...a two-level sort...the first level sort on the Region (to keep each region together) and the second-level sort on the Sales.
MrExcel_20200618.xlsx
ABCDEFGHIJKLMNO
1ProductRegionSalesQ1 changeQ2 changeRegionProductSalesQ1 changeQ2 changeRegionsCountCumul Ct
2AAAEurope600-2.310EuropeDDD600-1.040.8Europe33
3BBBEurope800-3.092.31EuropeBBB800-3.092.31Asia47
4CCCEurope300-2.91-3.82EuropeAAA600-2.310   
5DDDEurope600-1.040.8AsiaKKK10004.01-4.86   
6EEEEurope5003.683.18AsiaIII7003.97-0.65   
7FFFAsia8004.51-4.42AsiaHHH6004.01-1.41   
8GGGAsia900-2.35-1.25AsiaFFF8004.51-4.42
9HHHAsia6004.01-1.41     
10IIIAsia7003.97-0.65     
11JJJAsia5000.794.86     
12KKKAsia10004.01-4.86
13LLLAsia7000.794.86
Graemea
Cell Formulas
RangeFormula
G2:G11G2=INDEX($M$2:$M$5,AGGREGATE(15,6,(ROW($O$2:$O$5)-ROW($O$1))/($O$2:$O$5>=ROWS(G$2:G2)),1))
H2:H11H2=IFERROR(INDEX($A$2:$A$13,AGGREGATE(14,6,($B$2:$B$13=$G2)/($B$2:$B$13=$G2)*(ROW($B$2:$B$13)-ROW($B$1))/((SIGN($D$2:$D$13)>=0)<>(SIGN($E$2:$E$13)>=0)),COUNTIF($G$2:$G2,$G2))),"")
I2:I11I2=IFERROR(INDEX($C$2:$C$13,AGGREGATE(14,6,($B$2:$B$13=$G2)/($B$2:$B$13=$G2)*(ROW($B$2:$B$13)-ROW($B$1))/((SIGN($D$2:$D$13)>=0)<>(SIGN($E$2:$E$13)>=0)),COUNTIF($G$2:$G2,$G2))),"")
J2:J11J2=IFERROR(INDEX($D$2:$D$13,AGGREGATE(14,6,($B$2:$B$13=$G2)/($B$2:$B$13=$G2)*(ROW($B$2:$B$13)-ROW($B$1))/((SIGN($D$2:$D$13)>=0)<>(SIGN($E$2:$E$13)>=0)),COUNTIF($G$2:$G2,$G2))),"")
K2:K11K2=IFERROR(INDEX($E$2:$E$13,AGGREGATE(14,6,($B$2:$B$13=$G2)/($B$2:$B$13=$G2)*(ROW($B$2:$B$13)-ROW($B$1))/((SIGN($D$2:$D$13)>=0)<>(SIGN($E$2:$E$13)>=0)),COUNTIF($G$2:$G2,$G2))),"")
M2:M7M2=IFERROR(INDEX(B2:B13,MATCH(0,COUNTIF($M$1:M1,$B$2:$B$13),0)),"")
N2:N7N2=IF(M2="","",MIN(COUNT(($C$2:$C$13)/(((SIGN($D$2:$D$13)>=0)<>(SIGN($E$2:$E$13)>=0))*($B$2:$B$13=M2))),5))
O2:O7O2=IF(M2="","",SUM(N$2:N2))
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

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