0% found this document useful (0 votes)
136 views

Cognos RS - Functions - 16

This document provides examples of date functions in Oracle, SQL Server, and IBM Cognos for calculating dates, periods of time like year-to-date, quarter-to-date, and half-year-to-date. It also includes examples of filtering by time, identifying the nth weekday of a month, and calculating age.

Uploaded by

Harry Konnect
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

Cognos RS - Functions - 16

This document provides examples of date functions in Oracle, SQL Server, and IBM Cognos for calculating dates, periods of time like year-to-date, quarter-to-date, and half-year-to-date. It also includes examples of filtering by time, identifying the nth weekday of a month, and calculating age.

Uploaded by

Harry Konnect
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Using the periodsToDate function within IBM Cognos 10 Report Studio to Calculate an OLAP

Running-Total

IBM Cognos 8 Framework Manager - Dimensional Modeling for Time Period Analysis

Oracle date functions


Get the last day of the week select trunc(sysdate, 'DAY')+6 from dual;

Get first day of year SELECT TRUNC(TO_DATE('17-DEC-2001'),'YEAR') "First Day" FROM Dual; Get
last Day of Current Month
SELECT add_months(trunc(sysdate) - (to_number(to_char(sysdate,'DD')) - 1), 1) -1 FROM dual

SQL server date functions

Get the first day of month DATEADD(dd,-(DAY(DT.DAY_DT)-1),DT.DAY_DT) Get date key select
cast(convert(varchar(8), getdate(), 112) as integer) Get last Sunday
Select cast(convert(varchar(8), dateadd(day,-datepart(weekday,dateadd(WEEK,-
1,GETDATE()))+1,dateadd(WEEK,-1,GETDATE())), 112) as integer)

Useful Query items

1. Get total reminding to go for product Category XYZ, set 0 if the reminding to go as 0

if (([Product Category] = 'XYZ') and (total ([Forecast] for [Province Id], [Product Category] ) -
total([Sales] for [Province Id], [Product Category]) > 0)) then ( total ([Forecast] for [Province Id],
[Product Category] ) - total([Sales] for [Province Id], [Product Category] )) else (0)

2. TYD, QTD and HTD based day ( Assume [Fact Current Date],[Fact Current Quarter Number] and
[Fact Current Quarter Number] )
#/* YTD at the begining of year*/#
_add_days ([End Date], -_day_of_year ([Fact Table].[Dim Time].[Fact Current Date])+1)

#/* QTD */#


if ([Fact Table].[Dim Time].[Quarter Id] = [Fact Table].[Dim Time].[Fact Current Year]*10+[Fact
Table].[Dim Time].[Fact Current Quarter Number])
then ([Fact Table].[Fact Table].[Measure])
else (0)

#/* HTD */#


If ( [Fact Table].[Dim Time].[Fact Current Quarter Number] > 2 ) Then
( If ( [Fact Table].[Dim Time].[Quarter Number] > 2) Then
( If ([Fact Table].[Dim Time].[Quarter Id] <= [Fact Table].[Dim Time].[Fact Current Year]*10+[Fact
Table].[Dim Time].[Fact Current Quarter Number])
Then [Fact Table].[Fact Table].[Measure])
else (0) )
Else ( 0) )
Else (
If ([Fact Table].[Dim Time].[Quarter Id]<=[Fact Table].[Dim Time].[Fact Current Year]*10+[Fact
Table].[Dim Time].[Fact Current Quarter Number])
then ([Fact Table].[Fact Table].[Measure])
else (0) )

Time filter
filter(
[Time Dimension].[Time Hierarchy].[Date],
roleValue('_businessKey', currentMember([Time Dimension].[Time Hierarchy])) >=
#sq(prompt('From', 'Date'))# and
roleValue('_businessKey', currentMember([Time Dimension].[Time Hierarchy])) <=
#sq(prompt('To', 'Date'))#
)

Identifying nth Weekday of a month in reports


Requirement: Identify the 2nd Thursday of the current month. Solution: Create data items ToDate,
1stDateofMonth, 1stDayofMonth, Nth, Day , AddDays, NthDay ToDate - current_date 1stDateofMonth -
_first_of_month(ToDate) 1stDayofMonth - _day_of_week(1stDateofMonth ,1) // Assuming Monday is Day 1
Nth - 2 // The nth value of the weekday required, in our case we require 2nd Thursday Day - 4 // Assuming
Monday is Day 1, then Thursday is Day 4 AddDays - case when [Day] >= [1stDayofMonth] then [Day] -
[1stDayofMonth] else ([Day] + [1stDayofMonth]) -1 end NthDay - _add_days([1stDateofMonth],(([Nth]-1) * 7
) + [AddDay]) NthDay returns the 2nd Thursday of the month.

Age calculation
We have two ways to calculate age in years and age in yy years mm months and dd days format

Calculate Age in years...

Create a query Calculation as below...

_years_between (current_date, [Birth date column])

Calculate Age in yy years, mm months and dd days format...


(replace [Birth Date] with your own Data of birth column)

substring (cast(_ymdint_between (current_date, [Birth date]),char(6)),1,2) || ' Years '||

substring (cast(_ymdint_between (current_date, [Birth date]),char(6)),3,2)

|| ' months ' ||

substring (cast(_ymdint_between (current_date, [Birth date]),char(6)),5,2)

|| ' Days '

http://cognosskills.blogspot.in/

=============

You might also like