Hi Anya,
Is keep_grains the ONLY way currently to make a field dependent on which fields are included in the query?
Metrics group on all dimensions included in the query by default. keep_grains allows you to enforce that a metric is only used with selected dimensions, ignoring grouping on other dimensions. There is also of_all, which ignores grouping on the specified dimensions instead.
I assume you have already know about this but mentioned to be clear. Based on your use case, though, they may not be the most applicable options?
have a ceiling and floor applied, but those ceiling and floors need to be different depending on the granularity that we’re analyzing.
When you say ceiling and floor, do you mean restricting the granularity itself (for example, preventing analysis at certain grains), or applying a greatest / least calculation with different values?
Assuming it is the latter (based on your other question), are the values static for each selected grain, or are they metrics calculated at another granularity?
If the values are static, you could handle this with is_at_level, for example:
case(
when: is_at_level(orders.created_at | year()),
then: sql_number(
'GREATEST',
sql_number('LEAST', revenue, 50000),
0
),
when: is_at_level(orders.created_at | month()),
then: sql_number(
'GREATEST',
sql_number('LEAST', revenue, 3000),
10
),
when: is_at_level(orders.created_at | day()),
then: sql_number(
'GREATEST',
sql_number('LEAST', revenue, 2330),
20
),
else: 1
)
Swapping those static numbers to other metrics should also be straightforward, but it will depend on your use cases.