We can pass group name to the custom code, if the group name not changed, then Counter=Counter+1, otherwise, reset counter to 1. The custom code would like below.
dim shared Counter as integer=0
dim shared Group as String
public function getGroupCounter(GroupName as String) as Integer
If Group=GroupName Then
Then use the expression below to get the counter
If you have any questions, please feel free to ask.