Excel: Finding max value based on two criteria, if max value has two identical results - excel

If we have a table of sports teams, that have all played against each other and two teams are on top equal with x points, the winner would be crowned with the highest average goal differential.
But how would you do that with a formula in Excel?
This is the formula I am using to find the team with the highest points total:
=INDEX($C$8:$C$11,MATCH(MAX($K$8:$K$11),$K$8:$K$11,0))
This formula would give me the result of Ecuador being highest (first result of max value).
But in reality Qatar should be on top based on same points total AND average goal difference being higher.
Any solutions?

I assume you want to sort in descending order first by column P, then by column GD. If that is not the case, you can adjust it accordingly. In cell A8 you can put the following formula:
=TAKE(SORT(A3:I6, {9,8}, -1),2,1)
Here is the output:
You can also select the columns of your interest first via CHOOSECOLS then SORT:
=TAKE(SORT(CHOOSECOLS(A3:I6,1,8,9), {3,2}, -1),2,1)
If you want to include the row title, then:
=HSTACK({"First Place";"Second Place"},TAKE(SORT(A3:I6, {9,8}, -1),2,1))
Note: MATCH/XMATCH is not appropriate in this context, because what you really need is to sort the result, not finding values that match certain conditions. You can do it, but at the end you will end up implementing a sort manually and Excel has a built-in function for that. The resulting formula will unnecessary more verbose.
With SORT function you can use more than one criteria indicating in an array the columns to consider as first and second criteria, etc.. For your example it would be: {9,8} indicating as first sorting criteria column 9 and then column 8, so if column P have the same value, then it sorts by GD. The third input argument is to specify ascending (1, default)/descending order(-1). If you want to have a different sorting criteria for each column, then instead of -1, you can use it {-1,1} which means sort in descending order the column 9 and in ascending order the column 8. For your case you can use also: {-1,-1}, but using -1 produce the same result.

Related

How to find the 3 highest values and respective category for a cell

Here is an example of the data I'm trying to organize:
I'm looking for a way to automatically see the top 3 categories (column) for each Name# (row). The size of the category is determined by the number below the category.
Ideally, I'd also like to see a percentage breakdown (from the total) for each category. For example, in row "Name3" 2 categories make up a significantly larger portion of the total values. However, without this percentage breakdown, the 3 top values would seem to be comparable, when they are in fact, not.
Interested to see how this would all work with duplicate numbers, too.
I've tried Excel's rank function, but this doesn't tell me the categories that have the 3 largest sizes, just the 3 highest values.
With Office 365:
=FILTER(SORTBY($B$1:$H$1,B2:H2,-1),SORT(B2:H2,1,-1,TRUE)>=LARGE(B2:H2,3))
And copy down.
If there are ties it will expand the results to include it. It finds the third highest value and returns everything that is equal to or greater than it.
This approach spills all the results at once (array version). In cell J2, you can put the following formula:
=LET(D, A1:H5, A, TAKE(D,,1), DROP(REDUCE("", DROP(A,1), LAMBDA(ac,aa,
VSTACK(ac, TAKE(SORT(DROP(FILTER(D, (A=aa) + (A="")),,1),2,-1,1),1,3)))),1))
It assumes as per input data the cell A1 is empty (if not it can be adjusted accordingly). Here is the output:
An alternative that doesn't require previous assumption (but it is not really a hard one) is the following:
=LET(names, A2:A5, Data, B2:H5, colors, B1:H1, DROP(REDUCE("", names,
LAMBDA(ac,n, VSTACK(ac, TAKE(SORT(VSTACK(colors, INDEX(Data, XMATCH(n,names),0))
,2,-1,TRUE),1,3)))),1))
The non-array version can be obtained from previous approach, and expand it down:
=TAKE(SORT(VSTACK($B$1:$H$1,INDEX($B$2:$H$5, XMATCH(A2,$A$2:$A$5),0)),2,-1,TRUE),1,3)
Explanation
To spill the entire solution it uses DROP/REDUCE/VSTACK pattern. Check my answer to the following question: how to transform a table in Excel from vertical to horizontal but with different length.
For the first formula we filter for a given element of A name (aa) via FILTER the input data (D) to select rows where the name is empty (to consider the header) OR (plus (+) condition) the name is equal to aa. We remove via DROP the first column of the filter result (names column). Next we SORT by the second row (the first rows are the colors) in descending order (-1) by column (last input parameter of SORT we can use TRUE or 1). Finally, we use TAKE to take the first three columns and the first row.
For the second approach, we select the values for a given row (names equals n) and use INDEX to select the entire row (column index 0), then we form an array via VSTACK to add as first row the colors and use the similar logic as in previous approach for sorting and select the corresponding rows and column (colors).
Notes:
If you don't have VSTACK function available, then you can replace it as follow: CHOOSE({1;2}, arr1,arr2) and substitute arr1, arr2, wit the corresponding arrays.
In the second formula instead of INDEX/XMATCH you can use: DROP(FILTER(Data, names=n),,1), it is a matter of personal preference.

Multi-criteria lookup in Excel

I want the code value in column B if the handset matches and the date is between the date from and the date to.
How the results should look:
Since you problem involve Date Range, therefore Vlookup & Index match is not possible to solve, I will use If + AND formula to solve your problem:
=IF(AND(E9>$G$3,E9<$H$3,F9=$E$3),$F$3,
IF(AND(E9>$G$4,E9<$H$4,F9=$E$4),$F$4,
IF(AND(E9>$G$5,E9<$H$5,F9=$E$5),$F$5,
IF(AND(E9>$G$6,E9<$H$6,F9=$E$6),$F$6,""))))
If you are able to add a column, one possible solution without using VBA would be to use a combination of SUMIFS and XLOOKUP formulas.
In column E, add a unique identification number (e.g., 1, 2, 3, 4, 5, etc.). You could reference the row number or manually fill down the numbers or whatever. It wouldn't matter as long as the values are unique and all numbers (no text).
Then, use a SUMIFS formula to sum Column E if (1) the handset number matches, (2) the date is greater than or equal to the start date, and (3) the date is less than or equal to the finish date. As long as there are no overlaps in the date ranges for each handset, this will return the unique ID number. Depending on the dataset, you might have to play with the less/greater than or equal to vs just less/greater than.
=SUMIFS(E:E,A:A,I2,C:C,"<="&H2,D:D,">="&H2)
Then, use the XLOOKUP to return the code based on the unique ID. In this example, I combined the SUMIFS and XLOOKUP in one formula, but you could also do it in two columns to provide better visibility.
=XLOOKUP(SUMIFS(E:E,A:A,I2,C:C,"<="&H2,D:D,">="&H2),E:E,B:B,"NOT FOUND",0,1)
Here's an example.
screenshot

How to rank the position of students based on percentange

I hope you are will be fine.
I am stuck in finding the ranks of students based on percentage.
I have Excel table having multiple columns, concerning columns are Percentage and Position. My data set starts from Row 25 = R25(in excel)
I am using following formula to calculate the percentage of the students based on obtained marks and total marks
=IF(V25="F","",N25/O25)
Where Column V includes pass or fail (in case of fail "F"), column N includes obtained marks and column O includes total marks
It produces the correct output as required:
Now comes to the real problem. Finding the rank/position of student.
I am using RANK.EQ formula to calculate the rank/position of the students based on percentage
=IF(V25="F","Nil",RANK.EQ(P25,[Percentage]))
It also produces the correct output as per formula:
complete pic = >
Now what I want
The two student who obtains 80% marks are ranked as 1 (according to data set)
Now the student who obtains 74% marks should be ranked as 2 instead of 3 and so on
Here is the clear picture of what I require:
I already tried the =SUMPRODUCT(), =MATCH(), =COUNTIF() etc.
Got something I hope you can adapt to your needs. I replied your data this way:
As you can see, formula in column B returns ranking as you wish. To get this,I used an array formula:
=IF(A1="";"";SUM(IF(FREQUENCY(IF($A$1:$A$6>A1;$A$1:$A$6);$A$1:$A$6)>0;1))+1)
NOTE: Because this in an array formula, it must be entered into cell pressing CTRL+ENTER+SHIFT or it
won't work! You will know if you did it properly if your formula shows { at start and } at end.
The trick here is you want to rank your data in descendant order, but excluding duplicates. The array formula first gets an array of values higher than criteria (number in row), and then count how many unique values are in that array. If 0, it means is the highest value, so its rank is 1 (that explains the +1 at end of formula). If count returns 1, means there is an unique value higher than criteria. so its rank is 2 (count of 1 + we add 1 = 2), and so on...
Hope you can adapt this to your needs.
For this I used info shown here:
Count unique values among duplicates

4 variables index function, with great than and less than for 2 variables

I am trying to use index match functions to determine the appropriate rate for the below table.
So for example a consumer loan that is for a person that owns property, the car is 2 years or less in age and the total loan to value ratio is less than 140% should return a value of 5.15%
I believe this is what you wanted...
I would use a series of nested if functions to evaluate which column of LTV I would want the value to come from.
"That is what is done in the AND( ) part. If the value is greater than the 110% and smaller than 140% let's do the Index Match on the 110% Column, Otherwise do it on the 140% Column."
You could extend this for more columns with more IFs in the false condition.
Then it is a simple INDEX match with concatenation. It searches for the three parameters all concatenated in a single range of concatenations.
Hope it helped.
Proof of Concept
In order to achieve the above I had to make a minor edit to your header to be able to distinguish between the two 140% columns.
The functions used in this answer are:
AGGREGATE function
MATCH function
INDEX function
ROW function
IFERROR function
I placed the main part of the formula inside the IFERROR function as a way of dealing with things that may be out of range or when not all the input have been provided. I then assumed that what you were basing your search on would be provided in a series of cells. In my example I assumed the questions would be asked in the range H3 to K3 and I place the results in L3.
The main concept is centered around the INDEX function. I specified the index range as being the height of your table and the width of the percentage rates. Or for this example D2:F9.
=IFERROR(INDEX($D$2:$F$9,row number, column number),"Not Found")
That is the easy part. That more challenging part is determining the row and column number to look in. Lets start with the column number as it is the slightly easier of the two. I assumed the ratio to look for, or rather the header of the column to look in would be supplied. I basically used this equation to determine the column number:
=MATCH(K3,$D$1:$F$1,0)
which in layman's terms is which column between D and F, counting column D as 1, has the value equal to the contents of K3. So now that there is a formula to determine the column, we can drop that into our original formula and wind up with:
=IFERROR(INDEX($D$2:$F$9,row number,MATCH(K3,$D$1:$F$1,0)),"Not Found")
Now we just need to determine the row number. This is the most complex operation. We are going to basically make a bunch of logical checks and take the first row that matches all the logical checks. The premise here is that a logical check is either TRUE or FALSE. In excel 0 is false an every other integer is TRUE. So if we multiply a series of logical checks together, only the one that is true in all cases will be equal to 1. The first logical check is the loan type. it will be followed by the living status and then the vehicle age.
=(H3=$A$2:$A$9)*(I3=$B$2:$B$9)*(J3=C2:C9)
now if you put that into an array formula you will get a series of true false or 1/0. We are going to use it inside an AGGREGATE function with a special feature. The AGGREGATE function will perform array like calculation for some of its functions. We are going to use function 15 which will do this. We are also going to tell the aggregate function to ignore all errors, which is what the 6 does. So in the end what we wind up doing is dividing each row number by the logical check. If the logical check is false or 0, it will generate a Div/0! error which aggregate will choose to ignore. In the end we wind up with a list of row which match our logical check. We then tell the aggregate that we want the first result with the ,1. so we wind up with a formula that looks like:
=AGGREGATE(15,6,ROW($A$2:$A$9)/((H3=$A$2:$A$9)*(I3=$B$2:$B$9)*(J3=C2:C9)),1)
While this does provide us with the row number we want, we need to adjust it to make it an index number. In order to do this you need to subtract the number of header rows. In this case 1. So the index row number is given by this formula:
=AGGREGATE(15,6,ROW($A$2:$A$9)/((H3=$A$2:$A$9)*(I3=$B$2:$B$9)*(J3=C2:C9)),1)-1
And when we substitute that back into the earlier equation for the row number, we wind up with the final equation of:
=IFERROR(INDEX($D$2:$F$9,AGGREGATE(15,6,ROW($A$2:$A$9)/((H3=$A$2:$A$9)*(I3=$B$2:$B$9)*(J3=C2:C9)),1)-1,MATCH(K3,$D$1:$F$1,0)),"Not Found")

How to perform ranking based on several conditions?

Student Total Result GPC
A 398 PASS 1.2
B 341 PASS 1.6
C 396 PASS 1.8
D 402 FAIL 1.6
Let's say I have a table as above and I would like to perform sorting/ranking based on the following conditions:
Sort by "Result" descending
Then sort by "GPC" ascending
Finally sort by "Total" descending
Hence the end result would be like this:
Student Ranking
A 1
B 2
C 3
D 4
How can I do the sorting above in excel? I had tried to use RANK in the formula but it can only cater to one condition.
You can add a number of intermediate columns to calculate a rank order number based on your sort rules
Results
Formulas
Method
Column G: convert PASS/FAIL to a number (other functions require numeric data)
Column H..J: sort the data, a column for each of Total, Result, GPC
Column L..N: Rank the individual results
Column O: Combine individual rankings into single value for final ranking
EDIT
Here's as smaller version of the same method
You've pretty much answered your own, very simple, question. Using Excel's Sort dialog (Data>Sort) do the following:
Sort by "Result" descending
Then sort by "GPC" ascending
Finally sort by "Total" descending
You now have a sorted list. Enter the numbers 1 through whatever starting in row 2 in the column of your choice and your list will be ranked. Based on your question I can't see it being more complicated than that.
I have found the solution from the hint above.
Here what I have done:
1- Insert new column next to the "TOTAL" column.
2- Use IF command for fail students i.e IF("rank"="F",0,value of "Total")
3- Now sort your sheet with adding levels according to your need.
4- Carry on with RANK or IF formula, which ever suits your requirement.

Resources