SOLVED
Home

Can I get MATCH to grab the last identical value in an array?

%3CLINGO-SUB%20id%3D%22lingo-sub-755980%22%20slang%3D%22en-US%22%3ECan%20I%20get%20MATCH%20to%20grab%20the%20last%20identical%20value%20in%20an%20array%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-755980%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20everyone%20-%20I%20am%20wresting%20with%20some%20MATCH%20challenges%20and%20would%20appreciate%20some%20help.%26nbsp%3B%20In%20this%20case%2C%20my%20ideal%20outcome%20would%20be%20having%20MATCH%20grab%20the%20last%20value%20in%20a%20list%20of%20duplicate%20values%20(it%20defaults%20to%20grabbing%20the%20first%20duplicate%20value).%26nbsp%3B%20I%20have%20been%20trying%20other%20solutions%20such%20as%20MMULT%20but%20cannot%20crack%20this%20one.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20is%20some%20sample%20data.%26nbsp%3B%20I%20have%20a%20database%20of%20Users%20who%20progressed%20through%20a%20game%20to%20specific%20Levels%20at%20a%20certain%20Time%20(the%20exact%20Time%20they%20reached%20that%20Level).%26nbsp%3B%20Consider%20the%20Time%20data%20here%20to%20be%20the%20time%20in%20minutes%20since%20the%20User%20started%20playing%20the%20game.%3C%2FP%3E%3CP%3EI%20have%20other%20data%20which%20contains%20the%20User%20and%20an%20event%20which%20happened%20at%20a%20particular%20Time.%26nbsp%3B%20I%20am%20trying%20to%20determine%20what%20Level%20the%20User%20was%20at%20during%20that%20Time.%26nbsp%3B%20The%20second%20section%20shows%20some%20sample%20inputs%20for%20User%20and%20Time%2C%20and%20the%20desired%20result%20for%20Level.%3C%2FP%3E%3CP%3EThe%20last%20user%20in%20the%20source%20data%20is%20put%20at%20Level%2010%20because%20the%20MATCH%20formula%20is%20grabbing%20that%20row%20by%20mistake%20in%20some%20cases%20and%20I%20wanted%20to%20easily%20see%20when%20that%20was%20happening.%3C%2FP%3E%3CP%3EThe%20right%20columns%20labeled%20X%20and%20Y%20show%20the%20results%20using%20two%20variations%20of%20the%20MATCH%20function.%26nbsp%3B%20column%20X%20uses%20the%20exact%20match%20argument%20(%220%22)%3B%20column%20Y%20uses%20%221%22%20as%20the%203rd%20argument%20in%20the%20MATCH%20function.%26nbsp%3B%20Correct%20answers%20are%20highlighted%20in%20green.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EObservations%3A%3C%2FP%3E%3CP%3E-%20In%20column%20X%2C%20the%20wrong%20answers%20are%20due%20to%20MATCH%20grabbing%20the%20first%20value%20in%20a%20list%20of%20duplicates.%3C%2FP%3E%3CP%3E-%20Column%20Y%20seems%20clearly%20to%20be%20the%20wrong%20approach%20but%20it%20yields%20some%20correct%20answers%20vs.%20column%20X.%3C%2FP%3E%3CP%3E-%20Row%2013%20is%20the%20same%20data%20as%20Row%203%2C%20but%20column%20Y%20yields%20different%20results.%26nbsp%3B%20Weird.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20looking%20for%20any%20help%20to%20yield%20the%20right%20results%2C%20even%20different%20formulas%20or%20macros.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20759px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F123346i51710C506483C0EC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFormula%20in%20cell%20I2%20is%3A%20%7B%3DINDEX(%24A%242%3A%24C%2414%2CMATCH(1%2C(E2%3D%24A%242%3A%24A%2414)*(F2%26gt%3B%3D%24C%242%3A%24C%2414)%2C0)%2C2)%7D.%26nbsp%3B%20I%20used%20CSE%20when%20entering%20this%20formula%2C%20and%20copied%20it%20down%20to%20I14.%3C%2FP%3E%3CP%3EFormula%20in%20cell%20J2%20is%20%7B%3DINDEX(%24A%242%3A%24C%2414%2CMATCH(1%2C(E2%3D%24A%242%3A%24A%2414)*(F2%26gt%3B%3D%24C%242%3A%24C%2414)%2C1)%2C2)%7D.%26nbsp%3B%20Ditto%20here%20for%20CSE%20and%20copied%20to%20J14.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%20for%20help%20on%20this.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-755980%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%20Desktop%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ENeed%20Help%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-756075%22%20slang%3D%22en-US%22%3ERe%3A%20Can%20I%20get%20MATCH%20to%20grab%20the%20last%20identical%20value%20in%20an%20array%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-756075%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F376765%22%20target%3D%22_blank%22%3E%40CrankyPantz%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20you%20want%20the%20last%20match%2C%20LOOKUP%20is%20the%20function%20to%20use.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20can%20reproduce%20your%20column%20G%20results%20with%20the%20following%20non-CSE%20formula%3A%3C%2FP%3E%0A%3CPRE%3E%3DINDEX(%24B%242%3A%24B%2414%2CLOOKUP(1E%2B307%2C(ROW(%24A%242%3A%24A%2414)-ROW(%24A%242)%2B1)%2F((E2%3D%24A%242%3A%24A%2414)*(F2%26gt%3B%3D%24C%242%3A%24C%2414))))%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-756085%22%20slang%3D%22en-US%22%3ERe%3A%20Can%20I%20get%20MATCH%20to%20grab%20the%20last%20identical%20value%20in%20an%20array%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-756085%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20are%20probably%20best%20off%20%3CSTRONG%3E%3CEM%3Enot%3C%2FEM%3E%3C%2FSTRONG%3Ereading%20the%20on-line%20help%20for%20LOOKUP.%20It%20will%20only%20mislead%20you.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELOOKUP%20has%20a%20number%20of%20useful%20properties%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EContrary%20to%20the%20on-line%20help%2C%20the%20lookup%20column%20does%20%3CSTRONG%3E%3CEM%3Enot%3C%2FEM%3E%3C%2FSTRONG%3Eneed%20to%20be%20sorted%20in%20ascending%20order%3C%2FLI%3E%0A%3CLI%3EIf%20you%20search%20for%20a%20value%20so%20big%20(either%20in%20numeric%20magnitude%20or%20alphabetic%20sort%20order)%20it%20will%20%3CEM%3Ealways%3C%2FEM%3Ebe%20last%2C%20LOOKUP%20matches%20the%20last%20value%20of%20the%20same%20data%20type%20as%20the%20first%20parameter%3C%2FLI%3E%0A%3CLI%3ELOOKUP%20ignores%20error%20values%3C%2FLI%3E%0A%3CLI%3EIf%20you%20give%20LOOKUP%20two%20parameters%2C%20it%20returns%20the%20last%20match%20from%20the%20second%20parameter.%20If%20you%20give%20it%20three%20parameters%2C%20it%20returns%20the%20value%20in%20the%20third%20parameter%20that%20corresponds%20to%20the%20match%20found%20in%20the%20second%20parameter.%3C%2FLI%3E%0A%3CLI%3EIf%20you%20use%20LOOKUP%20in%20an%20array%20formula%2C%20it%20does%20not%20need%20to%20be%20array-entered.%20It's%20like%20SUMPRODUCT%20and%20AGGREGATE%20in%20that%20respect.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EIn%20the%20suggested%20formula%2C%20the%20magic%20happens%20in%20the%20second%20parameter%20of%20LOOKUP.%20The%20numerator%20is%20the%20index%20numbers%201%20through%2013%2C%20while%20the%20denominator%20is%20the%20results%20of%20your%20criteria%20testing.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20numerator%20of%20the%20second%20LOOKUP%20parameter%20returns%20index%20numbers%201%20through%2013.%20Although%20I%20could%20simplify%20the%20expression%20for%20the%20given%20layout%2C%20many%20people%20don't%20know%20how%20to%20change%20ROW(%24A%242%3A%24A%2414)-1%20for%20a%20different%20worksheet%20layout%2C%20but%20have%20no%20trouble%20adjusting%20(ROW(%24A%242%3A%24A%2414)-ROW(%24A%242)%2B1).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20denominator%20is%20a%20Boolean%20expression%20holding%20the%20criteria%20for%20which%20values%20to%20include%3A%20a%20match%20for%20E2%20in%20column%20A%20and%20F2%26gt%3B%3D%20values%20in%20column%20C.%20Each%20test%20returns%20an%20array%20of%20TRUE%20and%20FALSE%2C%20which%20get%20converted%20to%201%20and%200%20when%20used%20in%20an%20arithmetic%20expressions.%20Since%20the%20criteria%20are%20in%20the%20denominator%2C%20you%20get%20an%20array%20of%20either%20index%20number%20for%20the%20rows%20or%20a%20DIV%2F0!%20error%20values.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20first%20LOOKUP%20parameter%201E%2B307%20is%20a%20very%20large%20number.%20Although%20the%20formula%20would%20work%20with%20the%20given%20layout%20using%2014%2C%20I%20like%20to%20choose%20a%20number%20so%20large%20it%20will%20always%20work.%20Doing%20so%20gets%20people's%20attention%20and%20makes%20them%20reluctant%20to%20make%20changes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFinally%2C%20rather%20than%20applying%20INDEX%20to%20%24A%242%3A%24C%2414%20and%20then%20specifying%20the%20result%20come%20from%20the%20second%20column%2C%20I%20just%20use%20%24B%242%3A%24B%2414%20and%20drop%20the%20third%20parameter%20in%20the%20INDEX%20function.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-756519%22%20slang%3D%22en-US%22%3ERe%3A%20Can%20I%20get%20MATCH%20to%20grab%20the%20last%20identical%20value%20in%20an%20array%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-756519%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F376765%22%20target%3D%22_blank%22%3E%40CrankyPantz%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYour%20desired%20results%20can%20be%20returned%20by%20solely%20using%20LOOKUP%2C%20like%20this%20starting%20in%20Row%202%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3DLOOKUP(2%2C%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E1%2F((A%242%3AA%2414%3DE2)*(C%242%3AC%2414%26lt%3B%3DF2))%2C%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EB%242%3AB%2414)%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-757130%22%20slang%3D%22en-US%22%3ERe%3A%20Can%20I%20get%20MATCH%20to%20grab%20the%20last%20identical%20value%20in%20an%20array%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-757130%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F280482%22%20target%3D%22_blank%22%3E%40Twifoo%3C%2FA%3E%2C%3C%2FP%3E%0A%3CP%3EThe%20simple%20LOOKUP%20is%20so%20obvious%20in%20hindsight.%20Thanks%20for%20jumping%20in!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBrad%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-757145%22%20slang%3D%22en-US%22%3ERe%3A%20Can%20I%20get%20MATCH%20to%20grab%20the%20last%20identical%20value%20in%20an%20array%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-757145%22%20slang%3D%22en-US%22%3EYou%E2%80%99re%20welcome.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-757514%22%20slang%3D%22en-US%22%3ERe%3A%20Can%20I%20get%20MATCH%20to%20grab%20the%20last%20identical%20value%20in%20an%20array%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-757514%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F374823%22%20target%3D%22_blank%22%3E%40Brad_Yundt%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F280482%22%20target%3D%22_blank%22%3E%40Twifoo%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20so%20much%20to%20both%20of%20you%20for%20your%20help.%26nbsp%3B%20LOOKUP%20does%20exactly%20what%20I%20needed.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-757551%22%20slang%3D%22en-US%22%3ERe%3A%20Can%20I%20get%20MATCH%20to%20grab%20the%20last%20identical%20value%20in%20an%20array%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-757551%22%20slang%3D%22en-US%22%3EYou%E2%80%99re%20very%20much%20welcome.%20By%20the%20way%2C%20LOOKUP%20is%20my%20favorite%20function.%3C%2FLINGO-BODY%3E
CrankyPantz
New Contributor

Hi everyone - I am wresting with some MATCH challenges and would appreciate some help.  In this case, my ideal outcome would be having MATCH grab the last value in a list of duplicate values (it defaults to grabbing the first duplicate value).  I have been trying other solutions such as MMULT but cannot crack this one.

 

Below is some sample data.  I have a database of Users who progressed through a game to specific Levels at a certain Time (the exact Time they reached that Level).  Consider the Time data here to be the time in minutes since the User started playing the game.

I have other data which contains the User and an event which happened at a particular Time.  I am trying to determine what Level the User was at during that Time.  The second section shows some sample inputs for User and Time, and the desired result for Level.

The last user in the source data is put at Level 10 because the MATCH formula is grabbing that row by mistake in some cases and I wanted to easily see when that was happening.

The right columns labeled X and Y show the results using two variations of the MATCH function.  column X uses the exact match argument ("0"); column Y uses "1" as the 3rd argument in the MATCH function.  Correct answers are highlighted in green.

 

Observations:

- In column X, the wrong answers are due to MATCH grabbing the first value in a list of duplicates.

- Column Y seems clearly to be the wrong approach but it yields some correct answers vs. column X.

- Row 13 is the same data as Row 3, but column Y yields different results.  Weird.

 

I'm looking for any help to yield the right results, even different formulas or macros.

 

image.png

 

Formula in cell I2 is: {=INDEX($A$2:$C$14,MATCH(1,(E2=$A$2:$A$14)*(F2>=$C$2:$C$14),0),2)}.  I used CSE when entering this formula, and copied it down to I14.

Formula in cell J2 is {=INDEX($A$2:$C$14,MATCH(1,(E2=$A$2:$A$14)*(F2>=$C$2:$C$14),1),2)}.  Ditto here for CSE and copied to J14.

 

Thanks in advance for help on this.

7 Replies

@CrankyPantz 

When you want the last match, LOOKUP is the function to use.

 

I can reproduce your column G results with the following non-CSE formula:

=INDEX($B$2:$B$14,LOOKUP(1E+307,(ROW($A$2:$A$14)-ROW($A$2)+1)/((E2=$A$2:$A$14)*(F2>=$C$2:$C$14))))

You are probably best off not reading the on-line help for LOOKUP. It will only mislead you.

 

LOOKUP has a number of useful properties:

  1. Contrary to the on-line help, the lookup column does not need to be sorted in ascending order
  2. If you search for a value so big (either in numeric magnitude or alphabetic sort order) it will always be last, LOOKUP matches the last value of the same data type as the first parameter
  3. LOOKUP ignores error values
  4. If you give LOOKUP two parameters, it returns the last match from the second parameter. If you give it three parameters, it returns the value in the third parameter that corresponds to the match found in the second parameter.
  5. If you use LOOKUP in an array formula, it does not need to be array-entered. It's like SUMPRODUCT and AGGREGATE in that respect.

In the suggested formula, the magic happens in the second parameter of LOOKUP. The numerator is the index numbers 1 through 13, while the denominator is the results of your criteria testing.

 

The numerator of the second LOOKUP parameter returns index numbers 1 through 13. Although I could simplify the expression for the given layout, many people don't know how to change ROW($A$2:$A$14)-1 for a different worksheet layout, but have no trouble adjusting (ROW($A$2:$A$14)-ROW($A$2)+1).

 

The denominator is a Boolean expression holding the criteria for which values to include: a match for E2 in column A and F2>= values in column C. Each test returns an array of TRUE and FALSE, which get converted to 1 and 0 when used in an arithmetic expressions. Since the criteria are in the denominator, you get an array of either index number for the rows or a DIV/0! error values.

 

The first LOOKUP parameter 1E+307 is a very large number. Although the formula would work with the given layout using 14, I like to choose a number so large it will always work. Doing so gets people's attention and makes them reluctant to make changes.

 

Finally, rather than applying INDEX to $A$2:$C$14 and then specifying the result come from the second column, I just use $B$2:$B$14 and drop the third parameter in the INDEX function.

Solution

@CrankyPantz 

Your desired results can be returned by solely using LOOKUP, like this starting in Row 2: 

=LOOKUP(2,

1/((A$2:A$14=E2)*(C$2:C$14<=F2)),

B$2:B$14)

@Twifoo,

The simple LOOKUP is so obvious in hindsight. Thanks for jumping in!

 

Brad

@Brad_Yundt 

@Twifoo 

Thanks so much to both of you for your help.  LOOKUP does exactly what I needed.

You’re very much welcome. By the way, LOOKUP is my favorite function.
Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
22 Replies
flashing a white screen while open new tab
cntvertex in Discussions on
13 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies
PacketMon Components are not loading in WAC 1909
HotCakeX in Windows Admin Center on
2 Replies