Reputation: 77
I want to create a calculation that adds the dimensions current month and previous month to a Cognos Data Module. The Month format is 2022/11. This is what I tried. I do not get an error message, but the calculation does not return a result.
Case
when (Month_Adj = #timestampMask(_add_months($current_timestamp,0),'yyyy')+'/'+timestampMask(_add_months($current_timestamp,0),'mm')#) then 'Last Month'
when (Month_Adj = #timestampMask(_add_months($current_timestamp,-1),'yyyy')+'/'+timestampMask(_add_months($current_timestamp,-1),'mm')#) then 'Previous Month'
else null
end
Please find a screenshot for reference.
Upvotes: 0
Views: 719
Reputation: 326
The Cognos relative time filters allow you to create a set of predefined relative time measures without too much mucking about.
If you want to have calculations referencing two or more relative time measures, either from the same fact or from different fact tables, you would need to create stand alone calculations and make sure that the calculate after aggregation flag is on.
I think the trickiest part is getting the lookup reference in the fact table set up.
Here are two expressions which could be used as patterns for your lookup references.
The first one is suitable for converting key values in the 202211 format into Year-month date data types.
cast ( substring( cast( MONTH_KEY as varchar(8)) ,1,4 ) || '-' + substring( cast( MONTH_KEY as varchar(8)) ,5,2 )||'-01' , date)
The second one is suitable for converting key values in the 20221128 format into year-month-day date data types.
cast ( substring( cast( SHIP_DAY_KEY as varchar(10)) ,1,4 ) || '-' + substring( cast( SHIP_DAY_KEY as varchar(10)) ,5,2 )|| '-' || substring( cast( SHIP_DAY_KEY as varchar(10)) ,7,2 ) , date)
If the key values are more abstract you would need to find a way to generate values which would map to those of the relative time filters.
Here is a link to an overview of relative time. https://www.ibm.com/docs/en/cognos-analytics/11.2.0?topic=analysis-sample-calendars
Here's how to customize them:
https://www.ibm.com/docs/en/cognos-analytics/11.2.0?topic=analysis-creating-relative-date-filters
https://www.ibm.com/docs/en/cognos-analytics/11.2.0?topic=calendars-creating-custom-retail-calendar
https://pmsquare.com/analytics-blog/2020/1/10/creating-custom-calendars-in-cognos-analytics-111
https://senturus.com/blog/how-to-customize-relative-time-in-cognos-data-modules/
In 11.2.3 They have implemented where clause support for relative time, which makes queries with them a bit more efficient as you are not doing a bunch of local processing schlepping through the query results looking for the values which fit into the filters as the query will now be filtered to only have those results you want.
Upvotes: 1
Reputation: 3089
You are comparing a decimal result from your macro to a character value in your data. You don't have syntax errors because SQL implicitly casts the decimal value for the comparison. But the values are unlikely to match.
Using today's date, your macro code...
#timestampMask(_add_months($current_timestamp,0),'yyyy')+'/'+timestampMask(_add_months($current_timestamp,0),'mm')#
...should produce...
CAST(2022 AS DOUBLE PRECISION) / 11
So the resulting SQL is effectively...
Case
when (Month_Adj = 183.818181812) then 'Last Month'
when (Month_Adj = 202.2) then 'Previous Month'
else null
end
Use the sq()
function to put quotes around the values returned by the macro functions before concatenating the three parts of the expression.
#sq(timestampMask(_add_months($current_timestamp, 0),'yyyy'))# + '/' + #sq(timestampMask(_add_months($current_timestamp, 0),'mm'))#
You can see the SQL that Cognos is producing by...
Upvotes: 1