You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

XLOOKUP Not Return Desired Results

I am trying to make a mileage log, but not all miles are tracked.  Often, there are gaps between the vehicle’s beginning mileage and its “prior” ending mileage.


I jot the mileage on small postage notes, then at some point I enter the mileage into Numbers.  While I try to keep the notes in sequence, occasionally I will misplace or get one out of order.


If I enter a vehicle’s beginning mileage that’s less than its prior ending mileage, I want to highlight my beginning mileage is not correct.  The mileage can be equal to the last ending milage, but it cannot be less than the last ending mileage.


I though the best way to do this was to create a helper cell (column G: Last Logged Ending Mileage).  Then use conditional formatting to highlight if the vehicle’s beginning mileage is less than the vehicle’s last ending mileage. This may not be the best practice, but I though it’s something I could do.


I used the XLOOKUP function (see screenshot for formula), but I’m not getting the desired results.  As an example: using row 7, I was expecting the function to match the vehicle in column B (i.e., GMC), then go to the GMC's last entry (row 5) and return the ending mileage of 121 from column D, but instead it return 105.


I would appreciate help my formula and welcome suggestions for best practices or alternative solutions.


Thank you


Posted on Sep 4, 2021 1:46 PM

Reply
Question marked as Top-ranking reply

Posted on Sep 4, 2021 2:58 PM

Something is not right about your screenshot and formula. The screenshot has cell F7 selected but the formula appears to be for cell F5 and appears to be looking for a beginning odometer reading (C5) in column B, which is the vehicle makes. I will assume the table is the correct one and it is looking for B7 (which has "GMC" in it) in B column.


The issue you are seeing is that XLOOKUP will always return the first matching value. If you are doing a top down search of column B for the text "GMC", the first "GMC" it finds will always be row 2. The default for XLOOKUP is a top down search.


You need to do a bottom up search, starting one row above the formula.

The formula below is for cell F2. Note that the formula will break if the table is sorted, until Apple finally addresses the bug that causes it to break. There is a sort-safe version but it is harder to understand.


=XLOOKUP(B2, B$1:B1,D$1:D1,"", 0, -1)

Fill down to complete the column

The 0 is for "exact match"

The -1 is for bottom up search

It includes row 1 in the search so if you enter a vehicle make of "Vehicle" you will get an answer of "Beginning odometer". I doubt this will be a problem, just pointing out a flaw.






3 replies
Question marked as Top-ranking reply

Sep 4, 2021 2:58 PM in response to JessA1965

Something is not right about your screenshot and formula. The screenshot has cell F7 selected but the formula appears to be for cell F5 and appears to be looking for a beginning odometer reading (C5) in column B, which is the vehicle makes. I will assume the table is the correct one and it is looking for B7 (which has "GMC" in it) in B column.


The issue you are seeing is that XLOOKUP will always return the first matching value. If you are doing a top down search of column B for the text "GMC", the first "GMC" it finds will always be row 2. The default for XLOOKUP is a top down search.


You need to do a bottom up search, starting one row above the formula.

The formula below is for cell F2. Note that the formula will break if the table is sorted, until Apple finally addresses the bug that causes it to break. There is a sort-safe version but it is harder to understand.


=XLOOKUP(B2, B$1:B1,D$1:D1,"", 0, -1)

Fill down to complete the column

The 0 is for "exact match"

The -1 is for bottom up search

It includes row 1 in the search so if you enter a vehicle make of "Vehicle" you will get an answer of "Beginning odometer". I doubt this will be a problem, just pointing out a flaw.






Sep 4, 2021 6:22 PM in response to Badunit

Badunit,


Thank you. I honestly though I changed the search-type to Last to First (-1), but was getting the same results.


I'm sorry for the confusion with screenshot and formula. I copied the table and formula, then pasted them together to make a single image. I copied/pasted the wrong formula screenshot to the bottom of the table.


Again, thank you for your help.

XLOOKUP Not Return Desired Results

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.