Excel – The XLOOKUP function

We can use LOOKUPSs in Excel to search for a value in a column or row, and return a value from a matching column or row.

We look at them in a lot of depth on both our Excel Advanced and Excel Advanced Formulas and Functions courses.

There are a number of lookup and reference functions in Excel and Microsoft adds more to give us more efficient solutions. Many Excel users use the VLOOKUP function and really like the functionality, but the VLOOKUP has many limitations and is also very memory heavy.

If you are working with large data sets in particular, the VLOOKUP can turn into a nightmare. The VLOOKUP must store all the data from the table arrays in the computer’s memory. This can crash  Excel. The file becomes difficult to work with because the file is too slow.

 

In this blog post you will see how the much more efficient XLOOKUP can replace the VLOOKUP and HLOOKUP. If you have knowledge of using the INDEX and MATCH function, the logic of the XLOOKUP function will make a lot of sense!

 

The structure of the XLOOKUP (arguments inside square brackets are optional)

 

=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])

The lookup_value is the value to search for. If you enter nothing, the function will search for blank cells.

The lookup_array is the array or range in which you want to search for the lookup_value.

The return_array is the array or range to return value from.

The [if_not_found] is where a valid match is not found, return the [if_not_found] text you supply.

The [match_mode] is 0 an exact match, 1 Exact match. If none found, return the next larger item, -1 Exact match. If none found, return the next smaller item, and 2 A wildcard match.

The [search_mode] XLOOKUP can perform a search starting at the first item. This is the default or perform a reverse search starting at the last item.

 

Example 1  – XLOOKUP exact match

A company wants to increase prices for their products. They have a price list from last year and want to lookup the last year’s prices to this year’s price list.

The products are identified by a unique product ID, which will be used as the lookup value.

Last year’s price list. Below you can see two product ID columns. The reason for this is just to be able to explain how the XLOOKUP is superior to the VLOOKUP.

value

Below you can see the new price list and in column “Last year price” the LOOKUP

XLOOKUP

In this example A5 is the lookup value. The ID to identify the product.

Excel

The lookup array here is on the sheet with last year’s prices. If you are used to use VLOOKUP the lookup value can only be found in the first column of the table array. It means that we can only use the VLOOKUP function if the data is organised the right way. The XLOOKUP does not have this limitation.

Next argument return array is the column, Price. The XLOOKUP needs to return the price for the product identified by the lookup value.

The [if_not_found] argument has many uses. If the VLOOKUP cannot find the lookup value, it will return an #N/A! error. You can tell the XLOOKUP what to return if their is not an exact match to the lookup value. In this example, some new products have been added to the new price list and the XLOOKUP has to return “New Product” if it cannot find the lookup value.

The XLOOKUP needs to find exact match so 0 is entered in the [match_mode] argument. The last argument [search_mode] is not used here, but you will find an example of this later in this blog post.

The finale function:

=XLOOKUP(A5,’Product Prices 2022′!$E$4:$E$18,’Product Prices 2022′!$D$4:$D$18,”New Product”,0)

This will return the prices last year for products identified by the product IDs. Product ID 16 and 17 where not on the last year’s price list, that is why the XLOOKUP return New Product.

value

 

Example 2  – XLOOKUP approximate match

Next step is to look up the price increase percentage. Top right corner below you can see the table with the increase  percentages. If the price last year was between 0 and 50 the XLOOKUP should look up 5%, between 50 and 100; 4%, between 100 and 150 etc.

The XLOOKUP will look like this:

=XLOOKUP(D5,$H$2:$H$7,$I$2:$I$7,,-1)

The lookup value is the price last year, the lookup array is the price range column, the return array the Raise % column, the [if not found] is bypassed, and [match mode] is -1 one, to find exact match or nearest lowest match.

value

 

Example 3  – XLOOKUP two ways lookup value (horizontal and vertical lookup)

The XLOOKUP function replace both the VLOOKUP and the HLOOKUP. You will have to use two XLOOKUPs to do a two ways lookup. The first to find the right row and the second to find the right column.

In this example the XLOOKUP needs to return the product category or product name for a product identified by a product ID.

To find the right row we will use the product ID as lookup value. Here entered in E2. The lookup array is column A, where all the product IDs are listed.

=XLOOKUP(E2,A5:A21

It is in the argument return array, we need to nest the second XLOOKUP.

To find the right column, we can use the product category or product nameas lookup value. Here entered in F1. The lookup array is the range B4:C4. The return array is the range now for both XLOOKUPs the range B5:C21. Excel will bypass the rest of the arguments, because we do not need them in this example.

=XLOOKUP(E2,A5:A21,XLOOKUP(F1,B4:C4,B5:C21))

Example 4  – XLOOKUP search bottom to top for value

You  can define if you want the XLOOKUP to  search top to bottom or bottom to top for the lookup value.  How can this be useful? Normally it doesn’t matter, but in some situations this option can be a game changer.

In this example sales meeting details are listed by date, and we need to find the details from the last meeting with a specific customer. By using the customer’s name as the lookup value and search from bottom and up, the XLOOKUP will return the information needed.

 

To get the date the XLOOKUP looks like this (the last argument -1 is the [search mode] bottom to top).

=XLOOKUP(F3,B2:B21,A2:A21,,0,-1)

And to lookup the sales mount.

=XLOOKUP(F3,B2:B21,C2:C21,,0,-1)

You can see the result of the two XLOOKUPs below top right.

Example 5  – XLOOKUP find a start point and an end point in a range

In this example we need to sum a range, and we can use the XLOOKUP function to find a start point and an end point in the range.

We use the SUM function to sum the range. In the first XLOOKUP the lookup value is the start date, the lookup array is the column with all the meeting dates, the return array is the column order amounts, and 1 in the argument [match mode] is to get exact match or nearest higher match.

In the second XLOOKUP the lookup value is the end date, the lookup array is the column with all the meeting dates, the return array is the column order amounts, and -1 in the argument [match mode] is to get exact match or nearest lower match.

=SUM(XLOOKUP(G9,A2:A21,C2:C21,,1):XLOOKUP(H9,A2:A21,C2:C21,,-1))

value

Example 6  – XLOOKUP use wildcards lookup  value

You can lookup a part of a text string by using wildcards. Here are some examples of Wildcards:

? (question mark)             Use a question mark for any single character.

For example, Mart?n finds “Martin” and “Martyn.”

* (asterisk)          Adding the asterisk finds any number of characters.

For example, *west finds “Northwest” and “Southwest”.

Conclusion

XLOOKUP is a much more efficient function than VLOOKUP and HLOOKUP. The XLOOKUP does not have all the limitations a VLOOKUP have. You get much more options by using the XLOOKUP.

 

Clean data in Power Query to improve efficiency (part 1)

Have you ever pulled in data from various places to restructure it in Excel? Have you found yourself repeating this long, manual process every month when new data is added? The solution is to use Power Query. This blog is the first in a series of four. We will show you what Power Query can do to save you lots of time and help you become more productive. Let’s see how we can clean data in Power Query to improve efficiency (part 1).

What is Power Query and why is it useful?

Power Query is an app which transforms data sourced from many different locations before it is loaded into Excel. It works by making a connection back to the original data source. This means that whenever changes are made, they are saved as ‘Applied Steps’. These are stored in memory. The benefit is that a simple refresh in Excel is all that is needed to update any changes to the original data.

How to use Power Query
  1. In Excel, open a dataset with a column structure that needs some cleaning. In the example below, the ‘Full Name’ field has text entries where unwanted spaces need to be removed e.g. ‘ Anna Brown’

  1. Ideally the data needs to be a table before launching Power Query. To create a table, select a cell within the dataset and go to HOME > FORMAT AS TABLE. Select a coloured thumbnail and click OK in the ‘Create Table’ box
  2. Connect to the source data: DATA > FROM TABLE/RANGE. (NB. For any data that is external to Excel, go to DATA > GET DATA and browse to the specific data source)
  3. The following window appears:

  1. Note the ‘Applied Steps’ section on the right that will expand to include further steps
  2. Now select the ‘Full Name’ field, right click, and go to TRANSFORM > TRIM.

Right click on the ‘Extract Numbers’ field and go to SPLIT COLUMN > BY NUMBER OF CHARACTERS:

  1. Enter ‘2’ for ‘Number of Characters’ and select the radio button to split: ‘Once, as far right as possible’. Click OK
  2. Double click on each of the split columns and enter ‘Region’ and ID_no.

Now apply the changes back to Excel:

  1. In the Home tab, go to CLOSE & LOAD > CLOSE & LOAD TO > TABLE. Click OK to load the table to a new sheet
  2. Now in a new row under ‘Full Name’ add ‘ Mike Orange’ – with 2 leading spaces and under ‘Extract Numbers add ‘North19’
  3. Finally in the new sheet, go to QUERY > REFRESH and check the new data has changed

Power Query in Excel can connect to a vast range of external sources as well as within Excel itself. These include other Excel workbooks, financial systems, databases, websites, SharePoint, and many others.

Conclusion

Power Query is a powerful and extremely useful tool. It allows you to repeat routine cleaning tasks on your datasets without having to do it all manually. This tool will therefore help you become more efficient in the way you manage data which ultimately increases profitability. This concludes ‘Clean data in Power Query to improve efficiency (part 1)’.

For course details on our Power Query course at STL, please click on the link below:

https://www.stl-training.co.uk/syl/300/excel-power-query.html

Check out this useful article on data trends:

https://www.inc.com/john-hall/5-trends-in-data-emerging-in-2020.html