I am trying to come up with a formula which pulls data from a merged cell. Let me provide an example
If cell P12 and Q12 shows a value of 5, i would like the respective value in cell P5 to be reflected in cell M12, so cell M12 will be where my formula is. Do let me know if this is not clear, i will try to come up with a visual to provide more context.
I've tried extracting the data if Q12 = 5, however the problem I've been facing is as P5 and Q5 is merged, the data is stored in P5 and Q5 is an empty cell, thus it would not return anything.

=IFERROR(TEXTJOIN(", ", TRUE, "", IF($P12:$XFD12=5, IF($P$5:$XFD$5<>"",$P$5:$XFD$5, ""),"")), "")

This is my current formula