Cognos Analytics

Cognos Analytics

Connect, learn, and share with thousands of IBM Cognos Analytics users! 

 View Only
Expand all | Collapse all

Creating a Function in a Cognos Report

  • 1.  Creating a Function in a Cognos Report

    Posted Tue March 01, 2022 11:02 AM
    Good Morning,

    I hope that you and yours are all doing well. I would like to create a function that will be used by multiple queries within the report. The function should input 3 parameters and generate 1 result. The user defined function would be programmed with built-in functions and constructs. How would I create the user defined function within the report?

    Warm Regards,
    Eli

    #CognosAnalyticswithWatson


  • 2.  RE: Creating a Function in a Cognos Report

    Posted Tue March 01, 2022 11:34 AM
    Hi Eli,

    I don't know that it is possible to create a function in a report. The only pssible way I know is to create the function on the databse layer and import into Framework Manager to publish it into the package.
    See https://www.ibm.com/support/pages/how-use-database-user-defined-functions-framework-manager  for an example.


    ------------------------------
    Robert Dostal
    Team Leader BI
    GEMÜ
    Ingelfingen
    ------------------------------



  • 3.  RE: Creating a Function in a Cognos Report

    Posted Tue March 01, 2022 03:45 PM
    ...I want to pass one data item value from the data model and two parameter values from the report to produce the result in a calculated query data item.  Thanks; However, I am uncertain of what you reference apply to this problem.

    Warm Regards,
    Eli





  • 4.  RE: Creating a Function in a Cognos Report

    Posted Tue March 01, 2022 11:40 AM
    Hi Eli,

    I believe that you can only create a stored procedure query subject (see also using prompts with a stored procedure in the documentation) in Framework Manager.

    Correct me if I'm wrong but I think you cannot add a stored procedure with Data Module or with Report.

    Best regards,

    ------------------------------
    Patrick Neveu
    Positive Thinking Company
    ------------------------------



  • 5.  RE: Creating a Function in a Cognos Report

    Posted Tue March 01, 2022 03:42 PM
    ...I want to pass one data item value from the data model and two parameter values from the report to produce the result in a calculated query data item.  Thanks; However, I am uncertain of what you reference apply to this problem.

    Warm Regards,
    Eli





  • 6.  RE: Creating a Function in a Cognos Report

    Posted Tue March 01, 2022 04:14 PM

    Hi,

    As the very knowledgeable people above have indicated, there isn't really an easy way to define a function like this within an individual report. I guess you could add a query containing a query calculation, and then allow cross-products in the report so that you can reference it from any other query, but it would be messy and probably very inefficient.

    Other options (as suggested above) would be:
    1. Code the function within a database, and import it into a Framework Manager model as a UDF. You could define a standalone calculation within the model that references it, and then include that standalone calculation as part of your package, making it available for use in reports.
    2. Create a stored procedure in a database to return the result you are looking for. Import it in Framework Manager - it would then appear as a query subject. You could join this to the rest of your data and publish it in a package. You would then have the ability to use the results in reports.

    A third option would be simply to define a standalone calculation in your Framework Manager model that does what you need, created using the Cognos expression editor. Publish this in your package so it's then available for use in reports.

    There may be other options, but those are the main ones I can think of right now.

    Cheers!

    MF.



    ------------------------------
    Mark Fry
    Technical Consultant
    ------------------------------



  • 7.  RE: Creating a Function in a Cognos Report

    Posted Tue March 01, 2022 04:19 PM
    ...The report has a prompt page.  Whatever value that is selected by the user, can it be passed to the function?





  • 8.  RE: Creating a Function in a Cognos Report

    Posted Tue March 01, 2022 05:26 PM
    Hi Eli,

    From my original message, you will use prompt(s) with a stored procedure (with Framework Manager). You will be able to read more information in:
    https://www.ibm.com/docs/en/SSEP7J_11.2.0/pdf/ug_fm.pdf
    in page 68 (or 78 in the PDF).

    Best regards,

    ------------------------------
    Patrick Neveu
    Positive Thinking Company
    ------------------------------



  • 9.  RE: Creating a Function in a Cognos Report

    Posted Wed March 02, 2022 11:18 AM
    Good Morning,

    ...Thanks.  Does your solution require that the stored procedure be written in the database?  If so, we have a second problem.  Our Framework Manager is not importing stored procedures.  We have already created an IBM support trouble ticket:

    Continued Regards,
    Eli





  • 10.  RE: Creating a Function in a Cognos Report

    Posted Wed March 02, 2022 02:36 AM
    Hi Eli,
    maybe this helps to clarify one of the approaches.
    I've created two queries in a new report
    "local function" contains one column from a table and a calculation which combines a prompt macro with the data. It's just an example and can also be used with a stored procedure.
    On the "data" query" allow cross joins:
    Then you can create a new data item there referencing the calculation from the other query:

    Run the report and you will be prompted for "input". This will then be calculated by the function. In my test 1.5 x Quantity x Unit Cost


    ------------------------------
    Robert Dostal
    Team Leader BI
    GEMÜ
    Ingelfingen
    ------------------------------



  • 11.  RE: Creating a Function in a Cognos Report

    Posted Wed March 02, 2022 10:50 AM
    Good Morning,

    ...I understand what you are doing.  It really helps to minimize code duplication.  Thanks.  However, I need a  parameter passed into the function.  Hypothetically simiar to this:
    [local function].[Cost function]([Quantity])
    or
    Cost_Function([Quantity])
    instead of,
    [local function].[Cost function] * [Quantity]

    It is appears that [local function].[Cost function] generates a result that is later multiplied by [Quantity].

    Continued Regards,
    Eli





  • 12.  RE: Creating a Function in a Cognos Report

    Posted Wed March 02, 2022 02:55 AM
      |   view attached
    Hi Eli,

    another way of doing calculations by using the input from query information and parameters is using the java script interface api with custom controls.
    One difference of this approach compared to stored procedures is the way how you will visualize the output in the report.
    The output of the custom control can be a div object inside the report or you can pass it over to another Cognos parameter.
    An example on how you can work with Cognos parameter and custom controls is in the attached example.
    There are other examples available showing how to pass over query data to a custom control and with the combination of both you can manage your function.

    best regards
    Kai

    ------------------------------
    Kai Zeller
    ------------------------------

    Attachment(s)



  • 13.  RE: Creating a Function in a Cognos Report

    Posted Wed March 02, 2022 10:21 AM

    Good  Morning,

    ...Thanks.  However, I would be using a function within a query calculation.  In mathematics, a function from a set X to a set Y assigns to each element of X exactly one element of Y. The set X is called the domain of the function and the set Y is called the codomain of the function. Functions were originally the idealization of how a varying quantity depends on another quantity.  Here is a simple example of a  text function:
    Syntax:
    upper (string_exp)
    Examples:
    upper ('Auriga') - Returns the result: AURIGA
    upper ('DeEr') - Returns the result: DEER

    That's what I mean by function.  Here are several types of Cognos functions:

    Continued Regards,
    Eli






  • 14.  RE: Creating a Function in a Cognos Report

    Posted Wed March 02, 2022 10:30 AM
    Eli,

    The title from your original message is "Creating a Function in a Cognos Report". We gave you some answers for this question.

    Best regards,

    ------------------------------
    Patrick Neveu
    Positive Thinking Company
    ------------------------------



  • 15.  RE: Creating a Function in a Cognos Report

    Posted Wed March 02, 2022 11:26 AM
    ...Thanks; I appreciate the engagement.  However, I attempted to relay details that hinder the implimentation of those recommendations.  If you do not understand anything that I previously wrote, please do not hesitate to ask further.

    Continued Regards,
    Eli





  • 16.  RE: Creating a Function in a Cognos Report

    Posted Wed March 02, 2022 10:50 AM
    Edited by System Admin Fri January 20, 2023 04:27 PM

    Hi Eli,
    as we already wrote a user-defined and reusable function is not possible, but you can use custom controls or a stored procedure.
    The setup I used in my example is not really reusable as it's locally defined in the report and limited to your data and doesn't act like a real function. Try to modify it like you need it.

    I can only guess you want some complex calculation in your report so then create this calculation there if you have all needed data in your datasource.



    ------------------------------
    Robert Dostal
    Team Leader BI
    GEMÜ
    Ingelfingen
    ------------------------------



  • 17.  RE: Creating a Function in a Cognos Report

    Posted Wed March 02, 2022 11:32 AM
    Just another thought:
    Even if it's not a function you may name, you can create very complex calculations in reports. I assume you want to pass data to a function/calculation like name_of_the_local_function( [Data] ) and this shoud do something to the data or calculate a result (set).
    If it depends on data values if-then-else or a case statement might also be helpful to test.

    Me just guessing....

    ------------------------------
    Robert Dostal
    Team Leader BI
    GEMÜ
    Ingelfingen
    ------------------------------



  • 18.  RE: Creating a Function in a Cognos Report

    Posted Thu March 10, 2022 12:22 AM

    For persons using CA in a self managed environment (i.e. on-prem), can create user defined functions that are run in a query

    https://www.ibm.com/docs/en/SSEP7J_11.0.0/com.ibm.swg.ba.cognos.dg_dqe.doc/dg_dqe.pdf

    If a UDF is defined as an in-line-SQL expression, then it may be generated in-line within the SQL requests sent to a database vendor.

    If a UDF is defined in a JAR file, then it will be executed locally on the CA data tier.



    ------------------------------
    NIGEL CAMPBELL
    ------------------------------