- Highlight Selected Text
- Avoiding Query Macros
- Complex Crosstab Calculations
- Summary
Avoiding Query Macros
In class, we teach students that query macros can be written to take advantage of the IBM Cognos BI capability to dynamically change the report queries at runtime. Query macros can be built to integrate information about the report consumer or to enhance the queries that are written. Unfortunately, the query macros are not easy to master because the documentation exists only in the IBM Cognos Framework Manager documentation. In our example, we will show how the solution to the common business problem can be resolved without resorting to the use of query macros.
The sales team wants to be able to quickly analyze how each retailer region is performing according to product quantities sold, revenue produced, and gross profit achieved. The sales team would like to be able to filter the results in one of three ways: by selected date, by selected order method, or by selected employee.
The team would like one report where they can select the type of filter and the value to include for the filter and see the results in a crosstab so that they can quickly compare the numbers between the products and the regions. The final prompt result we are trying to achieve should look similar to Figure 1.8.
Figure 1.8 Main prompt page
After the prompt is selected, the report will run with a completely different filter option, and in case the prompt triggers another parameter, a new corresponding prompt will show up for the user to make the final selection.
Different second-page prompts are illustrated in Figures 1.9, 1.10, and 1.11.
Figure 1.9 Second conditional prompt page: Date Range
Figure 1.10 Second conditional prompt page: Order Method
Figure 1.11 Second conditional prompt page: Employee
Figure 1.12 illustrates the final report that is desired (we are assuming an Order method prompt selection and a specific order method selection).
Figure 1.12 Final report
Design
We have discovered that macro code syntax help is not readily available for IBM Cognos Report Studio developers. It is covered extensively in IBM Cognos Framework Manager training and materials but not in IBM Cognos Report Studio user guides.
We have to change the syntax of the filter expression depending on what the user selects in the prompt at runtime.
The reason we cannot just pass the filter expression as a parameter value is that it will be treated by the report engine as a text value and not as “code” that has to be used as a filter expression.
For this reason, the expression is captured in a prompt macro function. However, writing the macro function or finding out what the correct syntax is may be difficult for novice report developers who may not have access to the IBM Cognos Framework Manager developers who can help them.
The solution we are proposing in this example will completely avoid the use of macros, and it will be easy for business users to understand and replicate.
Our sample report will be a crosstab report that will be filtered in one of the three ways that users select at runtime. The options will be by date range, by order method, or by sales rep.
Step-by-Step
We will start with a simple crosstab template.
Step 1: Start the Report
- Launch Report Studio and select the GO Data Warehouse (query) package.
- Click on Create new option.
- Select the Crosstab report template and click OK.
We will be using the Sales (query) namespace inside the Sales and Marketing (query) folder (same as for the previous example).
Step 2: Set Up the Crosstab and the Query
When it comes to creating reports, there are two approaches. As in this example, you can build the query and the report layout at the same time. Our first example provides an approach where you can build the query first and then create the report layout.
- From the Source tab of the Insertable Objects pane, drag the following query items into the Crosstab:
- Product Line from the Products query subject to the Rows drop zone
- Quantity, Revenue, and Gross profit from the Sales fact query subject to the Columns drop zone
- In the Explorer bar, mouse over the Query Explorer and select Query1.
- From the Toolbox tab of the Insertable Objects pane, drag the Filter object into the Detail Filters pane.
The Detail Filters Expression box pops up.
- Create the following filter expression:
CASE ?choice? WHEN 1 THEN ([Sales (query)].[Time].[Date] in_range ?Date?) WHEN 2 THEN ([Sales (query)].[Order method].[Order method code] = ?OrderMethod?) ELSE ([Sales (query)].[Employee by region].[Employee key] = ?Employee?) END
- Validate the expression, choosing any prompt values, and click OK to close the dialog box.
- To return to the page design, mouse over the Page Explorer tab and select Page1.
- Click on any whitespace in the page body to select it and click on the Center button on the toolbar.
- Click on the report title text to select it, and change the Source Type property from Text to Report Expression.
- Double-click on the Report Expression property box.
The Report Expression dialog box opens.
- Create the following expression:
'Product Line Summary for ' + ParamDisplayValue('choice')
- Validate the expression, choosing any prompt values, and click OK.
Our crosstab report setup is complete at this point.
Step 3: Set Up the First Prompt Page
- Mouse over Page Explorer and select the Prompt Pages folder.
- Drag a Page object from the Insertable Objects pane into the Prompt Pages pane.
This will be Prompt Page1.
- Double-click on PromptPage1 to enter page design mode.
- Change the page title text to this:
Prompt Page 1 - Select Filter Type
- From the Toolbox tab of the Insertable Objects pane, drag a Value Prompt object into the page body of the prompt page.
The Prompt Wizard dialog box opens.
- Click on the Use existing parameter radio button, and from the drop-down menu, select the choice parameter.
- Click on the Finish button to close the Prompt Wizard dialog box.
- Click on the newly created value prompt to select it.
- Double-click the Static Choices property in the Properties pane.
The Static Choices dialog box is displayed.
- Click on the Add button in the lower-left corner of the Static Choices dialog box.
The Edit dialog box opens.
- Type in the value 1 in the Use property text box and type Filter by date range in the Display property text box.
- Repeat the preceding step to add two more static values:
- Use: 2 and Display: Filter by order method
- Use: 3 and Display: Filter by sales rep
Your Static Choices dialog box should look similar to Figure 1.13.
Figure 1.13 Static Choices dialog box
- Click OK to close the Static Choices dialog box.
- While you still have the value prompt selected, change the Select UI property to Radio button group.
- Change the Auto-Submit property to Yes.
- Click on any whitespace in the prompt page body to select it and click on the Center button on the Toolbar.
- Click on the page footer (which has all the prompt buttons) and Delete it.
We do not need the buttons because the prompt control we built on this page has the auto-submit property turned on, and it will submit the selections as soon as the user clicks on one of the radio buttons.
We have completed the first prompt page.
Step 4: Set Up the Second Prompt Page
The prompt controls shown on the second prompt page will depend on the selections made in the first page.
- Mouse over Page Explorer and select the
Prompt Pages folder.
- Drag a Page object from the Insertable Objects pane into the Prompt Pages pane below Prompt Page1.
This will be Prompt Page2.
- Double-click on PromptPage2 to enter prompt page design mode.
- Change the page title text to this:
Prompt Page 2 - Select the Filter Value
- From the Toolbox tab of the Insertable Objects pane, drag the Conditional Blocks object into the prompt page body.
- Click on the Conditional Blocks object to select it.
- Double-click on the Block Variable property in the Properties pane.
The Block Variable dialog box opens.
- From the Variable drop-down menu, select the <New string variable> option.
The New Variable dialog box opens.
- Make the Name of the new variable choice.
- Click on the Add button in the lower-left corner of the dialog box.
The Add dialog box opens.
- Enter the value 1 and click OK.
We will repeat the previous steps to add two more values: 2 and 3.
- Click on the Add button again in the lower-left corner of the dialog box.
The Add dialog box opens.
- Enter the value 2 and click OK.
- Click on the Add button one more time in the lower-left corner of the dialog box.
The Add dialog box opens.
- Enter the value 3 and click OK.
Your New Variable dialog box will look similar to Figure 1.14.
Figure 1.14 New Variable dialog box
- Click OK.
The Report Expression dialog box window opens.
- Create the following expression:
ParamValue('choice')
- Validate the expression and click OK twice to close the Report Expression and Variable dialog boxes.
- Change the Current Block property value from (Other) to 1.
We will now design the prompt for when the users select a date range filter type.
- From the Toolbox tab of the Insertable Objects pane, drag a Date prompt into the conditional block.
The Prompt Wizard dialog box is displayed.
- Click on the Use existing parameter radio button, and from the drop-down menu, select the Date parameter.
- Click the Finish button to close the Prompt Wizard dialog box.
- Click on the newly inserted Date prompt control to select it.
- Change the Multi-Select property to No.
- Click on the background of the Conditional Block to select it.
- Change the Current Block property to 2.
We will now design the prompt for when the users select an order method filter type.
- From the Toolbox tab of the Insertable Objects pane, drag a Value prompt into the conditional block.
The Prompt Wizard dialog box opens.
- Select the Use existing parameter radio button and from the drop-down menu select the OrderMethod parameter.
- Click the Next button.
- Change the Name property from Query2 to OrderMethodPrompt.
- Set the Values to display value to [Sales (query)].[Order method].[Order method type].
- Click on the Finish button to close the Prompt Wizard dialog box.
- Click on the background of the Conditional Block to select it.
- Change the Current Block property to 3.
We will now design the prompt for when the users select an employee filter type.
- From the Toolbox tab of the Insertable Objects pane, drag a Value prompt into the conditional block.
The Prompt Wizard dialog box opens.
- Click on the Use existing parameter radio button, and from the drop-down menu, select Employee parameter.
- Click on the Next button.
- Change the Name property from Query2 to EmployeePrompt.
- Set the Values to display value to [Sales (query)].[Employee by region].[Employee name].
- Click on the Finish button to close the Prompt Wizard dialog box.
Our second prompt page design is finished at this point. It can be enhanced by the addition of text boxes for additional instructions to the users, or prompt controls can be modified to have a different UI.
You can now test the report prompt page flow.
Step 5: Adjust the Report Title to Show the Selected Prompt Value
The objective is to dynamically display the prompt selections that the user selected at runtime.
- Mouse over Page Explorer and select the Page1 object.
- From the Toolbox tab of the Insertable Objects pane, drag the Conditional Blocks object into the page header area below the report title block.
We will reuse the variable we created for the purposes of the second prompt page.
- Click on the Conditional Block object we have just inserted, and double-click on the Block Variable property in the Properties pane.
The Block Variable dialog box opens.
- From the Value drop-down menu, select the choice variable that was created during the second prompt page design.
- Click OK to close the Block Variable dialog box.
- Change the Conditional Block property value from (Other) to 1.
- From the Toolbox tab of the Insertable Objects pane, drag a Layout Calculation object into the Conditional Block.
The Report Expression dialog box opens.
- Click on the Parameters tab of the Available Components section of the dialog box.
- Drag the Date parameter into the Expression Definition area.
- Validate the expression and click OK to close the Report Expression dialog box.
- From the Toolbar, click the Center button to center the parameter expression in the report title area.
- Change the Current Block property value to 2.
- From the Toolbox tab of the Insertable Objects pane, drag a Layout Calculation object into the Conditional Block.
The Report Expression dialog box opens.
- Click on the Parameters tab of the Available Components section of the dialog box.
- Drag the Order Method parameter into the Expression Definition area.
- Validate the expression and click OK to close the Report Expression dialog box.
- From the Toolbar, click the Center button to center the parameter expression in the report title area.
- Change the Current Block property value to 3.
- From the Toolbox tab of the Insertable Objects pane, drag a Layout Calculation object into the Conditional Block.
The Report Expression dialog box opens.
- Click on the Parameters tab of the Available Components section of the dialog box.
- Drag the Employee parameter into the Expression Definition area.
- Validate the expression and click OK to close the Report Expression dialog box.
- From the toolbar, click the Center button to center the parameter expression in the report title area.
Our report design is completed for this example.
We have achieved the desired result by using a conditional block in a second prompt page, which will show different second prompt options depending on the selections made on the first prompt page.
Furthermore, we have integrated the dynamic filter logic without the use of macro functions in order to demonstrate that novice report developers can be quite capable of creating very complex dynamic prompts and filter reports without advanced macro programming knowledge.
Finally, we have shown you how to dynamically control what will be shown in the title of the report based on the user’s selections during the prompt process.