extract the row number of a function result - excel

I have a simple table with 5 names and 5 grades if you will.
In another column I order the grades using the LARGE function.
Now is there a way to know the row of each of the "ordered" grades to obtain something like that?
White 23 31 5
Red 15 23 1
Green 23 23 3
Blue 18 18 4
Grey 31 15 2
The column I can't calculate is the last one!

You should use the rank() function if you want to rank these grades. Not large().
=RANK(D2,$D$2:$D$6,1)

You can try this
=MATCH(LARGE(B1:B5,1),B1:B5,0)
The result is a number of row...

In Cell D1 Put =INDEX($A$1:$A$5,MATCH(C1,$B$1:$B$5,0))
Then in Cell D2 put =IF(D1<>INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)+MATCH(C2,INDIRECT("$B$"&MATCH(C2,$B$1:$B$5,0)+1&":$B$5"),0)))
This will also work when duplicate Grades are present
But I Strongly Suggest using Sort as Follows:
*****Also: ***** Here is the explanation on the above Formulas.
To get the Row that contains the Number we are looking for (the number in Column C) you need yo use the Match() Function. We enter =MATCH(C1,B1:B5,0) in D1:
What this is doing: IS looking to the value in C1, this is 31
It is looking in Range("B1:B5"), And 0 is for an Exact match.
So when look for a match to C1 or 31 we get 5. This tells us that 31 is in Row 5
Now, to get the Value of Column A on Row 5 we use INDEX() Function as Follows:
We add to the =MATCH(C1,B1:B5,0) in D1 as =INDEX(A1:A5,MATCH(C1,B1:B5,0))
This will look in Range("A1:A5") for Row 5 (This is because =MATCH(C1,B1:B5,0) = 5)
And the result will be Grey
Now if we drag this formula down we will find the first problem:
Here are our 2 Issues:
1) We get an `N/A` error in the last row.
2) Although `Green` is only in `Range("A1:A5")` one time we see it twice
even though it would seem that `White` should be twice.
These are cause because:
1) We need to add `$` to the range that will remain the same so when we drag down
the formula is won't shift the range. As is the formula in `D5` is
`=INDEX(A5:A9,MATCH(C5,B5:B9,0))` and we receive the error *because*
`Range("A5:A9")` does not contain `15`, but the issue is we meant
to look in `Range("A1:A5")`
So we change the Formula as so: =INDEX($A$1:$A$5,MATCH(C1,$B$1:$B$5,0))
Take note that we do not use the $ on C1 in the formula cause we WANT this value to change as we move down.
But we still have the issue of double values when they shouldn't be there.
Because D1 is the first cell we won't change the formula in it. As anything that is equal to the greatest value is simply tied with it and I don't see any reason why the order of the tie would matter.
Instead we will start in D2 and enter =IF(D1<>INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)+MATCH(C2,INDIRECT("$B$"&MATCH(C2,$B$1:$B$5,0)+1&":$B$5"),0)))
What this is doing is checking if the value of =INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0))
is not equal to the value in the row above. (being a sorted list means all double values would be on top of each other) and If it is NOT the same then use the value, but if it is the same we need to do a little more work.
If the value is not the same we use the Formula INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)+MATCH(C2,INDIRECT("$B$"&MATCH(C2,$B$1:$B$5,0)+1&":$B$5"),0)))
Now to explain it I will use our example of double values. In D3 we find the formula: =IF(D2<>INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)+MATCH(C3,INDIRECT("$B$"&MATCH(C3,$B$1:$B$5,0)+1&":$B$5"),0)))
And because we know that INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)) will be equal to the above cell (White), and we have gone over how the if true works, I will focus on the if false value of: INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)+MATCH(C3,INDIRECT("$B$"&MATCH(C3,$B$1:$B$5,0)+1&":$B$5"),0))
We know MATCH(C3,$B$1:$B$5,0) is the Row that contains the first instance of C3 in this case 23 and the row is Row 1 so we need to look for 23 in the row Under Row 1. So we use MATCH(C3,INDIRECT("$B$"&MATCH(C3,$B$1:$B$5,0)+1&":$B$5"),0) which is equal to MATCH("23", B2:B4,0) because we are adding a 1 to the row that has the 1st match for 23 or C3.
that will now return us the Value of 2 as, the value 23 is in the second row of Range("A2:A5"), Red is in Row 1 and Blue in Row 3 of that range as shown:
but we don't want Row 2 we know that 23 relates to Green and that Green is in Row 3 So we add the row the we last found the value 23 (1 or MATCH(C3,$B$1:$B$5,0))to the row we currently found it (2) and get Row 3.

Here is a formula approach based on the methodology outlined in this link. The final layout of this approach is shown below.
I have assumed that there is 1 header row and I use 2 helper columns (D & E). While additional rows can be added to the header, the table must begin in column A in order for the formulas in column E to work correctly.
Although the helper columns could be eliminated by consolidating their formulas into the formulas in column F, I do not recommend it: the resulting formulas would be a pain to maintain.
Formulas Needed
Cell C2: =LARGE(B:B,ROW(A2)-ROW($A$1)) [Copy down to bottom of data]
Cell D2: =MATCH(C2,B:B,0) [Copy down to bottom of data]
Cell E2: =D2
Cell E3: =IF(D3<>D2,D3,E2+MATCH(C3,INDIRECT("B"&(E2+1)&":B"&COUNTA(A:A)),0))
[Copy down to bottom of data]
Cell F2: =OFFSET($A$2,E2-ROW($A$2),0) [Copy down to bottom of data]
Explanation of Answer
There are four steps to getting the answer:
Sort the grades from highest to lowest (as you showed in your example data)
Create a partial ordering of the row numbers for the sorted grades
Get the row numbers for duplicate grades
Use that ordering to show the name for each sorted grade
Sort the grades from highest to lowest
As you have done, my sort uses the LARGE function, which returns the nth largest value in a range or array. As shown, the LARGE function in cell C2 takes the grades in column B. The "n" for LARGE is calculated as the current row number minus the number of rows in the header, in this case the 1 row for cell A1. When the formula is copied down, "n" progresses from 1 to 2 to 3, etc.
Partially order the grade row numbers
The next step is to determine the row numbers for the unsorted grades that correspond to the sorted grades.
To do that, I use the MATCH function to find where each of the sorted grades lies in the list of unsorted grades in column B. MATCH takes three arguments--the value to be matched, the range in which to make the match, and optionally, the type of match, with a value of 0 or FALSE for an exact match--and returns an index number which represents where in the lookup range the match is found (1 for the first row in the match range, 2 for the second row, etc.).
In the formula for cell D2 shown above, the MATCH function on the grade 31 returns 6 since 31 is in the sixth row of column B.
The result for cell D4 shows why it is only possible to get a partial ordering with this formula. While we are trying to lookup the row for the second instance of a grade of 23, the formula returns a value of 2, which corresponds to the row for the first instance of 23. That's because MATCH will always return the first match for 23 it finds, which is on row 2!
Get correct row numbers for duplicate grades
The next step is to get the correct row references for the duplicated row numbers in column D. The formulas that accomplish this are shown for the first three cells in column E of the table.
There are three cases that have to be dealt with in column E:
For the first (and possibly only) instance of the highest grade, it is possible to just use the row number calculated in cell D2.
The second case deals with the first instances of the row references of the remaining grades. For these the rows numbers calculated in column D can again be used (via the TRUE branch of the IF statement in the column E formulas). For example, in cell E2 -- which corresponds to the first instance of grade 23 -- the row number in cell D3 can be used.
The final case is the rows for duplicate grades. Here, the MATCH for each duplicate in column B is recalculated using a sliding range that excludes the previous matches for that grade. For example, for the duplicated grade of 23 in column C, the match is on the range B3:B6, rather than the range of B2:B6 used in the column D calculation.
Diplay the names in sorted order
This final step is straight forward: Get the name corresponding to the sorted grade. Here the OFFSET function is used; its arguments are a cell reference and the number of rows and columns from that reference that the desired value is to be found.

Related

Using INDEX MATCH function to multiply and sum rows

I've the following table:
In cell A7 I define the index of column 1 I want to use for my computation, in this case A. For every row with index A I want to multiple the value for that row of column 2 with column 3. In this example there are two rows with index A, so I want to do the computation for both rows and then add the results. That will look as follows: 5 * 3 + 9 * 7= 78.
To achieve this, I first tried to write a code that sums all values in column 2 that match a given index. That index is A, so 5 + 9= 14 is what the output should be. I only get my code to find the first match, so that's row 2 and it will display the value of column 2, so that's 5. This is my code for cell B7:
=SUM(INDEX(B2:B5;MATCH(A7;A2:A5;0)))
Even if I solve this I still don't have what I actually want, but I think it's a start. How do I get what I innitially wanted and have the outcome equal 78?
Type in this formula:
=SUM((A2:A5=A7)*(B2:B5)*(C2:C5))
Then Ctrl-Shift-Enter
This converts it into an array formula, which you can identify because there will be braces around it:
{=SUM((A2:A5=A7)*(B2:B5)*(C2:C5))}
Using BYROW()
• Formula used in cell B7
=SUM(BYROW(FILTER(Table12[[Column 2]:[Column 3]],A7=Table12[Column 1]),LAMBDA(m,PRODUCT(m))))
If not using Structured References then
=SUM(BYROW(FILTER(B2:C5,A7=A2:A5),LAMBDA(m,PRODUCT(m))))
Or, use the incredible & versatile SUMPRODUCT() Function
• Formula used in cell C7
=SUMPRODUCT((A7=A2:A5)*(B2:B5)*(C2:C5))

Increment column value when certain conditions are met in Excel

I am trying to create a formula that increments values for 2 columns based on the previous row, the caveat being that it should increment by 1 when a certain value is present and increment by 2 when another value is present.
I have shown the expected output in columns ID and NUMBER.
You can see 3 sets of data that repeat, this is highlighted by the ID column, this value should increment by 1 every 24 rows - 63, 64, 65 - this can be done manually but if there is a formula for this it that would be very useful.
The main part of this question is column NUMBER, when column C changes to 'emr_p_file' the NUMBER column should increment by 2 but when the next row changes to 'L2_*' then it should only increment by 1.
Row 1 values for ID and NUMBER can be static, from row 2 onwards they should be calculated by a formula of some sort.
Is something like this possible in excel, if so please help.
CSV export:
link:https://pastebin.com/zkjsB9L7
The starting ID and NUMBER in cell F1 and G1 will be static, in cell F2 enter the following formula and drag it down:
=F1+(MOD(ROW(1:1),24)=0)
the logic is to use ROW function to return the row number from previous row, and use MOD function to find out if it is fully divisible by 24, if so increase the previous ID by 1 (which is TRUE returned by the equation in the brackets).
in cell G2 enter the following formula and drag it down:
=G1+IF(C2=C1,0,IF(FIND("_",C1)=3,2,1))
the logic is to use nested IF functions to find out if the data set is the same as the previous data set, if so returns 0, if not means there is a change of data set. The next IF will find out if the old data set starts by LR or emr, if former return 1 otherwise 2.
I notice that your example starting Row 49 is NOT increasing the values in Column G: NUMBER as prescribed. Can you please clarify if you provided incorrect expected outcome for those rows or if there is any missing criteria you did not mention in your post?

Lookup one cell in another sheet then return corresponding value

I have two sheets containing various columns in which two columns contains document number and prices. Now I want to match cell K3 (a document number ) of Sheet 1 within column D (all document numbers) of Sheet 2. If match exists, then i want the corresponding price mentioned in Column V to be returned.
This is the formula I've tried:
=VLOOKUP(K3,'Sheet1 (2)'!D2:D896,22,FALSE)
The VLOOKUP documentation is helpful here, especially points 2 and 3 which correspond to parts 2 and 3 of the formula.
The range where the lookup value is located. Remember that the lookup value should always be in the first column in the range for VLOOKUP to work correctly. For example, if your lookup value is in cell C2 then your range should start with C.
The column number in the range that contains the return value. For example, if you specify B2: D11 as the range, you should count B as the first column, C as the second, and so on.
So change the second D to V in your lookup range, and change the column to 19 instead of 22 - D is the 1st column, E the second and so on till V, the 19th column of the lookup range.
=VLOOKUP(K3,'Sheet1 (2)'!D2:V896,19,FALSE)
if VLOOKUP is not your thing, consider INDEX/MATCH.
=INDEX('Sheet1 (2)'!V:V,MATCH(K3,'Sheet1 (2)'!D:D,0))
=VLOOKUP(K3,Sheet1!D3:E8,2,FALSE)
In formula second last argument is the column number of values.

Jump to the next non-empty cell and use lates value in the list

I have the following Excel spreadsheet:
A B Desired Result Column B
1 Product A 50 **50** **50**
2 Product B =IF(A2="","",B1) 50 50
3 =IF(A3="","",B2)
4 Prodcut C =IF(A4="","",B3) 50 **40**
5 =IF(A5="","",B4)
6 ="" =IF(A5="","",B5)
7 Product D =IF(A5="","",B6) 50 40
8 Product E =IF(A5="","",B7) 50 40
** Input of User
In Column A there is a list of different products. As you can see there can either be empty cells or cells with formula ="".
In Column B I want to achieve that the last value before the first empty cell or ="" cell applies to the other rows.
For example: If I enter a 50 in Cell B1 I want to achieve that this 50 appears next to every product and empty cells or ="" are ignored.
I can achieve this with the following formula:
=IF(A2="","",$B$1)
Now the problem is, that the user can also type a different number in another cell in Column B. For example he could type in a 40 in Cell B4.
In this case I want that the 40 applies to all other following rows instead of the 50 as you can see in the section "Desired Result Column B" in the example above.
How do I have to change my formula in Column B to achieve this?
Enter the following formula in Cell B2
=IF(A2<>"",INDEX($B$1:$B1,MAX(IF($B$1:$B1<>"",1,0)*ROW($B$1:$B1))),"")
Drag/Copy down as required.
This is an array formula so commit it by pressing Ctrl+Shift+Enter.
Try:
B2: =IF(A2="","",LOOKUP(2,1/LEN($A$1:A1),$B$1:B1))
With the addressing mode used in the LOOKUP formula, it will examine the rows up to the row before the lookup. If the column A value is not blank, lookup_vector will match the last non-blank cell in column A prior to the row containing the formula, and result_vector will return the value in the same row in column B.
It is rather critical that your user entries are restricted as you have described above.
If the user can make an entry in column B that does NOT correspond to an entry in column A, and you want that entry to be copied down, then use:
=IF(A2="","",LOOKUP(2,1/LEN($B$1:B1),$B$1:B1))
Also, note that the sequence will be upset if the user deletes an entry in column B, instead of replacing it with another number. The formulas below the deletion will interpret the deleted value as a zero.

Excel Index & Match two columns while pulling data for a third

I am trying to create a formula which compares numerical values of two columns and when a match is found return data from a different column.
Here is the problem I am having - the formula I have come up with seems to go Row by Row - for example, if column A & B are the two columns I want to compare, with column C having the data I want if a match is made. If I have a value of 1 in row A1 and a value of 1 in B1 - it will successfuly return the data in column C.
The problem is that my numbers are jumping, the row's do not match, for example column A is 1,2,3 however column B is 1,3,2. The end result here is that I get data for the value 1, but on the mismatch for the second row I get no value.
Basically the formula I made seems to do a hard comparison based just off the two rows of each column - meaning it will only compare A1 to B1. What I really need is it to compare the ENTIRE column and disregard the rows completely
Here is the formula I have been fooling around with - this formula works if A1 and B1 match
=INDEX(M:M,MATCH($L:L,$V:V,0))
In this formula M has the data I need, while columns L and V have numerical values, I need it to not 'hard check' row by row and instead evaluate the entire column and when a match is found return the result (so if both columns have a '2' return that value REGARDLESS of the fact that the '2' may be in rows A2 and B9)
Hopefully I explained my issue well, and I appreciate all the help I can get
EDIT
Sorry for failing to explain it properly on my end -- I will base my explanation off the picture link below.
I need data from column B to show up in column D. What is happening is row 2 matches so the data is successfully retrieved for number 1 - however on row 3 where column A switches the numbering it compared the number '3' to the number '2' and recognizes it is not a match and returns NA -- even though there IS a match in columns A4 and C2 -- in this situation for C3 I would need the data from B4 to showup in D3
http://i.stack.imgur.com/nrKJp.png
Two formulas that will each give you the return you want, Put one of the following in D2 and copy down:
=VLOOKUP(C2,A:B,2,FALSE)
OR
=INDEX(B:B,MATCH(C2,A:A,0))

Resources