Explanation
XLOOKUP offers several features that make it exceptionally good for more complicated lookups. In this example, we want the latest price for an item by date . If data were sorted by date in ascending order, this would be very straightforward . However, in this case, data is unsorted .
By default, XLOOKUP will return the first match in a data set. To get the last match , we can set the optional argument search_mode , to -1 to cause XLOOKUP to search “last to first”. However, we can’t use this approach here because there is no guarantee that the latest price for an item appears last.
Instead, we can set the optional argument match_mode to -1 to force an approximate match of “exact or next smallest”, and adjust the lookup value and lookup array as explained below. The formula in G5, copied down, is:
=XLOOKUP(MAX(date),(item=F5)*date,price,,-1)
Working through arguments one by one, the lookup_value is the largest (latest) date in the data:
MAX(date) // get max date value
The lookup_array is derived with a boolean logic expression:
(item=F5)*date
By comparing each item to the value in F5, “Belt”, we get an array of TRUE/FALSE values:
{TRUE;FALSE;FALSE;FALSE;FALSE;FALSE;TRUE;FALSE;TRUE;FALSE;FALSE}
where TRUE values represent entries for “Belt”. This array acts like a filter. When it is multiplied by the values in the named range date , the TRUE/FALSE values are evaluated to 1’s and 0’s:
={1;0;0;0;0;0;1;0;1;0;0}*date
The result is an array that contains only zeros and dates for belts:
={43484;0;0;0;0;0;43561;0;43671;0;0}
Note: the serial numbers are valid Excel dates .
- This array is delivered directly to XLOOKUP as the lookup_array argument.
- The return_array is the named range price (D5:D15)
- The optional argument not_found is not provided.
- Match_mode is set to -1, for exact match, or next smallest item.
XLOOKUP looks through the lookup array for the maximum date value. Since the array has already been filtered to exclude dates not associated with “Belt”, XLOOKUP simply finds the best match (either the exact date, or the next smallest date) which corresponds to the latest date. The final result is the price associated with the latest date. The formula will continue to work when the data sorted in any order.
Explanation
Whereas VLOOKUP is limited to lookups to the right of the lookup column, XLOOKUP can lookup values to the left natively. This means XLOOKUP can be used instead of INDEX and MATCH to find values to the left in a table or range.
In the example shown, we are looking for the weight associated with Model H in row 12. The formula in H6 is:
=XLOOKUP(H4,E5:E14,B5:B14)
- The lookup_value comes from cell H4
- The lookup_array is the range E5:E14, which contains Model
- The return_array is B5:B14, which contains Weight
- The match_mode is not provided and defaults to 0 (exact match)
- The search_mode is not provided and defaults to 1 (first to last)
Lookup multiple values
XLOOKUP can return more than one value at a time from the same matching record. The formula in cell G9 is:
=XLOOKUP(H4,E5:E14,B5:D14)
which returns the Height, Weight, and Price of Model H in an array that spills into the range G9:H9.
The only difference from the formula above is that return_array is entered as a range that contains more than one column, B5:D14.
Dynamic Array Formulas are available in Office 365 only.