COGNOiSe.com - The IBM Cognos Community

IBM Cognos Analytics Platform => Cognos Analytics => Reporting => Topic started by: mp3909 on 04 Dec 2020 04:22:34 AM

Title: How to write case when statement
Post by: mp3909 on 04 Dec 2020 04:22:34 AM
Cognos Version 11

I have created a crosstab in my report which is based on an underlying query that contains 2 data items - PRODUCT DESCRIPTION and VALUE.

For the VALUE field in my cross tab report, I am creating an Expression for it such that I have written this:

CASE WHEN [Business View].[ETP File].[PRODUCT_DESCRIPTION] contains 'COMMENT' THEN [Business View].[ETP File].[VALUE]

But it is not liking it.
Is there anything wrong with it?

Thank You
Title: Re: How to write case when statement
Post by: BigChris on 04 Dec 2020 05:22:50 AM
Even if you don't put an ELSE clause in you're still going to need to END the case statement

CASE
  WHEN [Business View].[ETP File].[PRODUCT_DESCRIPTION] contains 'COMMENT' THEN [Business View].[ETP File].[VALUE]
  else NULL
END
Title: Re: How to write case when statement
Post by: mp3909 on 04 Dec 2020 06:05:19 AM
Thank you BigChris.
Title: Re: How to write case when statement
Post by: MFGF on 04 Dec 2020 07:19:01 AM
A handy tip when creating expressions is to look for the (f) button - usually on the left at the bottom in the expressions dialog. This gives you access to see (and drag in) a very useful set of expressions for functions, summaries, constructs, constants etc.

If you expand the Constructs section and drag in a 'Search case' construct, this is what it puts into the expression editor:

CASE  WHEN <condition> THEN <expression> . . . [ ELSE <expression> ] END

You can then replace the relevant parts with your own items and choose to use or discard the optional parts (in square brackets)

Doing things this way - especially while you are learning the product - can help you avoid issues like this and also educate you on how to code your expressions successfully.

Cheers!

MF.