Home > Articles

  • Print
  • + Share This
This chapter is from the book

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

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

Figure 1.9 Second conditional prompt page: Date Range

Figure 1.10

Figure 1.10 Second conditional prompt page: Order Method

Figure 1.11

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

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

  1. Launch Report Studio and select the GO Data Warehouse (query) package.
  2. Click on Create new option.
  3. 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.

  1. 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
  2. In the Explorer bar, mouse over the Query Explorer and select Query1.
  3. 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.

  4. 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
  5. Validate the expression, choosing any prompt values, and click OK to close the dialog box.
  6. To return to the page design, mouse over the Page Explorer tab and select Page1.
  7. Click on any whitespace in the page body to select it and click on the Center button on the toolbar.
  8. Click on the report title text to select it, and change the Source Type property from Text to Report Expression.
  9. Double-click on the Report Expression property box.

    The Report Expression dialog box opens.

  10. Create the following expression:
    'Product Line Summary for ' + ParamDisplayValue('choice')
  11. 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

  1. Mouse over Page Explorer and select the Prompt Pages folder.
  2. Drag a Page object from the Insertable Objects pane into the Prompt Pages pane.

    This will be Prompt Page1.

  3. Double-click on PromptPage1 to enter page design mode.
  4. Change the page title text to this:
    Prompt Page 1 - Select Filter Type
  5. 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.

  6. Click on the Use existing parameter radio button, and from the drop-down menu, select the choice parameter.
  7. Click on the Finish button to close the Prompt Wizard dialog box.
  8. Click on the newly created value prompt to select it.
  9. Double-click the Static Choices property in the Properties pane.

    The Static Choices dialog box is displayed.

  10. Click on the Add button in the lower-left corner of the Static Choices dialog box.

    The Edit dialog box opens.

  11. Type in the value 1 in the Use property text box and type Filter by date range in the Display property text box.
  12. 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

    Figure 1.13 Static Choices dialog box

  13. Click OK to close the Static Choices dialog box.
  14. While you still have the value prompt selected, change the Select UI property to Radio button group.
  15. Change the Auto-Submit property to Yes.
  16. Click on any whitespace in the prompt page body to select it and click on the Center button on the Toolbar.
  17. 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.

  1. Mouse over Page Explorer and select the

    Prompt Pages folder.

  2. Drag a Page object from the Insertable Objects pane into the Prompt Pages pane below Prompt Page1.

    This will be Prompt Page2.

  3. Double-click on PromptPage2 to enter prompt page design mode.
  4. Change the page title text to this:
    Prompt Page 2 - Select the Filter Value
  5. From the Toolbox tab of the Insertable Objects pane, drag the Conditional Blocks object into the prompt page body.
  6. Click on the Conditional Blocks object to select it.
  7. Double-click on the Block Variable property in the Properties pane.

    The Block Variable dialog box opens.

  8. From the Variable drop-down menu, select the <New string variable> option.

    The New Variable dialog box opens.

  9. Make the Name of the new variable choice.
  10. Click on the Add button in the lower-left corner of the dialog box.

    The Add dialog box opens.

  11. Enter the value 1 and click OK.

    We will repeat the previous steps to add two more values: 2 and 3.

  12. Click on the Add button again in the lower-left corner of the dialog box.

    The Add dialog box opens.

  13. Enter the value 2 and click OK.
  14. Click on the Add button one more time in the lower-left corner of the dialog box.

    The Add dialog box opens.

  15. Enter the value 3 and click OK.

    Your New Variable dialog box will look similar to Figure 1.14.

    Figure 1.14

    Figure 1.14 New Variable dialog box

  16. Click OK.

    The Report Expression dialog box window opens.

  17. Create the following expression:
    ParamValue('choice')
  18. Validate the expression and click OK twice to close the Report Expression and Variable dialog boxes.
  19. 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.

  20. From the Toolbox tab of the Insertable Objects pane, drag a Date prompt into the conditional block.

    The Prompt Wizard dialog box is displayed.

  21. Click on the Use existing parameter radio button, and from the drop-down menu, select the Date parameter.
  22. Click the Finish button to close the Prompt Wizard dialog box.
  23. Click on the newly inserted Date prompt control to select it.
  24. Change the Multi-Select property to No.
  25. Click on the background of the Conditional Block to select it.
  26. Change the Current Block property to 2.

    We will now design the prompt for when the users select an order method filter type.

  27. From the Toolbox tab of the Insertable Objects pane, drag a Value prompt into the conditional block.

    The Prompt Wizard dialog box opens.

  28. Select the Use existing parameter radio button and from the drop-down menu select the OrderMethod parameter.
  29. Click the Next button.
  30. Change the Name property from Query2 to OrderMethodPrompt.
  31. Set the Values to display value to [Sales (query)].[Order method].[Order method type].
  32. Click on the Finish button to close the Prompt Wizard dialog box.
  33. Click on the background of the Conditional Block to select it.
  34. Change the Current Block property to 3.

    We will now design the prompt for when the users select an employee filter type.

  35. From the Toolbox tab of the Insertable Objects pane, drag a Value prompt into the conditional block.

    The Prompt Wizard dialog box opens.

  36. Click on the Use existing parameter radio button, and from the drop-down menu, select Employee parameter.
  37. Click on the Next button.
  38. Change the Name property from Query2 to EmployeePrompt.
  39. Set the Values to display value to [Sales (query)].[Employee by region].[Employee name].
  40. 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.

  1. Mouse over Page Explorer and select the Page1 object.
  2. 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.

  3. 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.

  4. From the Value drop-down menu, select the choice variable that was created during the second prompt page design.
  5. Click OK to close the Block Variable dialog box.
  6. Change the Conditional Block property value from (Other) to 1.
  7. From the Toolbox tab of the Insertable Objects pane, drag a Layout Calculation object into the Conditional Block.

    The Report Expression dialog box opens.

  8. Click on the Parameters tab of the Available Components section of the dialog box.
  9. Drag the Date parameter into the Expression Definition area.
  10. Validate the expression and click OK to close the Report Expression dialog box.
  11. From the Toolbar, click the Center button to center the parameter expression in the report title area.
  12. Change the Current Block property value to 2.
  13. From the Toolbox tab of the Insertable Objects pane, drag a Layout Calculation object into the Conditional Block.

    The Report Expression dialog box opens.

  14. Click on the Parameters tab of the Available Components section of the dialog box.
  15. Drag the Order Method parameter into the Expression Definition area.
  16. Validate the expression and click OK to close the Report Expression dialog box.
  17. From the Toolbar, click the Center button to center the parameter expression in the report title area.
  18. Change the Current Block property value to 3.
  19. From the Toolbox tab of the Insertable Objects pane, drag a Layout Calculation object into the Conditional Block.

    The Report Expression dialog box opens.

  20. Click on the Parameters tab of the Available Components section of the dialog box.
  21. Drag the Employee parameter into the Expression Definition area.
  22. Validate the expression and click OK to close the Report Expression dialog box.
  23. 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.

  • + Share This
  • 🔖 Save To Your Account