I think the Allocated Memory problem happens when you create a circular reference or a scenario that approximates an outer join. Have you checked your model to make sure that "agent" isn't defined more than once and/or isn't part of a messy complex join?
Maybe change the initial query such that it will handle the <NV> logic? Something like:
case when Agent != '<NV>' then Agent else '********' end as Agent
That way at least you get rid of the nested IF. You could also take this opportunity to rename the column if it is part of a messy join.