If condition

Registration date
Wednesday November 26, 2014
Last seen
August 3, 2020
I need to do use IF condition for multiple cell check in a column

E.g, of my data as below . I need the answer in Column Z like if the data in column matches with below row, then Column Y data should get added with Comma like 25,23.
In below case since M appears in 3 rows of column X consecutively , then the answer required in Column Z as 25,23,21. If more data matches consecutively, then all data would get added with Comma in between .
I Used formula like If(A1=A2,(B1&","&B2),"B1)
I could use this formula only for 1st 2 cell data, pls help to do make this formula to check consecutively N number of times .

Column X Column Y Column Z( ans required)
M 25
M 23
M 21
N 10
N 1
O 5