Handling an array contining the same element multiple times

%3CLINGO-SUB%20id%3D%22lingo-sub-3296703%22%20slang%3D%22en-US%22%3EHandling%20an%20array%20contining%20the%20same%20element%20multiple%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3296703%22%20slang%3D%22en-US%22%3E%3CP%3EOne%20of%20the%20things%20(among%20quite%20a%20few)%20I%20find%20frustrating%20with%20the%20new-generation%20functions%20in%20Excel%20is%20their%20occasional%20limited%20scope.%20Two%20examples%20are%20XLOOKUP()%20and%20XMATCH%3A%20it%20took%20me%20a%20while%20to%20understand%20that%20they%20only%20return%20%3CSTRONG%3Eone%3C%2FSTRONG%3E%20result%2C%20even%20if%20more%20than%20one%20is%20present.%3C%2FP%3E%3CP%3EThe%20%3CA%20href%3D%22https%3A%2F%2Fsupport.microsoft.com%2Fen-us%2Foffice%2Fxlookup-function-b7fd680e-6d10-43e6-84f9-88eae8bf5929%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EXLOOKUP%20support%20page%3C%2FA%3E%20barely%20mentions%20that%3A%3C%2FP%3E%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3E%22The%20XLOOKUP%20function%20searches%20...%20an%20array%2C%20and%20then%20returns%20the%20item%20corresponding%20to%20the%20first%20match%20it%20finds.%22%3C%2FP%3E%3CP%20style%3D%22%20text-align%20%3A%20justify%3B%20%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20style%3D%22%20text-align%20%3A%20justify%3B%20%22%3EThe%20%3CA%20href%3D%22https%3A%2F%2Fsupport.microsoft.com%2Fen-us%2Foffice%2Fxmatch-function-d966da31-7a6b-4a13-a1c6-5a33ed6a0312%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EXMATCH%20support%20page%3C%2FA%3E%20doesn't%20even%20bother%20to%20mention%20it%3A%3C%2FP%3E%3CP%20style%3D%22%20text-align%20%3A%20justify%3B%20padding-left%20%3A%2030px%3B%20%22%3E%22The%20XMATCH%20function%20searches%20for%20a%20specified%20item%20in%20an%20array...%2C%20and%20then%20returns%20the%20item's%20relative%20position.%22%3C%2FP%3E%3CP%3EI%20would%20have%20emphasized%20that%20limitation%20with%20big%2C%20bold%20red%20letters%2C%20but%20that's%20just%20me...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20given%20that%20fact%20and%20this%20simple%20worksheet%2C%20how%20do%20I%20accomplish%20the%20following%3A%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%20image-alt%3D%22TheDub_0-1651076280133.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F367364iA8C9236654E0E2C1%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22TheDub_0-1651076280133.png%22%20alt%3D%22TheDub_0-1651076280133.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%20and%20simplest%3A%20Taking%20the%20first%20entry%20(%22John%22)%2C%20I%20would%20like%20to%20get%20the%20index%20positions%20of%20his%20row%20where%20the%20target%20is%20achieved.%20The%20answer%20in%20his%20case%20is%201%20and%203%2C%20but%2C%20true%20to%20form%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DXMATCH(target%2CB2%3AE2%2C0)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ereturns%20just%20%3CEM%3E1%3C%2FEM%3E.%3C%2FP%3E%3CP%3EHarnessing%20BYROW%20to%20get%20the%20same%20information%20for%20both%20John%20and%20Jane%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DBYROW(B2%3AE3%2CLAMBDA(myrow%2CXMATCH(target%2Cmyrow%2C0)))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ereturns%2C%20as%20expected%2C%20only%3CBR%20%2F%3E1%3CBR%20%2F%3E4%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20I%20want%20to%20get%20the%20years%20in%20which%20the%20target%20is%20achieved%2C%20for%20John%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DINDEX(B1%3AE1%2C%2CXMATCH(target%2CB2%3AE2%2C0))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ereturns%20only%202019%2C%20omitting%202021.%3CBR%20%2F%3EDoing%20the%20same%20for%20both%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DBYROW(B2%3AE3%2CLAMBDA(myrow%2CINDEX(B1%3AE1%2C%2CXMATCH(target%2Cmyrow%2C0))))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ereturns%20the%20wrong%20result%20for%20John%2C%20but%20the%20right%20result%20for%20Jane%20(since%20she%20only%20met%20the%20target%20once).%3C%2FP%3E%3CP%3ESo%20how%20do%20I%20go%20about%20getting%20an%20output%20by%20person%20and%20year%2C%20along%20these%20lines%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%20style%3D%22%20padding-left%20%3A%2030px%3B%20%22%3EJohn%2C%202019%2C%202021%3CBR%20%2F%3EJane%2C%202022%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3296703%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3298929%22%20slang%3D%22en-US%22%3ERe%3A%20Handling%20an%20array%20contining%20the%20same%20element%20multiple%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3298929%22%20slang%3D%22en-US%22%3EDo%20we%20each%20have%20a%20ro%CC%82le%20to%20play%3F%20One%20to%20shape%20the%20clouds%20and%20another%20to%20divine%20their%20significance%3F%3CBR%20%2F%3E%3CBR%20%2F%3EI%20can't%20help%20but%20feel%20that%20there%20is%20a%20change%20of%20mindset%20required%20to%20exploit%20the%20new%20functionality%20to%20best%20effect.%20Before%2C%20I%20would%20have%20been%20'authoring%20a%20formula'%20(albeit%20using%20a%20highly%20idiosyncratic%20syntax)%2C%20now%20it%20is%20more%20a%20case%20of%20writing%20a%20short%20program%20module.%20However%2C%20I%20do%20not%20like%20the%20need%20to%20resort%20to%20'thunks'%2C%20other%20than%20as%20a%20last%20resort%20to%20address%202D%20arrays%20of%20row%20vectors%20for%20example.%20A%20simple%20column%20of%20row%20vectors%20should%20just%20display%20on%20the%20worksheet%20without%20trickery.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3298790%22%20slang%3D%22en-US%22%3ERe%3A%20Handling%20an%20array%20contining%20the%20same%20element%20multiple%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3298790%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F214174%22%20target%3D%22_blank%22%3E%40Peter%20Bartholomew%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20are%20times%20reading%20through%20message%20threads%20like%20this%20when%20I%20definitely%20feel%20like%20Charlie%20Brown%20in%20this%20classic%20comic%20strip.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22mathetes_0-1651239983310.jpeg%22%20style%3D%22width%3A%20737px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F367988i644799183B1D5E31%2Fimage-dimensions%2F737x483%3Fv%3Dv2%22%20width%3D%22737%22%20height%3D%22483%22%20role%3D%22button%22%20title%3D%22mathetes_0-1651239983310.jpeg%22%20alt%3D%22mathetes_0-1651239983310.jpeg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3298532%22%20slang%3D%22en-US%22%3ERe%3A%20Handling%20an%20array%20contining%20the%20same%20element%20multiple%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3298532%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1131408%22%20target%3D%22_blank%22%3E%40TheDub%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20my%20most%20recent%20post%20on%20this%20thread%2C%20I%20had%20got%20to%20the%20point%20of%20returning%20the%20result%20for%20all%20names%20as%20a%20single%20array%20formula%20through%20the%20use%20of%20thunks.%26nbsp%3B%20The%20obvious%20shortcoming%20was%20that%20the%20number%20of%20rows%20and%20columns%20output%20was%20set%20to%202x2%20and%20required%20the%20user%20to%20modify%20the%20formula.%26nbsp%3B%20A%20version%20of%20the%20formula%20that%20extracts%20the%20size%20of%20the%20output%20from%20the%20thunk%20using%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E...%0A%20%20%20n%2C%20ROWS(achieved%CF%91)%2C%0A%20%20%20m%2C%20MAX(MAP(achieved%CF%91%2C%20Count%CE%BB))%2C%0A%20%20%20MAKEARRAY(n%2Cm%2C%20Expand%CE%BB(achieved%CF%91))%0A%0A%0Awhere%20Count%CE%BB%20refers%20to%0A%3D%20LAMBDA(x%2C%20COUNT(IF(TYPE(x)%3D128%2Cx()%2Cx)))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%26nbsp%3BCount%CE%BB%20is%20passed%20an%20array%2C%20it%20performs%20the%20count%3B%20if%20it%20is%20passed%20a%20thunk%2C%20it%20expands%20the%20thunk%20before%20counting.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3297624%22%20slang%3D%22en-US%22%3ERe%3A%20Handling%20an%20array%20contining%20the%20same%20element%20multiple%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3297624%22%20slang%3D%22en-US%22%3EA%20revision%20from%20my%20earlier%20formula%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3DBYROW(MAP(rng%2CLAMBDA(x%2CIF(ISTEXT(x)%2Cx%2CIF(x%3D2%2CINDIRECT(ADDRESS(1%2CCOLUMN(x)))%2C%22%22))))%2CLAMBDA(row%2CTEXTJOIN(%22%2C%22%2CTRUE%2Crow)))%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3297223%22%20slang%3D%22en-US%22%3ERe%3A%20Handling%20an%20array%20contining%20the%20same%20element%20multiple%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3297223%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1131408%22%20target%3D%22_blank%22%3E%40TheDub%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20feel%20slightly%20embarrassed%20to%20offer%20this%20solution%20to%20your%20problem.%26nbsp%3B%20It%20shouldn't%20be%20this%20complicated!!!%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20LET(%0A%20%20%20%20achieved%CF%91%2C%20BYROW(table%2C%0A%20%20%20%20%20%20LAMBDA(record%2C%0A%20%20%20%20%20%20%20%20Thunk%CE%BB(SORTBY(IF(record%3Dtarget%2C%20year%2C%20%22%22)%2C%20record%26lt%3B%26gt%3Btarget))))%2C%0A%20%20%20%20MAKEARRAY(2%2C2%2C%20Expand%CE%BB(achieved%CF%91))%0A%20%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Ewhere%20forming%20and%20expanding%20the%20Thunk%20is%20achieved%20by%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3EThunk%CE%BB%20%0A%3D%20LAMBDA(x%2CLAMBDA(x))%3B%0A%0AExpand%CE%BB%20%0A%3D%20LAMBDA(achieved%CF%91%2CLAMBDA(r%2Cc%2C%0A%20%20%20%20INDEX(INDEX(achieved%CF%91%2Cr%2C1)()%2Cc)))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EWhat%20it%20does%20is%20to%20replace%20the%201D%20array%20of%201D%20arrays%20by%20a%202D%20array%20of%20scalars.%26nbsp%3B%20The%20latter%20is%20legal%20whereas%20the%20former%20is%20a%20sin%20beyond%20comprehension!%26nbsp%3B%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9664%22%20target%3D%22_blank%22%3E%40JoeMcDaid%3C%2FA%3E%26nbsp%3B%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3297175%22%20slang%3D%22en-US%22%3ERe%3A%20Handling%20an%20array%20contining%20the%20same%20element%20multiple%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3297175%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20are%20absolutely%20right%20about%20running%20into%20the%20very%20dreaded%20nested%20array%20problem...%3C%2FP%3E%3CP%3EBy%20way%20of%20illustration%2C%20I%20tried%20to%20take%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1174419%22%20target%3D%22_blank%22%3E%40Quadruple_Pawn%3C%2FA%3E's%20answer%20(and%20the%20same%20goes%20for%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F428790%22%20target%3D%22_blank%22%3E%40Patrick2788%3C%2FA%3E's%20answer)%20and%20filter%20out%20the%20spaces%20from%20the%20output.%20That%20called%20for%20combining%20FILTER()%20and%20BYROW()%20this%20way%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DBYROW(answer%2CLAMBDA(myrow%2CFILTER(myrow%2Cmyrow%26lt%3B%26gt%3B%22%22)))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eor%2C%20since%20we're%20playing%20games%2C%20in%20reverse%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DFILTER(answer%2CBYROW(answer%2CLAMBDA(myrow%2Cmyrow%26lt%3B%26gt%3B%22%22)))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESure%20enough%2C%20a%20%23CALC%20error%20popped%20up%20with%20the%20message%20%22Nested%20arrays%20are%20not%20supported%22!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20going%20to%20turn%20now%20to%20playing%20with%20your%20answer%20and%20see%20what%20I%20get%2C%20but%20-%20off%20the%20bat%20-%20I%20have%20to%20say%20that%20the%20idea%20adults%20(presumably)%20like%20us%20have%20to%20resort%20to%20workarounds%20like%20%22turn%20the%20row%20result%20into%20a%20single%20concatenated%20value%22%20to%20get%20to%20the%20promised%20land%20makes%20me%20shake%20my%20head%20(or%20SMH%2C%20as%20they%20say%20nowadays....)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3297140%22%20slang%3D%22en-US%22%3ERe%3A%20Handling%20an%20array%20contining%20the%20same%20element%20multiple%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3297140%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1131408%22%20target%3D%22_blank%22%3E%40TheDub%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20problem%20you%20describe%20is%20remarkably%20difficult%20because%20you%20hit%20the%20dreaded%20nested%20arrays%20problem.%26nbsp%3B%20The%20first%20stage%20is%20reasonable%20OK.%26nbsp%3B%20Given%20a%20single%20record%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3D%20FILTER(year%2C%20record%3Dtarget)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eidentifies%20the%20years%20the%20target%20is%20achieved.%26nbsp%3B%20The%20catch%20is%20that%20as%20soon%20as%20you%20try%20to%20use%20BYROW%20to%20create%20a%202D%20output%20array%2C%20Excel%20throws%20an%20error.%26nbsp%3B%20One%20way%20of%20evading%20the%20problem%20is%20to%20turn%20the%20row%20result%20into%20a%20single%20concatenated%20value.%26nbsp%3B%20Then%20the%20BYROW%20step%20works.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3D%20BYROW(table%2C%20%0A%20%20%20%20LAMBDA(record%2C%0A%20%20%20%20%20%20LET(%0A%20%20%20%20%20%20%20%20yrs%2C%20FILTER(year%2C%20record%3Dtarget)%2C%0A%20%20%20%20%20%20%20%20TEXTJOIN(%22%2C%20%22%2C%2C%20yrs)%0A%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%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%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20214px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F367513i0B23887748052DDC%2Fimage-dimensions%2F214x104%3Fv%3Dv2%22%20width%3D%22214%22%20height%3D%22104%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3ETo%20get%20an%20array%20of%20arrays%20requires%20MAKEARRAY%20to%20expanding%20an%20inner%20Lambda%20function%20(probably%20a%20thunk%20-%20that%20is%20a%20Lambda%20function%20that%20takes%20a%20null%20parameter%20string).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3296927%22%20slang%3D%22en-US%22%3ERe%3A%20Handling%20an%20array%20contining%20the%20same%20element%20multiple%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3296927%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1131408%22%20target%3D%22_blank%22%3E%40TheDub%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Erng%3D%20A2%3AE3%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%20image-alt%3D%22Patrick2788_0-1651083294767.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F367399iBE9EA410813A3819%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Patrick2788_0-1651083294767.png%22%20alt%3D%22Patrick2788_0-1651083294767.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3296839%22%20slang%3D%22en-US%22%3ERe%3A%20Handling%20an%20array%20contining%20the%20same%20element%20multiple%20times%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3296839%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1131408%22%20target%3D%22_blank%22%3E%40TheDub%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DIF(B2%3AE3%3DB4%2CB1%3AE1%2C%22%22)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThis%20returns%20a%20similar%20output%20in%20my%20sheet.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

One of the things (among quite a few) I find frustrating with the new-generation functions in Excel is their occasional limited scope. Two examples are XLOOKUP() and XMATCH: it took me a while to understand that they only return one result, even if more than one is present.

The XLOOKUP support page barely mentions that:

"The XLOOKUP function searches ... an array, and then returns the item corresponding to the first match it finds."

 

The XMATCH support page doesn't even bother to mention it:

"The XMATCH function searches for a specified item in an array..., and then returns the item's relative position."

I would have emphasized that limitation with big, bold red letters, but that's just me...

 

So given that fact and this simple worksheet, how do I accomplish the following:

 

TheDub_0-1651076280133.png

 

First and simplest: Taking the first entry ("John"), I would like to get the index positions of his row where the target is achieved. The answer in his case is 1 and 3, but, true to form

 

=XMATCH(target,B2:E2,0)

 

returns just 1.

Harnessing BYROW to get the same information for both John and Jane

 

=BYROW(B2:E3,LAMBDA(myrow,XMATCH(target,myrow,0)))

 

returns, as expected, only
1
4

 

If I want to get the years in which the target is achieved, for John

 

=INDEX(B1:E1,,XMATCH(target,B2:E2,0))

 

returns only 2019, omitting 2021.
Doing the same for both

 

=BYROW(B2:E3,LAMBDA(myrow,INDEX(B1:E1,,XMATCH(target,myrow,0))))

 

returns the wrong result for John, but the right result for Jane (since she only met the target once).

So how do I go about getting an output by person and year, along these lines:

 

John, 2019, 2021
Jane, 2022

 

Thanks.

9 Replies

@TheDub 

=IF(B2:E3=B4,B1:E1,"")

This returns a similar output in my sheet. 

@TheDub 

The problem you describe is remarkably difficult because you hit the dreaded nested arrays problem.  The first stage is reasonable OK.  Given a single record, 

 

 

= FILTER(year, record=target)

 

 

identifies the years the target is achieved.  The catch is that as soon as you try to use BYROW to create a 2D output array, Excel throws an error.  One way of evading the problem is to turn the row result into a single concatenated value.  Then the BYROW step works.

 

 

= BYROW(table, 
    LAMBDA(record,
      LET(
        yrs, FILTER(year, record=target),
        TEXTJOIN(", ",, yrs)
      )
    )
  )

 

 

image.png

To get an array of arrays requires MAKEARRAY to expanding an inner Lambda function (probably a thunk - that is a Lambda function that takes a null parameter string).

You are absolutely right about running into the very dreaded nested array problem...

By way of illustration, I tried to take @Quadruple_Pawn's answer (and the same goes for @Patrick2788's answer) and filter out the spaces from the output. That called for combining FILTER() and BYROW() this way

 

=BYROW(answer,LAMBDA(myrow,FILTER(myrow,myrow<>"")))

 

or, since we're playing games, in reverse:

 

=FILTER(answer,BYROW(answer,LAMBDA(myrow,myrow<>"")))

 

Sure enough, a #CALC error popped up with the message "Nested arrays are not supported"!

 

I'm going to turn now to playing with your answer and see what I get, but - off the bat - I have to say that the idea adults (presumably) like us have to resort to workarounds like "turn the row result into a single concatenated value" to get to the promised land makes me shake my head (or SMH, as they say nowadays....)

@TheDub 

I feel slightly embarrassed to offer this solution to your problem.  It shouldn't be this complicated!!!

= LET(
    achievedϑ, BYROW(table,
      LAMBDA(record,
        Thunkλ(SORTBY(IF(record=target, year, ""), record<>target)))),
    MAKEARRAY(2,2, Expandλ(achievedϑ))
  )

where forming and expanding the Thunk is achieved by

Thunkλ 
= LAMBDA(x,LAMBDA(x));

Expandλ 
= LAMBDA(achievedϑ,LAMBDA(r,c,
    INDEX(INDEX(achievedϑ,r,1)(),c)))

What it does is to replace the 1D array of 1D arrays by a 2D array of scalars.  The latter is legal whereas the former is a sin beyond comprehension!  @JoeMcDaid ?

 

A revision from my earlier formula:

=BYROW(MAP(rng,LAMBDA(x,IF(ISTEXT(x),x,IF(x=2,INDIRECT(ADDRESS(1,COLUMN(x))),"")))),LAMBDA(row,TEXTJOIN(",",TRUE,row)))

@TheDub 

In my most recent post on this thread, I had got to the point of returning the result for all names as a single array formula through the use of thunks.  The obvious shortcoming was that the number of rows and columns output was set to 2x2 and required the user to modify the formula.  A version of the formula that extracts the size of the output from the thunk using

 

...
   n, ROWS(achievedϑ),
   m, MAX(MAP(achievedϑ, Countλ)),
   MAKEARRAY(n,m, Expandλ(achievedϑ))


where Countλ refers to
= LAMBDA(x, COUNT(IF(TYPE(x)=128,x(),x)))

 

If Countλ is passed an array, it performs the count; if it is passed a thunk, it expands the thunk before counting.

@Peter Bartholomew 

 

There are times reading through message threads like this when I definitely feel like Charlie Brown in this classic comic strip.

mathetes_0-1651239983310.jpeg

 

Do we each have a rôle to play? One to shape the clouds and another to divine their significance?

I can't help but feel that there is a change of mindset required to exploit the new functionality to best effect. Before, I would have been 'authoring a formula' (albeit using a highly idiosyncratic syntax), now it is more a case of writing a short program module. However, I do not like the need to resort to 'thunks', other than as a last resort to address 2D arrays of row vectors for example. A simple column of row vectors should just display on the worksheet without trickery.