Excel vlookup expansion - excel

I currently have an excel worksheet with three columns
id annotation person_id
1 yes 1
1 no 2
1 yes 3
I'm trying to reformat this on another worksheet into a table that looks like:
id 1 2 3
1 yes no yes
I'm using this
vlookup: =VLOOKUP(A2,sheet2!$F$1:$G$10,2,FALSE)
where a2 is the id and f$1:g$10 is the range of data for that particular person.
At the moment I'm doing this per person and its tedious - there's thousands of people. I need a way to incorporate the person_id into my vlookup so that, if the person_id in the column header matches the person_id in sheet 2 and both ids match, then insert the annotation.

This appears to be a good candidate for an INDEX-MATCH formula, with a multi-condition MATCH() formula to account for both ID criteria. The formula below places the raw data are in A1:C4 and the reformatted data in E1:H2, so you will need to change the references:
In cell F2, I've entered {=INDEX($A$1:$C$4, MATCH(1, ($A$1:$A$4=$E2)*($C$1:$C$4=F$1), 0), 2)}, which yields
[A] [B] [C] [D] [E] [F] [G] [H]
[1] id annotation person_id ID 1 2 3
[2] 1 yes 1 1 yes no yes
[3] 1 no 2
[4] 1 yes 3
The relative references on $E2 and F$1 allow the filling of the formula across rows and columns.
To explain the formula a bit:
In the INDEX() formula, the first argument $A$1:$C$4 is the raw data "table" (array).
The second argument searches for the row where the value ($A$1:$A$4=$E2)*($C$1:$C$4=F$1) is exactly (given by the third argument, 0) equal to 1.
($A$1:$A$4=$E2)*($C$1:$C$4=F$1) is the multiplication of two TRUE/FALSE statements, which is equal to 1 when the main ID in col A is equal to the ID value in E2, and the person ID in col C is equal to the column header in F1.
The final argument, 2, is the column in the data table from which to look up the result.
You may want to consider using another MATCH() formula to dynamically identify the look-up column based on the column header; e.g., instead of 2, use MATCH("annotation", $A$1:$C$1, 0).
Important notes:
The formula needs to be entered as an array formula using Ctrl+Shift+Enter.
This method assumes the combinations of id & person_id are unique.
Also, note that this formula is clearer with named ranges: e.g. (using the suggested second MATCH() function instead of 2), {=INDEX(RawData, MATCH(1, (ID=$E2)*(PersonID=F$1), 0), MATCH("annotation", Headers, 0)}.

You could do like this
First create a new column on the first table =B2&D2
A B C D
1| id annotation person_id
2| 11 1 yes 1
3| 12 1 no 2
4| 13 1 yes 3
And then on the second table use a formula like this
=VLOOKUP(B16&C15;A2:C4;3;FALSE)
B C D F
14 1 2 3
15 1 yes no yes
16 2

Related

Checking if pair of values exists in pair of columns?

I am attempting to find in Excel whether a unique pair of values exists across two columns in a separate spreadsheet. For example:
Array 1
1 - A
2 - B
3 - A
4 - C
Array 2
1 D
2 E
2 B
3 C
I would like the column next to 2, B to return "TRUE", and all other columns to return "FALSE", as 2B exists in Array 2 but none of the other pairds do.
I've tried surfing Stack Overflow for this problem as I felt it would be a very common lookup but surprisingly have had no luck so far. VLookups have given some results but they stop at the first pair. For example let's say we had Array 3:
Array 2
1 D
2 E
2 B
4 C
Now both 2B and 4C in Array 1 should return trues.
Formula in C1:
=COUNTIFS(D:D,A1,E:E,B1)>0
Array one in Column A
Array two in Column B
Paste this XLookup in Column C1, then drag formula down to C4. The corresponding values that match will be listed next to the values from Column A if they exist in Column B.
=XLOOKUP(A1,B$1:B$4,B$1:B$4,"",0,1)

Compare 2 excel columns if their corresponding ID's match

I have 4 excel columns. I want to compare if 2 columns (Type) match if their corresponding IDs match. ID is unique.
Example
ID Type ID Type
1 A 2 B
2 B 3 C
3 C 4 A
4 D 6 F
5 A
Output desired is result column if match found or no.
Which function should I use. I tried vlookup but I don't know how to check only their IDs match.
If these are in columns A through D you could use in Column E:
=if(vlookup(C1, A:B, 2, false)=D1, "Matches", "Doesn't Match")
This will look up the C1 value into the A:B list and see if the corresponding Type matches when the ID's match.

How to use index match with IF in excel?

Table1
A B C D
1 Seq Item Re-Order Qty On-hand Qty
2 1 X 10 15
3 2 Y 10 5
4 3 Z 10 10
Other worksheet:
Table2
Expected output:
A B C
1 Seq Item Re-Order Qty
2 1 N/A N/A
3 2 Y 10
4 3 N/A N/A
In table2 I need to put in column 2 equation like this:
Index(Table1[Item],Match(table2[Seq],tabel1[Seq],0) WHERE table1[reorder qty] > table1[On-hand Qty]
I'm not sure how such requirement could be managed?
This can be done. It requires the use of an array formula in Table2.
Normally with an INDEX you simply use a range of cells as the array (first argument of the formula). In this case, we will give it a new array to return based on the results of a conditional (your WHERE clause).
I will start with the picture of results and then give the formulas. For me, Table1 is on the left, Table2 on the right.
Formulas
The formulas are very similar, the main difference is which column to return in the IF part which generates the array for INDEX. The conditional part of the IF is the same for all columns. Note that using Tables here really helps copying around the formulas since the ranges cannot change under us.
These are all array formulas and need to be entered with CTRL+SHIFT+ENTER.
Table2[Item]
=INDEX(IF(Table1[Re-Order Qty]>Table1[On-hand Qty],Table1[Item],"N/A"), MATCH([#Seq],Table1[Seq],0))
Table2[Re-Order Qty]
=INDEX(IF(Table1[Re-Order Qty]>Table1[On-hand Qty],Table1[Re-Order Qty],"N/A"), MATCH([#Seq],Table1[Seq],0))
Table2[On-hand Qty]
=INDEX(IF(Table1[Re-Order Qty]>Table1[On-hand Qty],Table1[On-hand Qty],"N/A"), MATCH([#Seq],Table1[Seq],0))
The main idea behind these formulas is:
Return a new array based on the conditional. This new array will return the desired column (Item, Re-order, ...) or it will return N/A if the conditional is FALSE. This requires the array formula entry since it is going row by row in the IF.
The MATCH part of the formula to get the row number is "standard". We are simply looking for the Seq number in Table1. This determines which row of the new array to return.

How to format rows to color group by like values in column 1

I have a worksheet that has information like this:
a
a
b
c
c
c
How do I format it so that all of the rows that have a value of a in the first column are one color, then all the rows that have a value of b in the first column are a different color, etc. ?
Edit not from OP to add clarification from comment:
Everything is already sorted alphabetically, and will stay that way, and I want multiple colors.
Create a helper column with a formula like this;
=MOD(IF(A3=A2,0,1)+B2,2)
In this example column A is the column of sorted values to be grouped by, and column B is the helper column. The formula is entered on row 3. Set the first row of the helper column to the value 0 and the others to the formula. This will result in alternating values in the helper column for each group, ie;
a 0
a 0
b 1
c 0
c 0
c 0
d 1
d 1
e 0
You can then set conditional formatting based on the column value. If the value is 1 then highlight the row; if it is 0 do not highlight it. Or use alternating colors or whatever. You can reference any of the articles on the web that describe how to conditional format the entire row based on the value in the column.
IF(A3=A2,0,1) compares the current row (3) and prior row (2) returning a 1 or 0.
MOD( [...] +B2,2) accomplishes the alternation between 0 and 1 when the grouping column value changes.
I think you need a helper column, say B seeded with 1 in row1, and =IF(A1=A2,B1,B1+1) in B2 and copied down to suit. Then formulae of the kind below should suit for conditional formatting:

Merge unique values if another cell matches

Merge all unique values if another cell matches. I already know how to merge cells but now some information is double. So what I would like to achieve is the following:
if column A has the same name, then all values given in column B for
that name must be given only ONCE in a new column.
My data has a row names and a row mode, for example (Row 1 is header)
A B
2 Brenda a
3 Brenda a
4 Joey a
5 Joey b
So I want:
E
2 a
3
4 a,b
5
I already did merge the modes in column 3:
=IF(A1<>A2;B2;C1&","&B2)
So I get in this example:
C
2 a
3 a,a
4 a
5 a,b
Then, I already did that only the first record get the additional modes in column 4:
=IF(A1=A2;"";INDEX(Sheet1!$C:$C;COUNTIF(Sheet1!$A:$A;$A2)+MATCH($A2;Sheet1!$A:$A;0) -1))
So I get in this example
D
2 a,a
3
4 a,b
5
Now I need a column that only uniques values are given for each name. So in this example:
E
2 a
3
4 a,b
5
If I am understanding how your data is structured, try this:
Add a new column, say column G for ease of explanation, that concatenates the name and mode in each row. So, cell G2="Brendaa", G3="Brendaa", G4="Joeya", G5="Joeyb", etc.
In your merge step you will test whether the current value in the cell for this column matches any previous values in the column: If no, you do the merge; if yes, you don't.
Your merge formula would change to something like the following:
=IF(A1<>A2,B2,IF(ISERROR(VLOOKUP(G2,G$1:G1,1,0)),C1&","&B2,""))
Then you would the next step as before.

Resources