Hi! I am trying to implement a `variable` to be a...
# gooddata-cloud
c
Hi! I am trying to implement a
variable
to be able to dynamically change the definition of some metrics based on a filter (cf exemple below). This is something I am used to us in Looker Studio (is it something working on Gooddata)?
Copy code
SELECT CASE
  WHEN $(ComparisonType) = "Previous Period" THEN
    (
      SELECT SUM({fact/revenue})
      - 
      SELECT SUM({fact/revenue}) FOR PreviousPeriod({date.period})
    ) 
    / 
    SELECT SUM({fact/revenue}) FOR PreviousPeriod({date.period})
    
  WHEN $(ComparisonType) = "Previous Year" THEN
    (
      SELECT SUM({fact/revenue})
      - 
      SELECT SUM({fact/revenue}) FOR PreviousPeriod({date.year})
    ) 
    / 
    SELECT SUM({fact/revenue}) FOR PreviousPeriod({date.year})
    
  ELSE NULL
END
j
MAQL doesn't support dynamic variable substitution like
$(ComparisonType)
or conditional logic such as
CASE WHEN
. So, we can't dynamically switch metric definitions based on a filter the way you might in Looker Studio. Instead, here are a few recommended workarounds: 1. Create separate metrics for each logic. For example, one for "% change vs previous year" and another for "% change vs previous period". You can then display both in the dashboard or choose which one to show depending on context. 2. Use dashboard sections to show different comparisons. For instance, one can show previous year metrics, another shows previous period metrics. This keeps things clear for users. 3. If you need true interactivity, you can implement a custom dashboard plugin. It can include a dropdown for users to choose the comparison type and dynamically update the displayed metric accordingly. This requires frontend development with GoodData.UI but gives you full control.
c
Thanks for the answer. That's clear