Business Objects Enterprise: Create a Dynamic List of Date Ranges

Add a dynamic, date-range parameter to a report which includes the most recent 12 months, plus Crystal Reports’ Month-to-Date and Year-to-Date ranges.

Date-Range Parameter

Objective

Give the report

Instructions

Create the Sql Server Components

A Sql Server user-defined function (see Sql Server: DateRanges Function) will dynamically create the values that are needed for the this project. This function needs a Sql Server view (see Sql Server: GetNow View) to generate the system’s current date/time.

Create Data Connection

  1. Open the Business View Manager and log in.
  2. Create a new Data Connection.
  3. Configure the Data Connection to attach to the appropriate Sql Server database.
  4. Save the Data Connection as ‘Sql Server DC’.

Create a Data Foundation

  1. In the Business View Manager, create a Data Foundation.
  2. Select ‘Sql Server DC’ as the Data Connection.
  3. Save the Data Foundation as ‘Sql Server DF’.

Create a Business View

  1. In the Business View Manager, create a Business View.
  2. Save the Business View as ‘Sql Server BV’.

Create a Command Object

  1. Create a Command Object in the Data Foundation. Add the following text to the Command Object:
  2. SELECT *
    FROM dbo.DateRanges()
    ORDER BY Sequence DESC

  3. Save the Command Object as ‘DateRanges’.

Create Command Object

Data Foundation

Create a Business Element

  1. Create a new Business Element in the Business View Manager.
  2. Add the Description and Value fields from the ‘DateRanges’ Command Object.
  3. Save the Business Element as ‘DateRanges’.

Business Element

Add Business Element to Business View

  1. Open the Business View.
  2. From the ‘Insert’ menu, select ‘Insert Business Elements…’.
  3. Locate and select the ‘DateRanges’ Business Element, click ‘Add’, then ‘Close’.

Business View

Create the List of Values

  1. Create a new List of Value in the Business View Manager.
  2. Select the desired Business View from the ‘Select Business View’ window.
  3. Enter ‘Date Range LoV’ in the ‘Name’ textbox.
  4. Expand the ‘DateRanges’ Business Element in the ‘Available Fields’ list.
  5. Select ‘Value’, then click the ‘>’ button.
  6. Check ‘Sort by Value in Descending Order’.
  7. Choose ‘Description’ from the ‘Description Field’ dropdown list.
  8. Click ‘OK’ to save the List of Values.

Create List of Values

Examine Repository

Open the Repository Explorer in the Business View Manager. The list of objects for the Date Range Parameter should resemble this list:

Repository Explorer

Create the Dynamic Parameter

  1. Create a new report using Crystal Reports XI.
  2. Connect to the Repository that contains the ‘Sql Server BV’ Business View.
  3. Create a new parameter, named ‘Date Range’. Leave the ‘Prompt Group Text’ textfield blank.
  4. Select ‘Dynamic’ from the ‘List of Values’ dropdown list.
  5. Select the Existing radio button and choose ‘Date Range LoV’ from the list.
  6. Click ‘Create Parameter’ in the ‘Parameters’ column of the ‘Choose a Data Source’ group.
  7. Set ‘Prompt Text’ to ‘Choose Date Range’.
  8. Set ‘Sort Order’ to ‘Descending by Value’.
  9. Set ‘Prompt With Description Only’ to ‘True’.
  10. Click ‘OK’ to save the parameter.

Create Parameter

Modify the Report

  1. Add the CDateTime Custom Function to the report (see Crystal Reports: CDateTime).
  2. Add a Custom Function named ‘getDateRange’ (see below) to the report.
  3. Edit the Record Selection Formula. Add the following code:
    //set the database field to the value that is extracted from the 'Date Range' parameter
    {table.date_field} IN getDateRange({?Date Range})
  4. Add a formula field to display the date range. Add the following code to the formula:
    CStr(Minimum(getDateRange ({?Date Range}))) + " - " + CStr(Maximum(getDateRange ({?Date Range})))
    Add the formula field to the report.

Summary

When the report is run, the parameter form will resemble:

Date-Range Parameter

Dynamic Date-Range Parameter Sample

getDateRange Custom Function

'--------------------------------------------------------------------------------
'Author: Craig Buchanan;craig.buchanan@cogniza.com
'Purpose: Convert a string value into its equivalent Date Range
'Parameters: text - a string in the format index;startDate:endDate
' or index;NamedDateRange
' e.g. 1;1/1/2006:12/31/2006 or 2;MonthToDate
'Returns: Date Range
'--------------------------------------------------------------------------------
Function getDateRange (text as string) As Date Range
If Instr(text,";")=0 Then Exit Function
Dim value as string: value = Split(text,";")(2)
If Instr(value,":")=0 Then
getDateRange = CDateRange (value)
Else
Dim startDate as Date: startDate = Cdate(Split(value,":")(1))
dim endDate as Date: endDate = cdate(Split(value,":")(2))
getDateRange = startDate to endDate
End If
End Function

4 thoughts on “Business Objects Enterprise: Create a Dynamic List of Date Ranges

  1. satpreet

    After reviewing the information with our resource we came to the same conclusion that you would need to create a formula using a IF statement or CASE statement. However, this would not be at runtime and would require the information first from the database.

    The way you have currently designed the report is better because the information is at least processed from the database first then bringing into Crystal Reports.

    Satpreet D.
    Technical Product Specialist, Technical Support
    Business Objects

  2. Tyson Roberts

    I have implemented the above-outlined solution and it works very well (thank you).

    The only problem I am having is that when reports are scheduled they seem to retain the date range from the first instance. Any recommendations?

    Best,
    Tyson

  3. Tyson

    In the following SQL:

    SELECT *
    FROM dbo.DateRanges()
    ORDER BY Sequence DESC

    What does the “()” indicate. We currently are referenceing the SQL table that contains the Date Range Values here. Is this correct?

    Thanks,

    Tyson

  4. Craig Buchanan

    It indicates that dbo.DateRanges is a function. This function returns a table of date-range values.

Leave a Reply

Your email address will not be published. Required fields are marked *