You are on page 1of 10

IBM Cognos Proven Practices: Dynamic Selection of Columns in IBM Report Studio

Product(s): IBM Cognos Report Studio; Area of Interest: Reporting


Skill Level: Introductory

16 Jun 2011 How to build a Report Studio report that allows the user to select from a prompt which columns to include in the report.

Dynamic Selection of Columns in IBM Report Studio Copyright IBM Corporation 2011

Trademarks Page 1 of 10

developerWorks

ibm.com/developerWorks

Introduction
Purpose This technique demonstrates how to create a report that allows a user to dynamically select columns in an IBM Cognos Report Studio report. The effect will be a report that will mimic an IBM Cognos Query Studio report. Applicability The technique in this document should only be used against relational datasources. The technique identified in the document was validated using IBM Cognos 8.4.24.78 and IBM Cognos 10 using the GO Sales (Query) package shipped with the samples. It should be noted that references to the query items in this document may not match with other version of the samples. Its always best follow the technique to build your own report rather than copying code from the document.

Creating the Report


Creating the base report To create a base report, 1. 2. 3. 4. 5. Open IBM Cognos Report Studio against the GO Sales (Query) package. Select create a New report. When prompted, select List Object. From the Insertable Objects pane using the source tab, expand the Sales (query) namespace. From the Product query subject drag Product Line, Product type, Product name and Product description onto the list report and from the Sales query subject drag Unit cost onto the list.

Dynamic Selection of Columns in IBM Report Studio Copyright IBM Corporation 2011

Trademarks Page 2 of 10

ibm.com/developerWorks

developerWorks

Figure 1: The base list report in Report Studio showing columns for Product line, Product type, Product name, Product description and Unit cost

Creating the Prompt Page 1. 2. 3. 4. 5. Use the View menu or Page Explorer to switch to the Prompt Pages view. Drag the Page object from the Insertable Objects pane onto the middle of the Prompt Pages canvas. Double-click on the object Prompt Page1. From the Insertable Objects pane, locate and drag the Value Prompt object onto the Prompt page to evoke the Prompt Wizard dialog box. Within the Prompt Wizard dialog box, give the parameter a name and then click Finish. The prompt will have only static choices for the selection of the columns so there is no need for the prompt to be associated to a query. For this example the parameter name will be col_selc_prt. In the Properties pane for the Value Prompt under the General category, change the Multi-Select property to Yes and the Select UI property to Check box group.

6.

Dynamic Selection of Columns in IBM Report Studio Copyright IBM Corporation 2011

Trademarks Page 3 of 10

developerWorks

ibm.com/developerWorks

Figure 2: The value prompt with the Multi-Select property set to Yes and the Select UI property set to Check box group

7.

Add static Use and Display values to the Value prompt. Each of these values will be checkbox item on the prompt page. In this example, the static Use and Display choices for the first 4 columns in the report will be set as follows,
Use value C1 C2 C3 C4 Display value Product Line Product Type Product Product Description

Dynamic Selection of Columns in IBM Report Studio Copyright IBM Corporation 2011

Trademarks Page 4 of 10

ibm.com/developerWorks

developerWorks

Figure 3: The static choices to use for the value prompt

Making the Query Items Dynamic 1. 2. Use Page Explorer to switch to Page1. Double click on the Product Line query subject and insert the following calculation:

case when ('C1' in (#promptmany('col_selc_prt','varchar')#)) then([Sales (query)].[Product].[Product line]) else('dummy') end

3.

Insert the same calculation on all of the remaining query items with the exception of the query item Unit cost. The value in the case statement

Dynamic Selection of Columns in IBM Report Studio Copyright IBM Corporation 2011

Trademarks Page 5 of 10

developerWorks

ibm.com/developerWorks

C1 is replaced with the corresponding Use value from the static choices entered earlier and the value in the then portion matches the data item being modified. 4. Now create the render variables to show and hide the columns on the user choice. To do this, click on the Product Line column then clicking on the List Column ancestor after clicking on the up arrow beside the List Column Body element on the header of the Properties pane. Figure 4: List of elements in the sample report that can be selected as an ancestor for the List Column Body

5.

In the Properties pane, click on the Render Variable entry. In the Render Variable dialog that appears, select the down arrow on the list box and select the item <New boolean variable> Enter the variable name var_Pline and click OK. In the Report Expression window enter the following and click OK.

6. 7.

ParamValue('col_selc_prt') contains 'C1'

Dynamic Selection of Columns in IBM Report Studio Copyright IBM Corporation 2011

Trademarks Page 6 of 10

ibm.com/developerWorks

developerWorks

Figure 5: Report Expression for Render Variable

8. 9.

Repeat step 7 to create the Render Variables for all other query items using values C2 through C4 and corresponding variable names. Run the report and select the desired columns from the prompt page by clicking on the checkbox beside the column. Figure 6: Checkbox prompt with choices selected

In this instance, the Product Line, Product and Product Description items were checked and only these columns along with the Unit cost column will appear in the report.

Dynamic Selection of Columns in IBM Report Studio Copyright IBM Corporation 2011

Trademarks Page 7 of 10

developerWorks

ibm.com/developerWorks

Figure 7: Sample report output showing only those columns selected on the prompt page and the column for Unit cost

Note that the Product Type column is not displayed in the report. You can enhance the report by adding a calculation to count the number of records returned in the result set and display it in the report output. The steps to do this are outlined below. 10. Use Query Explorer to switch to query of the list, which is Query1. 11. From the Insertable Objects pane, drag a Data Item object into the Data Items pane and insert the following calculation,
count(count(1 for [Product line], [Product type], [Product], [Product description]) for report)

The generic calculation is count(count(1 for <all the dimension columns in the sequence in which they are placed in Report Page>) for report). 12. Rename the Data Item to Record Count. 13. Go back to the report page by clicking on Page Explorer and associate the report page to the list query by selecting the page body and then using the ancestor button choose Page. In the Properties window under the Data section change the entry Query to reference the list query which is Query1.

Dynamic Selection of Columns in IBM Report Studio Copyright IBM Corporation 2011

Trademarks Page 8 of 10

ibm.com/developerWorks

developerWorks

Figure 8: Properties dialog for the Page object showing Query1 associated to the Query property

14. From the Insertable Objects pane, in the toolbox tab drag a Text Item object into the header of the report and when prompted enter the text Record Count :. 15. Drag the Record Count data item onto the header of the report page beside the Text Item inserted in Step 14. Figure 9: Sample report page that will show the record count in the report header

16. Run the report and select the desired columns from the prompt page by clicking on the checkbox.

Dynamic Selection of Columns in IBM Report Studio Copyright IBM Corporation 2011

Trademarks Page 9 of 10

developerWorks

ibm.com/developerWorks

Figure 10: Checkbox prompt with choices selected

In this instance only the Product Line and Product columns have been selected and only these columns along with the Unit cost column will appear in the report. In addition, the record count is displayed in the report header and for this report the number of records is 21. Figure 11: Sample report output showing only those columns selected on the prompt page, a column for the Unit cost and giving the record count in the report header

Dynamic Selection of Columns in IBM Report Studio Copyright IBM Corporation 2011

Trademarks Page 10 of 10