Help needed with Multi row formula

Hi All,

I need to implement a multirow formula similar to Alteryx.

Problem:

I have my data like

I need to group by the groupIds and then calculate the positions based on next and PrevId. If Previous id is null position is 1 and then increment it based on Id column.

The result should look like

Help and a sample workflow appreciated.