Having a look around this may work better but I cant seem to get the if statement syntax correct.
Could anyone help.
LET TotalRecords=NoOfRows('TableA'); //get total rows from TableA
for i=1 to $(TotalRecords) // loop through every row in TableA
if Previous(Primary)=Primary then
Result As Top3
Order By Primary;