I see, then Dan's code should work. You can also use the lag function, so for you example data Subject Counts could be:
If(Lag(:Subj, 1) != :Subj | Is Missing(Lag(:Subj, 1)), 1, Lag(:Subj Counts, 1) + 1)
and visit counts could be:
If(Lag(:Subj, 1) != :Subj | Is Missing(Lag(:Subj, 1)) | Lag(:visit, 1) != :visit, 1, Lag(:Visit Counts, 1) + 1)
This still relies on sorting by subj and visit though.