Please copy the formula into this post, so I can troubleshoot it. I did test this here, and it didn't repeat the value.
To correct for the last record, change the last section to start with:
For i := 1 to unbound(x) do (
If onlastrecord or
{table.number} = next({table.number})-1 then
Y := totext(x,0,"") else //etc.
Onlastrecord will work if you are not doing this within a group section.
-LB