i have this formula,
=INDEX($A$21:$A$8517,MATCH(2,1/(MMULT((COUNTIF(OFFSET('MAIN DATA'!B21:B8517,(ROW(INDIRECT("1:"&ROWS('MAIN DATA'!B21:B8517)-B5+1))-1)+{-1,0,1},0,(B5+1)),B3)=(B5+1)+{-1,0,-1})+0,{1;1;1})=3),1)+(B5+1)-1))
is there a way to make the formula count the occurrence instead of indexing