Excel - find nth largest value based on criteria - excel

This is a kind of an extension to this problem: Excel - Sum values from the data set based on criteria
I have a table like this:
Country Region Code Name of product Year Value
Sweden Stockholm 52 Apple 1995 1000
Sweden Malmö 25 Pancake 1991 1500
Sweden Malmö 52 Apple 1992 2470
Finland Helsinki 21 Candy 1987 2500
Denmark Copenhagen 52 Apple 1987 2571
What I want to do is to make a code that can give me the sum of the nth largest value of products that have been sold in a specific country.
That is, if I want to get the highest value for products sold in Sweden it should return Apple and the sum of sold apples, 3470.
edit: The solution of Glitch_Doctor:

Firstly, for the value:
Both formulas are array formulas, please confirm the formula with Ctrl+Shift+Enter while still in the formula bar
=MAX(SUMIFS($F$2:$F$6,$A$2:$A$6,$I2,$D$2:$D$6,$D$2:$D$6))
This builds an array of SUMIFS() results for the country in cell $I2 and each product name then grabs the MAX() result.
And the product name:
=INDEX($D$2:$D$6,SMALL(IF(SUMIFS($F$2:$F$6,$A$2:$A$6,$I2,$D$2:$D$6,$D$2:$D$6)=$K2,ROW($D$2:$D$6)-1),1))
Now using the max SUMIFS() result, we reference the list of SUMIFS() results and get the row of the product (offset to the start of the INDEX()) and retrieve the smallest row number.
You can adjust MAX() in the first formula to be LARGE(,n) where n is the nth largest result.

Related

Finding past occurrence

I have a set of stock units transacted (ordered by date from old to new).
I want to find the Previous Quantity that was transacted.
Here is a sample dataset:
Date(A) Type(B) Stock(C) Qty(D) PrevQty(E) CumulativeQty(F)
2016-01-03 Buy MSFT 100 0 100
2016-01-04 Buy GOOG 500 0 500
2016-01-05 Buy MSFT 100 100 200
2016-01-06 Sell MSFT 100 100 100
I can compute CumulativeQty via:
=SUMIFS(D:D,C:C, C4,A:A, "<=" &A4)
Question: How do I figure out PrevQty?
This formula is designed for your cell E3.
=IFERROR(LOOKUP(2,1/($C$2:$C2=$C3),$D$2:$D2),0)
Basically, it looks for the last occurrence of the 'Stock' in C3. However, that last occurrence would naturally by in C3 itself. Therefore the row in which the formula resides is excluded from the range in which the last occurrence is sought.
The presumption is that your first data are in row 2 in your worksheet.
PrevQty
{=index(D:D,match((A4-1)&C4,A:A&C:C,0))}
Update:
For non-sequential dates, to get the value from the same Stock Name, you'll need to do 2 sorting - 1 date from old to new, 2 stock name from A-Z or Z-A
Leave the first (oldest stock) row empty and start the formula from the second oldest row of that stock name. Plus N/A handling.
{=IFERROR(INDEX(D:D,MATCH(MAX(A$3:A3)&C4,A:A&C:C,0)),0)}
Example below: (above formula in cell E4)

Sum Multiple values in different Rows using Vlookup Function

A B
France 152
Italy 255
France 221
Spain 215
USA 222
Spain 155
I desire to add each value occurring in front of repeating countries using Vlookup function
Solution:
A B
France 373
Spain 370
If you use a SUMIF then you can total the columns
If the data starts in cell A1 then in cell C2 type
=SUMIF(A:A,A3,B:B)
then drag the formula down. this will give totals for each country
Or if you just want to show the first instance (where it says France for example) then use
=IF(COUNTIF(A$1:A2,A2)=1,SUMIF(A:A,A2,B:B),"")
You won't be able to do it with a vlookup.
You could use a pivot table which might be easier and that is useful for this exact type of question.
To get the list of repeating countries enter the following formula in Cell D2
=IFERROR(INDEX($A$2:$A$7,MATCH(1,INDEX((COUNTIF($D$1:D1,$A$2:$A$7)=0)*(COUNTIF($A$2:$A$7,$A$2:$A$7)>1),0,0),0)),"")
Then to get sum of corresponding country, in Cell E2 enter
=SUMIF($A$2:$A$7,D2,$B$2:$B$7)
Drag/Copy formulas as required. See image for reference.

Average if cell and and adjacent cell are not NA (Excel)

I am trying to calculate an average value if the rows in two columns are not NA. This is to aid me in calculating an average weight of fish. Here are some example data:
Country,fish weight,fish number
Belgium,264.5,NA
Channel Islands,NA,NA
England,625,281000
Netherlands,737,690000
France,189.5,NA
In this example, the average function would create an average of England and Netherlands weights but not include Belgium and France's weights as the number of fish is not known.
I tried doing this 'averageifs' statement but I can only get it to includes all data.
=AVERAGEIFS(weights,weights,"<>0", numbers,"<>0")
NA is not the same as 0, therefore your formula will see those cells as different from 0 and take them into account. Change your criteria into "<>NA"

Excel Index Partial match

I have Sheet1 with column A listing every single country in alphabetical order..
A
1 Afghanistan<
2 Albania
3 Algeria
4 American Samoa
5 Andorra
----------
228 United Kingdom
229 United States
etc
I have Sheet2 column A with empty cells with adjacent cells in column B listing address details
A B
1 empty cell Unit 3, Road;London, United Kingdom
2 empty cell Building 1, Road, TX, United States
3 empty cell 8th floor, Business Park, India 1234
etc
What I would like to know is how can I obtain the country within the address details in sheet2 column B and place them in Sheet2 column A, based on a match on the list of countries in Sheet1 column A.
Part of the problem is there is no coherent method as to how to country is placed within the address; could be at the end or in the middle of the address.
I have tried various index match formulas with no luck
any help would be appreciated.
I tried it with the reference table being in A1:B7, and lookups being A10:B10 onwards down. The formula is for these cells. You can adjust it for Sheet1/2!.
Assuming your data is in B10 onwards, and your reference data was in B1:B7, you can write this formula in A10 =INDEX($B$1:$B$7,MAX(IF(ISERROR(FIND($B$1:$B$7,B10)),-1,1)*(ROW($B$1:$B$7)-ROW($B$1)+1))). This is an array formula, so please hit Ctrl+Shift+Enter for excel to read it as an array formula.
(In the screenshot, I have pasted the table in A10:B12 as values only in D10:E12)
Text to Columns with a comma delimiter

Excel: Trailing 3 month decline using data from a pivot table

(http://stackoverflow.com/questions/5283466/calculate-moving-average-in-excel seems to be somewhat related. I'm curious to know the non-vba way to do this.)
My my pivot table has source data that creates a count of how many sales a person had in the past 36 days. Each day os 1 column in the pivot table, and the persons name is on the row.
Some of these people did not have sales on day1, day2, etc, and may only have 3 days where they sold something on days 14,15 and 16. No matter what their sequence, I want to to find the most recent sale data (closest to the right edge of pivot table) and calculate three sales increases e.g. C20/C19 will be >1 if they had more sales on the whatever day C20 is. The increase ca nbe fond by subtracting 1 and changing to percent. The problem is, if a person only had sales on d10, d11, d12 then how can I put a general formula in Excel to say "look for the most recent consecutive sales, then calculate this ratio"? For a person who had sales in the past three days, that's easy. It will be chaotic to hardcode where to look for each sales value.
d1 d2 d3 d4...d7 d8 d9...d34 d35 d36 mostrecentincrease nextrecent
ant 1 5 7 7/5=1.4 5/1=5
bat ...10 11 12... 12/11=1.blah 11/10=1.1
cat 2 6 9 13
dog 19 20 20/19=1.blah 19/blank=0
elf 4 4/dnexist=0
i don't have excel here, but i hope this will guide you to the correct result (use , instead of ; if that is your list separator):
define sales as a range of values from d1 to d36 for a given row (or use actual ranges)
compute positions of the last values for each row using these array formulas (use ctrl+shift+enter instead of just enter after you write these formulas):
position_1 =max(if(sales<>0;column(sales)))
position_2 =max(if((sales<>0)*(column(sales)<>position_1);column(sales)))
position_3 =max(if((sales<>0)*(column(sales)<>position_1)*(column(sales)<>position_2);column(sales)))
retrieve the values:
value_1 =index(sales;position_1)
do some error handling (=iferror(...;0)) and the like...

Resources