also copy/paste/modified from https://github.com/dimagi/commcare-hq/blob/master/corehq/apps/reports/generic.py - should find a way to reuse