Hi team, We're struggling with the time macros in ...
# gooddata-cloud
e
Hi team, We're struggling with the time macros in MAQL queries. We have a metric that just adds a date shift to another metric, like: Metric B =
metric/metric_a FOR PREVIOUS(date_object.year, 1)
It's been working well when using it with date_object filter only, until we wanted to use it in a visualization that has multiple date objects. It seems like
FOR PREVIOUS
doesn't support shifting multiple date objects in a metric definition. Is there a way to work around this problem and have a metric that shifts any date filter applied to it by X? If not in a single metric definition, what would you suggest to get the expected behavior? Let me know if you need more details or have any questions. I'm happy to share examples if the problem is not clear. Thanks in advance!
k
You're correct that the MAQL function
FOR PREVIOUS
(and similarly,
FOR PREVIOUSPERIOD
) only operates on a single specified date attribute at a time. When your metric is used in a visualization with multiple date attributes (date objects), MAQL does not support dynamically shifting all of them within a single metric definition. The function will only apply to the date attribute you specify, and there is no built-in way to have it automatically shift any date filter applied to the metric by X periods for all possible date attributes. Key points from the documentation: -
FOR PREVIOUS
strictly follows the date attribute specified in its parentheses and does not adapt to other date attributes present in the visualization or filters. It cannot shift multiple date objects at once—only the one you specify in the metric definition [[FOR Previous](https://www.gooddata.com/docs/cloud/create-metrics/maql/time/for-previous/)]. -
FOR PREVIOUSPERIOD
adapts to the most granular date attribute present in the visualization, but still only works with one date dimension at a time, and you must specify which date dimension to use. It does not shift all date filters applied to the metric [[Functions For Referring to Past Time Periods](https://www.gooddata.com/docs/cloud/create-metrics/maql/time/functions-for-referring-to-past-time-periods/)]. - There is no MAQL function or macro that can generically shift "any" date filter applied to a metric by X periods across multiple date attributes. Workarounds and Recommendations: - Create separate metrics for each date attribute you want to shift. For example, if you have multiple date objects (e.g.,
date_object_1
,
date_object_2
), you would need to define separate metrics for each, each using
FOR PREVIOUS(date_object_X, 1)
. - Design your visualizations to use only one date attribute at a time if you want to leverage the shifting behavior in a straightforward way. - If you need to support user-driven selection of which date attribute to shift, you may need to create multiple metrics and allow users to select between them in the dashboard or via dashboard plugins (custom UI logic), but this requires additional setup and possibly frontend development [[Community Slack Example](https://gooddataconnect.slack.com/archives/C04S1MSLEAW/p1732710010389509?thread_ts=1732710010.389509&cid=C04S1MSLEAW)]. Summary: There is currently no way in MAQL to define a single metric that will shift any and all date filters applied to it by X periods. The shifting must be explicitly tied to a specific date attribute in the metric definition. The best practice is to create separate metrics for each date attribute you need to shift, or to structure your reports to use a single date attribute for time-shifting metrics. If you have a more complex use case or need dynamic behavior, please provide more details or examples, and I can help suggest a more tailored approach.