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

Excel SORT Function

Today, we want to learn how the SORT func­tion works in Excel. 

Excel SORT Function

The SORT func­tion is avail­able from Dynam­ic Array Excel, which is around 2021. Let’s start with the sim­plest ver­sion of the SORT func­tion, where we only need to write an array. We have to remem­ber that the array must be giv­en with­out head­ers. Let’s select our data and put it as an argu­ment and let’s put our for­mu­la into a cell (Fig. 1)

=SORT(A2:C169)

SORT function
Fig. 1 SORT function

And we have the results. The most impor­tant thing about for­mu­las in Dynam­ic Array Excel is that they spill. It means that our for­mu­la is in only one cell, but the results spill. The for­mu­la is only in cell E2, but when we click on cell E3, we can see that the for­mu­la is grayed out in the for­mu­la bar. It means that it con­tains results of this func­tion, but does­n’t con­tain the func­tion itself (Fig. 2)

Formula grayed out
Fig. 2 For­mu­la grayed out

When we add an array with data to be sort­ed, Excel’s sort­ing will be based on the first col­umn, which is the col­umn with dates. In the sort­ed array we don’t see dates but num­bers because Excel does­n’t know how to copy the for­mat­ting in array for­mu­las. If we want to have prop­er for­mat­ting, we can copy the for­mat­ting using the For­mat Painter (2) from the Home tab (1). We will also high­light the cell with the for­mu­la (3) (Fig. 3)

Using the Format Painter command
Fig. 3 Using the For­mat Painter command

Now, our results are for­mat­ted. Since we sort­ed by the first col­umn, our results are basi­cal­ly the same as in our orig­i­nal col­umn. To change that, we can add the sec­ond argu­ment to our SORT func­tion, which is the sort index. It’s the num­ber of a col­umn by which we want to sort our data. Let’s write 2, mean­ing the sec­ond col­umn (Fig. 4)

=SORT(A2:C169,2)

SORT formula with the second argument
Fig. 4 SORT for­mu­la with the sec­ond argument

We can see that the sort­ing has changed. Now, we can see that only Chan­dlers are at the top because it’s an ascend­ing sort­ing. This way we can sort by any col­umn. Let’s move on to the third argu­ment, which is the sort order. Let’s change it to a descend­ing sort­ing by writ­ing ‑1 (Fig. 5)

=SORT(A2:C169,2,-1)

Descending sorting
Fig. 5 Descend­ing sorting

Now, the first sales­man in Ross. Let’s add some­thing more. Let’s sort by Sales from the largest to the small­est. Now, the sales are writ­ten ran­dom­ly in the Sales col­umn. We can add the third col­umn to the sec­ond argu­ment. We can also add a ref­er­ence to cells where num­bers of columns are writ­ten. It’s very impor­tant to write them in the cor­rect order. In our case the first cell from the top con­tains 2 and the sec­ond one con­tains 3. It means that first, we will sort by the sec­ond col­umn, and then by the third col­umn (Fig. 6)

Adding a cell reference
Fig. 6 Adding a cell reference

And we have the results. We can see that Ross is sort­ed by val­ues in the Sales col­umn. The same is with the rest of the sales­men. Now, let’s focus on the third argu­ment once again. We have two columns of sort­ing in the sec­ond argu­ment, but only one num­ber (-1) in the third argu­ment which is the sort order. It means that the sort­ing is descend­ing for each col­umn. When we want to have an ascend­ing order for one col­umn and a descend­ing order for anoth­er col­umn, we can do the same as we did with the sec­ond argu­ment. We have to refer to some cells in Excel. In our case we have a sort­ing order for each col­umn (Fig. 7)

Referring to other cells
Fig. 7 Refer­ring to oth­er cells

Now, we have an ascend­ing sort­ing for the Sales­man col­umn, and a descend­ing sort­ing for the Sales column. 

When we don’t want any addi­tion­al cells in our sheet, we can hard code them in the for­mu­la by press­ing F9 key. It changes the argu­ment into an array. We can do the same with the sort order argu­ment. When we look at the for­mu­la now, we can see that the sec­ond col­umn in ascend­ing, and the third col­umn is descend­ing (Fig. 8)

Hard coding the values
Fig. 8 Hard cod­ing the values

Now that we have every­thing we need­ed, we can can­cel the unnec­es­sary cells. We don’t even need the third argu­ment, as it is reserved for hor­i­zon­tal sort­ing. Here, we are sort­ing only by columns, so we can leave it like that. Here are our results (Fig. 9)

Results
Fig. 9 Results

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