We have similar code in ga-measure. It might be worth to create a service for such transformations. To be done in separate PR for sure.