I would suggest making this kind of thing a server-side formatter using the values of the DB query in a 'Set Value' step (set to output). Doing it in a client-side formatter can end up executing many, many times (if the values are in a repeat) causing a slow-down on the page. If you want to do it client-side, you could initiate it from a Run Javascript within a flow - trigger the flow on form/server connect on success. That way it runs just once when the form/SC completes.