Hi again, I have a new question. This time, I’m wo...
# gooddata-platform
t
Hi again, I have a new question. This time, I’m wondering about working with
FOR PreviousPeriod
in combination with a time filter on the board. E.g. I want to create a report that shows a metric on a monthly basis, next to the same value 12 months ago. As soon as I limit this board to show the current 12 months, the metrics doesn’t pull the 12M comparison in anymore. How can I change this?
f
Hi Thomas, I’ve been trying to reproduce this behavior, but it is actually working as expected on my end. For example, I have a
Revenue
metric. So I built a simple PreviousPeriod version of it, like so:
Copy code
SELECT Revenue FOR PreviousPeriod(Year (Date))
I added it to an Insight, trending by Date(Month) (see the first screenshot), and added this insight to a Dashboard. Even when I filter the dashboard to limit it to a single year, it will still show the PreviousPeriod value. That’s how it should work, since PreviousPeriod actually calculates based on the current filtered date values - so it shouldn’t matter if, for example, you are limited to a single year on the Dashboard filter - you’ll still see values from the previous year, but the data point is still from the current year, if that makes sense. Would you show me in a bit more detail how you are reaching the behavior you described?
t
your second screenshot actually shows the problem: Jan 21 has no value for the previousperiod metric, even though on the full screen it has a value.
👀 1
f
OK I understand now! The explanation is in the article Functions For Referring to Past Time Periods:
On the other hand, FOR PreviousPeriod( ) adapts to the context of the report at hand. Regardless of the date attribute specified by the metric author, the PreviousPeriod( ) function takes on the date attribute of the smallest granularity from the report.
So if you use FOR PreviousPeriod(Year) on your metric, for example, but slice it by Month, the metric will actually show the value for the previous Month (the new smallest granularity), not the previous year. That seems to cause the offset values on the graph. So to show the previous year in an insight with the Month granularity, the metric would actually have to be:
Copy code
SELECT Revenue FOR PreviousPeriod(Year (Date),12)
Which, in the context of the insight/dashboard, ends up being the same as:
Copy code
SELECT Revenue FOR Previous(Month/Year (Date),12)
Since both of those end up giving you the same value, I would recommend using the second example as a solution, since it’s simpler to understand - that will fix your time frame and ensure that the values are both correct and aligned in the final insight. Please give it a shot and let me know if it works out better!
t
thanks, will give that a try!
🙌 1
Previous instead of Previous period worked.
What’s not clear is, why did Previous period on month granularity with monthly granularity on the insight and a monthly filter NOT work?
f
I believe it is caused by how PreviousPeriod pulls the context from the insight - if there’s a cutoff on the Date filter on the dashboard, the metric does not have the previous period’s value to calculate (e.g.: if the year is only 2023, January 2023 can’t be calculated because there’s no data for December 2022 for it to use as a comparison). This behavior is consistent on my end. In opposition to that, FOR Previous calculates it regardless of how it is limited on the filter - hence why using it avoids the offset datapoint effect. Ultimately, PreviousPeriod is designed only for comparing against the period immediately before the current datapoint - it isn’t suitable for checking for multiple periods.
t
well then its basically not suitable for comparisons with cutoff dates, at least for the cases where you need it to be complete (as in your example). Good to know. It just means that we need to build multiple metrics when we want to compare over different granularities. not a big problem though
1