probably can't group by this formula.
CR will only group on a formula if it can evaluate the formula in one read of the data, with no other dependencies.
for example, if you have a date, you can group on the day of week of the date, or if you have 2 columns, CR can contenate the columns or subtract the columns or check if a value is contained in the columns...or maybe, if the previous or next record have some value, but it needs the formula to be answered without having to read outside of the current record.
At least that is what the rule seems to be.
I am guessing that you are expecting multiple values to display...
If so, I would expect the code to look something like:
output := output + ", " + ToText(ref);
HTH