Appying formula in case statement

Hi ,
I have a query which is i have to 2 columns say revenue, grossmargin. I need to apply a formula on
gross margin such as if revenue is less than zero the gross margin=0. I tried to write in case statement on GrossMargin as below
if Revenue > 0 then GrossMargin
else GrossMargin='0' end.
The above code throws error.
Can anyone pls suggest solution?.
Regards,
Venki.

Try with this:
CASE
WHEN revenue > 0 THEN GrossMargin
ELSE 1=1 THEN '0'
END
This sentence means that if revenue > 0 then return value of GrossMargin, if not evalue 1=1 (it's always true) and then return '0'.
Regards.

Similar Messages

  • Using Case statement instead of IF in formula fields

    Hi,
    Can I use Case statement instead of IF in formula fields?If yes,please specify the syntax.
    regards,
    sunitha

    Hi Sunitha,
    You can surely use case statement instead of the If statement. The syntax for the case statement is :
    Select [1 To n]
      Case "Condition a", "Condition b":
               "c"
      Case "Condition d","Condition e":
                "f"
      Default:
    Hope this helps you!
    Regards,
    Anindita

  • Pushing case statement applied in answers formula to Database

    Hi All,
    There are three prompts, for which there is a kind of header report with narrative view, now when user selects value in prompt element1 and do not select anything in prompt element 2 and 3, in the header only element1 should be displayed and emlement2,3 and few other elements should be nullified, to do this, i used presentation variables for element2,3 directly in narrative view. but for other non-prompt elements not to show up, i used case statement like:
    case when length(@{presntation_var}) > 0 then "display.column" else NULL end
    Now OBIEE is executing in such a way that, it is retrieving all the columns from the database and then applying logical case statement at BI server level, I need to push this case statement to DB level, can anyone help me how this could be accomplished.
    Thanks,
    Sreekanth Jala

    Hi
    why cant u use the condition in RPD.
    In rpd use the case condition and make that as a one logical column. Test=" Fact"."Service Revenue" / "Fact"."Hours Budget". make it as a logical sum.
    Then bring this column in to answerside, and use the case condition
    case when
    "Fact"."Location" <> 'X'
    then
    (" Fact""Test")
    else 
    "Fact"."Hourly Rate"
    end
    Regards,
    VG

  • SQL Expression Field - Combine Declared Variable With Case Statement

    Hello All, I have been using Crystal & Business Objects for a few months now and have figured out quite a bit on my own. This is the first real time I have struggled with something and while I could do this as a Formula Field I would like to know how to do this as a SQL Expression. Basically I want to create a SQL Expression that uses a CASE statement but I wanted to make the code a little more efficient and employ a variable to hold a string and then use the variable in the CASE statement. The expression editor accepts the CASE statement OK but I don't know how to declare the variable. Please assist with the syntax?
    This is what I have:
    CASE
       WHEN u201CDatabaseu201D.u201DFieldu201D = u2018Hu2019 THEN u2018Hedgeu2019
       WHEN u201CDatabaseu201D.u201DFieldu201D = u2018Pu2019 THEN u2018PVIu2019
       ELSE u2018Noneu2019
    END
    This is what I want:
    DECLARE strVar AS VARCHAR(25)
    strVar =  u201CDatabaseu201D.u201DFieldu201D
    CASE
       WHEN strVar = u2018Hu2019 THEN u2018Hedgeu2019
       WHEN strVar = u2018Pu2019 THEN u2018PVIu2019
       ELSE u2018Noneu2019
    END

    Hi Todd,
    Please use the following for loop; your problem will be solved.
    Local StringVar str := "";
    Local NumberVar strLen := Length ({Database.Field});
    Local NumberVar i;
    For i := 1 To strLen Do
           if {Database.Field} <i> = "H" then str := "Hedge"
            else if {Database.Field} <i> = "P" then str := "PVI"
            else str := "None"; exit for
    str
    Let me know once done!
    Thank you,
    Ashok

  • User input prompt in CASE statement

    I would like to obtain user input via a prompt and use what the user selects in my OBIEE Report case statement.
    For example &START_DATE AND &END_DATE are user inputs:
    CASE when SHIP_DATE BETWEEN &START_DATE AND &END_DATE THEN 1
    Thanks

    Use presentation variables for start and end_dates
    In the criteria tab->SHIP_DATE->click on fx
    on Edit Column Formula->Bin Tab->
    Add Bin->Add button->Variable->Presentation
    Use the presenation variables with with required condition
    Okay then set 1 at Edit Bin Name
    Mark if helps
    Srini VEERAVALLI on Feb 7, 2013 5:23 PM
    Any updates on this?
    Edited by: Srini VEERAVALLI on Feb 20, 2013 4:10 PM

  • Problem to identify values in a case statement.

    Hi Friends,
    Total number of records in my report should be divided by 5 and to be alloted into 5 grades in Grade column.
    This is what I have done so far in my report with following layout.
    S.No - Col A - Col B - Col C - Grade ( Colums A,B,C will be hidden in Report)
    *****Column C will have only 5 values at max ( 0.0, 0.2, 0.4, 0.6, 0.8).The below example is for 6 records in a report.The same will be applied for 7,8,9,10 records.
    In column A: MAX(cast(RCOUNT(1) AS DOUBLE))/5 ( Ex : 6/5 = 1.2)
    In Column B: Truncate(MAX(cast (RCOUNT(1) as double)/5), 0) ( Ex : 6/5 = 1.0)
    In Column C: Col A- Col B ( EX:1.2 - 1.0 =0.2)
    (MAX(cast(RCOUNT(1) AS DOUBLE))/5 - Truncate(MAX(cast (RCOUNT(1) as double)/5), 0))
    Now In Grade coulum I want to use this column C to Grade the records with case statements in it.
    I am trying to use the following case statement for Grade Coulmn
    Case
    when (MAX(cast(RCOUNT(1) AS DOUBLE))/5 - Truncate(MAX(cast (RCOUNT(1) as double)/5), 0)) = 0.0 then .........
    when (MAX(cast(RCOUNT(1) AS DOUBLE))/5 - Truncate(MAX(cast (RCOUNT(1) as double)/5), 0)) = 0.2 then .............
    when (MAX(cast(RCOUNT(1) AS DOUBLE))/5 - Truncate(MAX(cast (RCOUNT(1) as double)/5), 0)) = 0.4 then .............
    when (MAX(cast(RCOUNT(1) AS DOUBLE))/5 - Truncate(MAX(cast (RCOUNT(1) as double)/5), 0)) = 0.6 then ........
    when (MAX(cast(RCOUNT(1) AS DOUBLE))/5 - Truncate(MAX(cast (RCOUNT(1) as double)/5), 0)) = 0.8 then .....
    end
    Case statement works gud for 0.0 but not for other 4 values.
    It is unable to identify other 4values.
    Please tell me, how can I make case statement work for all values in above scenario.
    Thanks in Advance,
    Varsha.
    Edited by: Varsha on Nov 28, 2010 6:23 PM

    In column A: MAX(cast(RCOUNT(1) AS DOUBLE))/5 ( Ex : 6/5 = 1.2)
    In Column B: Truncate(MAX(cast (RCOUNT(1) as double)/5), 0) ( Ex : 6/5 = 1.0)
    In Column C: Col A- Col B ( EX:1.2 - 1.0 =0.2)
    (MAX(cast(RCOUNT(1) AS DOUBLE))/5 - Truncate(MAX(cast (RCOUNT(1) as double)/5), 0))Change the formula a bit by adding the by dim value for the MAX column.
    Column A: MAX(cast(RCOUNT(1) AS DOUBLE) by DimTable.Column)/5
    Column B: Truncate(MAX(cast (RCOUNT(1) as double) by DimTable.Column)/5, 0)
    Column C: MAX(cast(RCOUNT(1) AS DOUBLE) by DimTable.Column)/5 - Truncate(MAX(cast (RCOUNT(1) as double) by DimTable.Column)/5, 0)
    now write a case statement on top column c, based on your logic every 5th record will have a value 0.0
    Note: Dimtable.column is the table.column for which the row count is taken.
    Thanks,
    Vino

  • CASE Statement not working.

    I have following case statement in formula to display a specific image if the value from a column is a certain color:
    CASE "Facts"."Indicator" WHEN 'Green' THEN '<img src="res/s_blafp/images/green_image.gif" title="Green" </>' WHEN 'Yellow' THEN '<img src="res/s_blafp/images/yellow_image.gif" title="Yellow" </>' WHEN 'Red' THEN '<img src="res/s_blafp/images/red_image.gif" title="Red" </>' END
    So far when I use the case statement it only dislpays the following instead of the actual image.
    <img src="res/s_blafp/images/green_image.gif" title="Green" </>'

    shaolin_obiee wrote:
    I have following case statement in formula to display a specific image if the value from a column is a certain color:
    CASE "Facts"."Indicator" WHEN 'Green' THEN '<img src="res/s_blafp/images/green_image.gif" title="Green" </>' WHEN 'Yellow' THEN '<img src="res/s_blafp/images/yellow_image.gif" title="Yellow" </>' WHEN 'Red' THEN '<img src="res/s_blafp/images/red_image.gif" title="Red" </>' END
    So far when I use the case statement it only dislpays the following instead of the actual image.
    <img src="res/s_blafp/images/green_image.gif" title="Green" </>'Take a look at this link:
    http://gerardnico.com/wiki/dat/obiee/image

  • Issue with referencing Presentation variables(Date cols) in Case statement

    Hi,
    I m trying to reference presentation variables created on a Date column in the case statement of one of the request's columns as shown below:
    case when "- Quote Details"."Creation Date" between '@{from}{1999-1-1}' and '@{until}{2999-1-1}' then "- Quote Metrics"."# of Quotes" end
    I m getting a view display error with the below error message:
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 17001] Oracle Error code: 1861, message: ORA-01861: literal does not match format string at OCI call OCIStmtExecute
    SQL Issued: SELECT "- Quote Types"."Level2 Name" saw_0, case when "- Quote Details"."Creation Date" between '4/1/2009' and '4/15/2009' then "- Quote Metrics"."# of Quotes" end saw_1 FROM "(Unity) Quotes Catalog" WHERE "- Quote Details"."Creation Date" BETWEEN timestamp '2009-04-01 00:00:00' AND timestamp '2009-04-15 00:00:00' ORDER BY saw_0
    When I created the prompt on the date field I put the following formula in the prompt:
    cast("- Time".Day as DATE)
    When I apply a filter on the request using these variables it works fine.
    If anyone has faced this kind of issue earlier and have a resolution, please help me out.
    Thanks,
    Kartik

    Thanks Goran!!
    I've created prompts by selecting columns from the logical tables (i.e. I've selected 2 different cols of datatype DATE...inorder to assign 2 presentation variables).
    Yes I m using calendar widget for prompts.
    I haven't included any filter in my report. I m trying to display a metric when it satisfies the case statement.
    I will defintley go through the blogs you mentioned for better understanding.
    Thanks,
    Kartik
    Query fired on physical db:
    WITH
    SAWITH0 AS (select D1.c1 as c1,
    D1.c2 as c2,
    D1.c3 as c3,
    D1.c4 as c4,
    D1.c5 as c5
    from
    (select T192898.QLS_FIRST_ACAP_DT as c1,
    TO_DATE('3/2/2009','mm/dd/yyyy') as c2,
    TO_DATE('4/29/2009','mm/dd/yyyy') as c3,
    T188666.LEVEL2_NAME as c4,
    T188666.ID as c5,
    ROW_NUMBER() OVER (PARTITION BY T188666.ID, T192898.QLS_FIRST_ACAP_DT ORDER BY T188666.ID ASC, T192898.QLS_FIRST_ACAP_DT ASC) as c6
    from
    XXSLS_GBL_QTE_TYPES_V T188666,
    XXSLS_GBL_QTE_DTL_F T192898
    where ( T188666.ID = T192898.QUOTE_TYPE_ID )
    ) D1
    where ( D1.c6 = 1 ) ),
    SAWITH1 AS (select D1.c1 as c1,
    D1.c2 as c2,
    D1.c3 as c3
    from
    (select count(distinct T192898.QUOTE_HEADER_ID) as c1,
    T188666.LEVEL2_NAME as c2,
    T188666.ID as c3,
    ROW_NUMBER() OVER (PARTITION BY T188666.ID ORDER BY T188666.ID ASC) as c4
    from
    XXSLS_GBL_QTE_TYPES_V T188666,
    XXSLS_GBL_QTE_DTL_F T192898
    where ( T188666.ID = T192898.QUOTE_TYPE_ID )
    group by T188666.ID, T188666.LEVEL2_NAME
    ) D1
    where ( D1.c4 = 1 ) ),
    SAWITH2 AS (select D1.c1 as c1,
    D1.c2 as c2,
    D1.c3 as c3
    from
    (select case when SAWITH1.c2 is not null then SAWITH1.c2 when SAWITH0.c4 is not null then SAWITH0.c4 end as c1,
    case when SAWITH0.c1 between SAWITH0.c2 and SAWITH0.c3 then SAWITH1.c1 else NULL end as c2,
    case when SAWITH0.c5 is not null then SAWITH0.c5 when SAWITH1.c3 is not null then SAWITH1.c3 end as c3,
    ROW_NUMBER() OVER (PARTITION BY case when SAWITH0.c5 is not null then SAWITH0.c5 when SAWITH1.c3 is not null then SAWITH1.c3 end , case when SAWITH1.c2 is not null then SAWITH1.c2 when SAWITH0.c4 is not null then SAWITH0.c4 end ORDER BY case when SAWITH0.c5 is not null then SAWITH0.c5 when SAWITH1.c3 is not null then SAWITH1.c3 end ASC, case when SAWITH1.c2 is not null then SAWITH1.c2 when SAWITH0.c4 is not null then SAWITH0.c4 end ASC) as c4
    from
    SAWITH0 full outer join SAWITH1 On SAWITH0.c5 = SAWITH1.c3
    ) D1
    where ( D1.c4 = 1 ) )
    select SAWITH2.c1 as c1,
    SAWITH2.c2 as c2
    from
    SAWITH2
    order by c1

  • Case statement for dashboard text title based on NQ_SESSION.ROLES

    Hello,
    I figured out on my dashboard I can add a textbox
    with the code '@{biserver.variables['NQ_SESSION.ROLES']}'  and it shows the user groups. That works great. Now
    I need to have a case statement or so that shows a text, that changes
    with each user role i.e. each user role gets its own "report title" Is that possible?
    Thanks.

    Hello,
    Thanks for the column formula example.
    Unfortunately in both cases it resolves to the ELSE clause even when the other two are true:
    case  when Locate('BIConsumer_Two', VALUEOF(NQ_SESSION.ROLES))>0 then @{VAR_REPORTTITLE}{'test'} when Locate('BIConsumer_One', VALUEOF(NQ_SESSION.ROLES))>0 then @{VAR_REPORTTITLE}{'test2'}   else @{VAR_REPORTTITLE}{'test3'}   end  
    case when Position('userrole1' in VALUEOF(NQ_SESSION.ROLES))>0 then 'w00t' else 'ang' end
    The one below works (at least I can read test1 on the results page of my answers report:
    case when Position('mytestrole' in 'mytestrole')>0 then @{VAR_REPORTTITLE}{'test1'} else @{VAR_REPORTTITLE}{'test'} end
    The NQ_SESSION.ROLES comes like this:
    'BIConsumer_One;BIAdministrator;AuthenticatedUser;BIConsumer;BIConsumer_Two;BIAuthor'
    I realized why I could not resolve it. I did not define it as a session but presentation variable. So here it is a session variable:
    case when Position('mytestrole' in 'mytestrole')>0 then VALUEOF(NQ_SESSION.VAR_REPORTTITLE) ='test1' else VALUEOF(NQ_SESSION.VAR_REPORTTITLE)='test2' end
    but now there is an error: [nQSError: 26012]

  • Case statement error OBIEE

    Hi All,
    I'm getting an error with my Case statement in OBIEE. Below is the statement.
    case when @{PV_RELEASE_NAME} = 'January' then "Script Details"."Calendar Date" is between ('1/1/2012','1/31/2012') else"Script Details"."Calendar Date" end
    Please advice. Thanks for your help in advance!

    Thanks for the response , I tried with the formula u mention still I am getting the same error ..Please find the error below
    case when @{PV_RELEASE_NAME} = 'January' then "Script Details"."Calendar Date" || " is between ('1/1/2012','1/31/2012')" else"Script Details"."Calendar Date" end
    Formula syntax is invalid.
    [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 27005] Unresolved column: " is between ('1/1/2012','1/31/2012')". (HY000)
    SQL Issued: SELECT case when 0 = 'January' then "Script Details"."Calendar Date" || " is between ('1/1/2012','1/31/2012')" else"Script Details"."Calendar Date" end FROM "Test Script - Ad Hoc"
    If I use the below formula I am getting syntax error
    case when @{PV_RELEASE_NAME} = 'January' then "Script Details"."Calendar Date" is between ('1/1/2012','1/31/2012') else"Script Details"."Calendar Date" end
    Formula syntax is invalid.
    [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 27002] Near : Syntax error [nQSError: 26012] . (HY000)
    SQL Issued: SELECT case when 0 = 'January' then "Script Details"."Calendar Date" is between ('1/1/2012','1/31/2012') else"Script Details"."Calendar Date" end FROM "Test Script - Ad Hoc"

  • Excel..case statement prob..urgent..plz

    Hi,
    In our RPD we are using Excel as datasource..my problem is when Create a column in BMM layer with case statement and even in Answers Formula section also I am getting aerror in Answers like..
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: 37000 code: -3100 message: [Microsoft][ODBC Excel Driver] Syntax error (missing operator) in query expression 'case when T52.[GRADE] = 'ASE' and T71.[P1] = 'Y' then T71.[Subtopic_id] end'.. [nQSError: 16014] SQL statement preparation failed. (HY000)
    SQL Issued: SELECT Employee.EMP_ID saw_0, Facts.ASE saw_1 FROM EMP ORDER BY saw_0, saw_1
    what may be the issue..when I am doing consistency check it not giving any warnings or errors..and If I tried in Answers column formula section also it is giving the error ...please give me what is missing...
    Note:In RPD i set features to "Restore to default"..
    Edited by: User on Jun 18, 2012 6:18 AM

    Hi all,
    by referring this thread I got solution and my problem solved But can anybody tell what This "Ask DBMS" button will do when we cliked it what is this functionality..
    Excel and CASE statement.

  • Using Presentation variables..along with case statements..

    Hi All.
    I have a issue using presentation variable along with CASE statements. My approach is
    1) I have a dashboard prompt, which is being set as Presentation variable.
    Based on the value selected in prompt, for ex the values of prompt can be 'ABC' and 'DEF'.
    I have a calculated column, the calculation goes this way...
    The forumal is
    CASE WHEN @{Presentation Variable Name} = 'ABC' THEN xxxxxxxxxx ELSE IF @{Presentation Variable Name} = 'DEF' END. It gives error of "no table being referenced"..
    Is this is the right approach??
    Can i get the values of variable in a column formula, so that a column can have values selected in prompt?
    Can anybody pls help me here..
    Thanks in advance...

    Hi
    Thanks for the quick response..
    I agree to ur point..
    But the requirement is
    Based on the value of the prompt I need to switch the calculation in one of the formula area of one column..
    If Prompt value is ABC then one kind of calculation in Fx and If the prompt value is DEF then one kind of calculation in the same Fx..
    How can I acheive this?
    Thanks in advance..

  • Help with a CASE statement

    Please help me with a CASE Statement:
    - When ID = 15, 16, 17, 18 then "Bad"
    - when ID = 19, then "Average"
    - when ID = 21, then "Good"
    - else "Null"
    Thank you!!

    Well the 1st thing to do would be to correct my poor spelling... change    Delault : to Default :
    Don't know why you would get an error stating "The result of selection formula must be a boolean". It's working fine on my machine.
    If your ID field is numbers stored text you have a couple different options...
    1) Convert the ID to a number...
    Select  ToNumber({home.noone_ID})
    2) Wrap the ID values in double quotes...
       Case "15", "16", "17", "18" :
          "BAD"
    Even if this were your problem... the error should be something other than the boolean thing...
    Jason

  • CASE Statement order of precedence

    The question i have is in regards to the CASE statement.
    If i have the following CASE statement in a calculation, and use the calculation to populate the column header, it returns 2 columns, one for 'Mexico' and one for 'Exclude' - That is good
    Statement 1:
    CASE WHEN ( "Customers (Sales History)".Country = 'MX' )
    THEN 'Mexico'
    ELSE 'Exclude'
    END
    So i have sucessfully extracted out sales from Mexico, now i want break those sales into 2 categories, 'Primary' and 'Secondary'. If i use the below case statement, I was expecting to get 3 column headers, one for 'Primary' one for 'Secondary' and one for 'Exclude', but instead I only get 1 column header for exclude. Im sure it has something to do with order of precedence, but could someone explain the logic of why discover handles it this way, and how to get around it?
    Statement 2:
    CASE WHEN ( "Customers (Sales History)".Country = 'MX' )
    THEN
    CASE WHEN ( Item Groups.Sales Type Id <> 3)
    THEN 'Primary'
    ELSE 'Secondary'
    END
    ELSE 'Exclude'
    END
    As a side note, if i use statement 2 and set a condition "Statement 2 <> 'Exclude'" , the result will be the columns 'Primary' & 'Secondary'
    Thanks
    Chris

    Hi,
    Can anyone tell me how to use a fast formula in discoverer-desktop [for reporting]. Currently, we have a disco-report that calcuates the 'monthly salary', but we don't want to use that. Instead, we have a custom fast-formula [xx_oab_monthly_salary] which has the conditions and calculations that we need.
    I want to use that formula in discoverer-desktop directly and get the monthly-salary.. i.e., someone needs to create a folder under the business area and add it i believe.. the formula internally calls a function.
    when i checked about this with my dba - he said, he can add the 'function', but not the formula. and i need to write the calculation part...
    does anyone know how to add it...there shud a way right...
    i do not have access to the technical-stuff here..
    someone said ["you can wrap the FF_EXEC.run_formula call into your own pl/sql function and map this function into the EUL so that the formula can be called from a workbook.", but this talks about pl/sql-do we need pl/sql
    for this].
    also, someone else said [ some fast formulas need specific contexts to be set. Depending on whether the fast formula below the only fast formula that you want to recreate in Discoverer.
    For a single formula, in general my recommendation is to recreate the fast formula instead of using the ff_exec call].
    any quick answers greatly appreciate..
    thx,

  • SQL Expression in decode function or case statement?

    Can I put SQL expressions in decode function or case statement?
    For example,
    select le.profile, decode( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile, 0, 'N', 'Y')
    from element le;
    or
    select le.profile, case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0 THEN 'N'
    ELSE 'Y'
    from element le;
    None of the above work.
    Can anyone tell me how to make it work?
    Is there any workaround?
    Thanks,
    J

    You simply needed and END to your CASE statement;
    SQL> with profile_data as (
       select 'XXXX_AFTER' name, 1 object_id from dual),
         element as (
       select 1 profile from dual union all
       select 2 from dual)
    select le.profile,
       case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0
       THEN 'N'
       ELSE 'Y'
       END new_col
    from element le
       PROFILE N
             1 Y
             2 N

Maybe you are looking for