2D Lookup — INDEX and MATCH or XLOOKUP functions

How to do a 2D Lookup in Excel? Let’s start.

2D Lookup | Excel Tips 68

When we want to find an inter­sec­tion between a row and a col­umn bas­ing on head­ers, we can do it in two dif­fer­ent ways. The first one will be in Lega­cy Excel, and the sec­ond in Dynam­ic Array Excel.

The first solu­tion uses the MATCH func­tion as well as its close friend, the INDEX function. 

Let’s start with look­ing for the coun­try. In this case we want to find the row num­ber. Let’s write the cell, the range where we will be look­ing for and 0 for an exact match (Fig. 1)

=MATCH(B8,A3:A6,0)

MATCH function for countries
Fig. 1 MATCH func­tion for countries

We also want to find the col­umn num­ber. In this case we also use the MATCH func­tion. Let’s the cell, the range where we will be look­ing for and 0 for an exact match (Fig. 2)

=MATCH(B9,B2:G2,0)

MATCH function for months
Fig. 2 MATCH func­tion for months

When we have our row and col­umn num­bers, we can use the INDEX func­tion, select the whole dataset, then write the row and col­umn num­bers (Fig. 3)

=INDEX(B3:G6,D8,D9)

INDEX function
Fig. 3 INDEX function

Just like that, we did the 2D lookup, or we have found the val­ues we want­ed (Fig. 4)

Results
Fig. 4 Results

So far, we have had three for­mu­las. If we want to have only one, we need to imple­ment some mod­i­fi­ca­tions. We have to copy our MATCH func­tion and paste it in the place refer­ring to cell D8. Then, we have to copy the sec­ond MATCH func­tion with­out the equal sign, and paste it in the place of the ref­er­ence to cell D9. Now, we have one, big­ger for­mu­la instead of three small­er ones (Fig. 5)

=INDEX(B3:G6,MATCH(B8,A3:A6,0),MATCH(B9,B2:G2,0))

One, big formula
Fig. 5 One, big formula

Now, let’s focus on the solu­tion in the Array Excel. We will be using here the XLOOKUP func­tion twice. 

First, we are look­ing for a row in the coun­try head­ers. Then, as a return­ing array, we give Excel the whole dataset (Fig. 6)

=XLOOKUP(B8,A3:A6,B3:G6)

 XLOOKUP function
Fig. 6 XLOOKUP function

Since I’m using the XLOOKUP func­tion, and we are work­ing in the Array Excel, Excel will return the whole row. 

In such a case, we have to find a prop­er col­umn. We can also do it with the XLOOKUP func­tion. Let’s write the XLOOKUP func­tion once again. This time we will be look­ing for a month in the month head­ers. As a return array, we have the row we found pre­vi­ous­ly (Fig. 7)

=XLOOKUP(B9,B2:G2,XLOOKUP(B8,A3:A6,B3:G6))

Another XLOOKUP function
Fig. 7 Anoth­er XLOOKUP function 

And just like that, we have our results (Fig. 8)

Results
Fig. 8 Results

Even if we change the val­ues in our for­mu­la, the result is still cor­rect (Fig. 9)

Checking the formula
Fig. 9 Check­ing the formula

The results are cor­rect both in XLOOKUPs and INDEX with MATCH functions. 

https://www.youtube.com/watch?v=fZqiVB3Vffw

Compare 2 lists — Legacy Excel classic formulas

Today, we want to com­pare two lists and find ele­ments that are on both lists as well as on only one. 

Com­pare 2 lists clas­sic for­mu­las Lega­cy Excel

We are going to use Clas­sic Excel, Lega­cy Excel and a clas­sic for­mu­la with the AGGREGATE func­tion. It means that this for­mu­la will work from 2010 and thanks to this, we won’t need to use the Crtl + Shift + Enter key combination.

Let’s start with find­ing ele­ments. We will be check­ing whether ele­ments from My son’s favorite movies list are on My daugh­ter’s favorite movies list. We can use the MATCH func­tion here. We are look­ing up ele­ments from my son’s list (list B) on my daugh­ter’s list (list A). We’re click­ing F4 key to lock both lists. Then we write zero to have the exact match and that’s it (Fig. 1)

=MATCH($C$2:$C$9,$A$2:$A$11,0)

MATCH function
Fig. 1 MATCH function

Excel has just giv­en us the results. I have the Dynam­ic Array Excel, which means that Excel spilled the results. We can see that How to train a Drag­on is in list B with num­ber 5 which means that it’s on posi­tion num­ber 5 in list A. We also have Spi­der-man on posi­tion num­ber 7. If we want to extract those num­bers, we have to use the AGGREGATE func­tion. In the first argu­ment of the func­tion, we have to use a func­tion num­ber 15, as this func­tion under­stands an array for­mu­la. Then, in the sec­ond argu­ment, we have to write 6 to ignore errors. At the end, in the k argu­ment let’s write ROWS from E2 to E2 and press F4 key to lock only the first one. This will allow the for­mu­la to expand while copy­ing down (Fig. 2)

=AGGREGATE(15,6,MATCH($C$2:$C$9,$A$2:$A$11,0),ROWS($E$2,E2))

AGGREGATE function
Fig. 2 AGGREGATE function

Now, we have the results, how­ev­er there are some errors. To remove that, we can add the INDEX func­tion and write list A in the first argu­ment, then press F4 key to lock it (Fig. 3)

=INDEX($A$11:$A$11,AGGREGATE(15,6,MATCH($C$2:$C$9,$A$2:$A$11,0),ROWS($E$2,E2))

INDEX function
Fig. 3 INDEX function

Now, we have the ele­ments that are on both lists. We can remove the errors. Let’s add the IFERROR func­tion (Fig. 4)

=IFERROR(INDEX($A$11:$A$11,AGGREGATE(15,6,MATCH($C$2:$C$9,$A$2:$A$11,0),ROWS($E$2,E2))),“”)

IFERROR function
Fig. 4 IFERROR function

We have our results in the tar­get list, and there is still some back­up space in case we have some more results (Fig. 5)

Results with extra space
Fig. 5 Results with extra space

Now, we can cre­ate a for­mu­la that will look up ele­ments only from list B. Let’s start with giv­ing each ele­ment a num­ber. We’ll use the ROW func­tion here and write the whole list as the first argu­ment. Then F4 key to lock it (Fig. 6)

=ROW($C$2:$C$9)

ROW function
Fig. 6 ROW function

As we can see, Excel has spilled the results and each ele­ment has got its own num­ber. How­ev­er, we want to start from 1, not from 2 (Fig. 7)

Numbers for each element
Fig. 7 Num­bers for each element

To do this, we have to sub­tract the head­er row and press F4 key to lock it (Fig. 8)

=ROW($C$2:$C$9)-ROW($C$1)

Subtracting the header row
Fig. 8 Sub­tract­ing the head­er row

And we have all ele­ments num­bered from 1 to 8. Now, we want only num­bers of ele­ments that aren’t on list A. How can we do it? We can divide our list by the MATCH func­tion. In the first argu­ment, we write list B and press F4 key to lock it, then list A in the sec­ond argu­ment, as that’s the place where we will be look­ing up our val­ues. Then F4 key. As the final step, we write 0, as we want to have the exact match and the look­ing up process will start from the top (Fig. 9)

=(ROW($C$2:$C$9)-ROW($C$1))/MATCH($C$2:$C$9,$A$2:$A$11,0)

Dividing the list
Fig. 9 Divid­ing the list

Just like that we have num­bers only on those posi­tions that are on both lists. How­ev­er, we want a reverse sit­u­a­tion, where we will have num­bers in #N/A posi­tions. To to that we have to put our MATCH func­tion into the ISNA func­tion (Fig. 10)

=(ROW($C$2:$C$9)-ROW($C$1))/ISNA(MATCH($C$2:$C$9,$A$2:$A$11,0))

ISNA function
Fig. 10 ISNA function

If the MATCH func­tion returns an error, the ISNA func­tion will change this error into TRUE. And a num­ber divid­ed by the val­ue of TRUE will change the TRUE val­ue into 1. If the ISNA func­tion returns FALSE, the math­e­mat­i­cal oper­a­tion will change FALSE into 0. Since we can­not divide by 0, we will have #DIV/0! errors (Fig. 11)

#DIV/0! errors
Fig. 11 #DIV/0! errors

Since we have a list with num­bers which are the posi­tions of ele­ments we want to extract and errors, it’s time to put our whole for­mu­la into the AGGREGATE func­tion. Just like before, we start from the small­est val­ue, so let’s write 15, then 6, as we want to ignore errors. At the end of our func­tion, we write the ROWS func­tion with G2 cell. Then F4 key to lock the first one (Fig. 12)

=AGGREGATE(15,6,(ROW($C$2:$C$9)-ROW($C$1))/ISNA(MATCH($C$2:$C$9,$A$2:$A$11,0)),ROWS($G$2:G2))

 AGGREGATE function
Fig. 12 AGGREGATE function

Now, we have posi­tions with­out errors, which means that we can add the INDEX func­tion to our for­mu­la. As we take posi­tions from list B, we have to select the list, then press F4 key to lock it (Fig. 13)

INDEX function
Fig. 13 INDEX function

After press­ing Enter, or Ctrl + Enter, we have our results. Now, we can add the IFERROR func­tion to put emp­ty text strings in the places where the for­mu­la returns errors (Fig. 14)

=IFERROR(AGGREGATE(15,6,(ROW($C$2:$C$9)-ROW($C$1))/ISNA(MATCH($C$2:$C$9,$A$2:$A$11,0)),ROWS($G$2:G2)),“”)

IFERROR function
Fig. 14 IFERROR function

And we have our results (Fig. 15)

Results
Fig. 15 Results

This way, we have two for­mu­las. The first one to extract ele­ments that are on both lists, and the sec­ond one to extract ele­ments that are only on one list. 

https://www.youtube.com/watch?v=gb7zo6lPZSs&t

The most frequent text

In the pre­vi­ous post, I was talk­ing about a dom­i­nant, which is the most fre­quent num­ber. Today, we are going to find the most fre­quent text.

Most fre­quent text 

This task is a bit hard­er, because the MODE func­tion that we used pre­vi­ous­ly needs num­bers. It means that we have to con­vert text into num­bers. To do so, we can use the MATCH func­tion. It means that we will be look­ing for our whole text in our whole text. We want the exact match (Fig. 1)

MATCH function to look for the exact match
Fig. MATCH func­tion to look for the exact match

While using the MATCH func­tion, Excel starts cal­cu­la­tions from the top. It means that each pup­py in the range is refer­ring to the first one on the list, which is on the first posi­tion is pre­sent­ed as 1 (Fig. 2)

uppy gets number 1
Fig. 2 Pup­py gets num­ber 1

Look­ing at the sec­ond exam­ple, the first kit­ten is in posi­tion num­ber 2, so each kit­ten will refer to the same num­ber (Fig. 3)

Kitten gets number 2
Fig. 3 Kit­ten gets num­ber 2

Now, that we have our text con­nect­ed to num­bers, we can add the MODE.MULT func­tion to return the most fre­quent num­bers, i.e. posi­tions of text (Fig. 4)

=MODE.MULT(MATCH(A2:A9,A2:A9,0))

 MODE.MULT function to return the most frequent numbers
Fig. 4 MODE.MULT func­tion to return the most fre­quent numbers

And we have the results (Fig. 5)

Results
Fig. 5 Results

Since we have the posi­tions, we can add the INDEX func­tion to return text con­nect­ed to those posi­tions (Fig. 6)

=INDEX(A2:A9,MODE.MULT(MATCH(A2:A9,A2:A9,0)))

INDEX function to return text
Fig. 6 INDEX func­tion to return text

Just like that we have our solu­tions in the Dynam­ic Array Excel (Fig. 7)

Solutions in Dynami Array
Fig. 7 Solu­tions in Dyna­mi Array

In the clas­sic Lega­cy Excel, we should select more cells and use the Ctrl + Shift + Enter key com­bi­na­tion to put our results in all cells (Fig. 8)

Key combination
Fig. 8 Key combination

How­ev­er, we are in DA Excel, so we have a sim­pler solu­tion. At the end, we want to check what hap­pens when the text appears only once in a range. We can see that the MODE.MULT func­tion returns the #N/A error (Fig. 9)

 #N/A error
Fig. 9 #N/A error

When text appears the same num­ber of times, in our case it’s cat and dog, the MODE.MULT func­tion will return those results (Fig. 10)

Results
Fig. 10 Results

https://www.youtube.com/watch?v=MpdrAz_ZNdo

Random list no repeats, manual and formulas solutions

Today, we are going to talk about choos­ing a ran­dom ele­ment from a list with­out repeats. We have eight ele­ments in our list. I put there the same num­ber two times, which gives me the oppor­tu­ni­ty to choose the same num­ber two times. If there are three the same num­bers, I could choose the num­ber up to three times. How­ev­er, the choos­ing is ran­dom, so I don’t real­ly know how many times I will choose it. 

Ran­dom list no repeats, man­u­al and for­mu­las solutions
 A list of elements
Fig. 1 A list of elements

If we want to choose an ele­ment from the list at ran­dom with­out repeats, we can use a man­u­al solu­tion. We need to write the RAND func­tion which returns num­bers from 0 to 1 with fif­teen dig­it pre­ci­sion. It means that it’s almost impos­si­ble to choose the same num­ber twice (Fig. 2). 

=RAND()

RAND function
Fig. 2 RAND function

After drag­ging for­mu­la down, we have 8 ran­dom num­bers. We have to remem­ber that the RAND func­tion is a volatile func­tion. It means that it is recal­cu­lat­ed every time our work­sheet changes (Fig. 3).

Recalculation
Fig. 3 Recalculation

Now, that we have our helper col­umn, we can do the sort­ing. We go to Data tab and we choose the A to Z or Z to A command.

Sorting
Fig. 4 Sorting

If we want to have, e.g. five ele­ments, we have to choose five ele­ments from the top. It does­n’t mat­ter whether we sort from A to Z or Z to A. We just choose the first five ele­ments. We can see that the num­bers are recal­cu­lat­ed every time we sort our data (Fig. 5)

Five elements from the list
Fig. 5 Five ele­ments from the list

Let’s get back to our orig­i­nal list and try the sec­ond solu­tion. It is from Lega­cy Excel and it uses for­mu­las. Let’s start with a way to sort our num­bers. We can use, e.g. the LARGE func­tion, which returns the biggest num­ber from the list, then the sec­ond biggest, then the third biggest, and so on. We have to press the F4 key to lock our list. Then, we need a way to choose the first, the sec­ond, and so on num­bers. So, we want to change our key argu­ment using a prop­er func­tion. One of the sim­plest solu­tion I know is using the ROW func­tion with ref­er­ence to cell A1, which is free (with­out $ signs). When we copy our for­mu­la down, our A1 ref­er­ence will also go down, and the ROW func­tion will return the first row, the sec­ond, the third, and so on (fig. 6)

=LARGE($B$2:$B$9,ROW(A1))

ROW function
Fig. 6 ROW function

After enter­ing the for­mu­la, we have our num­bers placed from the largest to the small­est. Our five ele­ments are now in a prop­er order. It means that now we can choose ele­ments. How­ev­er, we must use the MATCH and INDEX func­tion in our data ori­en­ta­tion. Let’s add the MATCH func­tion to find the posi­tion of our num­ber. We have to add the range, which is our ran­dom num­ber col­umn and press the F4 key to lock it. Then, we are adding 0 because we want to have the exact match (Fig. 7)

=MATCH(LARGE($B$2:$B$9,ROW(A1)),$B$2:$B$9,0)

MATCH function
Fig. 7 MATCH function

After enter­ing and copy­ing down, we have posi­tions of the largest num­bers. When we look at the first posi­tion in our Cho­sen col­umn, we have 5. It means that the largest num­ber from our orig­i­nal list is locat­ed in the 5th place. 

Position of the largest number
Fig. 8 Posi­tion of the largest number

How­ev­er, we don’t want to have posi­tion num­bers, but the ele­ments con­nect­ed to those num­bers. It means that we have to add the INDEX func­tion to our MATCH func­tion. This time, we want to choose ele­ments from our list, so we are adding the range and lock it by press­ing the F4 key. It turns out that our MATCH func­tion is the row num­ber. We have to close the for­mu­la (Fig. 9)

=INDEX($A$2:$A$9,MATCH(LARGE($B$2:$B$9,ROW(A1)),$B$2:$B$9,0))

INDEX function
Fig. 9 INDEX function

After enter­ing and copy­ing down, we have ele­ments select­ed at ran­dom with­out repeats. When I press the F9 key, I am forc­ing it to recal­cu­late our results. Some­times, we see our num­ber (123) twice because it is twice on our orig­i­nal list (Fig. 10)

Elements selected at random
Fig. 10 Ele­ments select­ed at random

It’s high time we tried the final solu­tion in Dynam­ic Array Excel. This solu­tion is sim­pler that the one in Lega­cy Excel because we just need two func­tions. In DA Excel, we have the RANDARRAY func­tion which can cre­ate a ran­dom array. We just need to copy the same col­umn into our for­mu­la, which means that we need an array with eight rows and one col­umn. That’s why we are writ­ing just 8 (Fig. 11).

=RANDARRAY(8)

RANDARRAY function
Fig. 11 RANDARRAY function

After putting our for­mu­la into Excel, we have eight ran­dom num­bers. In DA Excel, we can sort by this col­umn. Thus, we have to add the SORTBY func­tion to our for­mu­la. The array which we want to sort is our list. This time, we don’t have to lock it because it is a DA for­mu­la, and our for­mu­la is the result (Fig. 12). 

=SORTBY(A2:A9,RANDARRAY(8))

SORTBY function
Fig. 12 SORTBY function

After putting the for­mu­la down, we have eight ele­ments from our list in a ran­dom order. Press­ing the F9 key allows us to choose ele­ments once again at ran­dom. Let’s choose five ele­ments from the top once again. We still can see the remain­ing ele­ments. If you don’t want to see them, you have some options. You can, for exam­ple, select five ele­ments from our list, then press the F2 key to go to the for­mu­la edit mode, then press the Ctrl + Shift + Enter com­bi­na­tion to cre­ate an array for­mu­la. The array for­mu­la is from Lega­cy Excel. Now, we have only five ele­ments because I’ve select­ed only five cells. Those cells are an array, which means that we can­not change or delete any sin­gle ele­ment of the array. If you want to change it, you have to change the whole array at one time (Fig. 13).

An array
Fig. 13 An array

Sum­ming up, if you want to choose an ele­ment at ran­dom from a list, you can use a man­u­al solu­tion, a Lega­cy Excel solu­tion or a Dynam­ic Array solution. 

https://www.youtube.com/watch?v=myT-vQzDSeE