Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

if statement within an if statement

Im trying to write an if statement so that if my data is of a certain value then the background turns a certain colour, this will then change depending on which conditions are picked by a slider.

I've set up the variables and the various values but am having trouble wrapping an if statement within another if statement,

my code looks like

if(vKickOutcomeMatchTable=0)

if([Positive %]/[Count({SAR_1<[Kick Outcome]={1}>}TOTAL <Kicker> DISTINCT MatchID_ScenarioMap)/Count({SAR_1} TOTAL <Kicker> DISTINCT MatchID_ScenarioMap)])>$(vT1),$(vRAG1),
if([
Positive %]/[Count({SAR_1<[Kick Outcome]={1}>}TOTAL <Kicker> DISTINCT MatchID_ScenarioMap)/Count({SAR_1} TOTAL <Kicker> DISTINCT MatchID_ScenarioMap)])>$(vT2),$(vRAG2),
if([
Positive %]/[Count({SAR_1<[Kick Outcome]={1}>}TOTAL <Kicker> DISTINCT MatchID_ScenarioMap)/Count({SAR_1} TOTAL <Kicker> DISTINCT MatchID_ScenarioMap)])>$(vT3),$(vRAG3),
if([
Positive %]/[Count({SAR_1<[Kick Outcome]={1}>}TOTAL <Kicker> DISTINCT MatchID_ScenarioMap)/Count({SAR_1} TOTAL <Kicker> DISTINCT MatchID_ScenarioMap)])>$(vT4),$(vRAG4))

if(vKickOutcomeMatchTable=1)

if([Positive %]/[({SAR_1<[Kick Outcome]={1}>}TOTAL <MatchID> DISTINCT MatchID_ScenarioMap)/Count({SAR_1<[Kick Outcome]={'-1',0,1>} TOTAL <MatchID> DISTINCT MatchID_ScenarioMap)])>$(vT1),$(vRAG1),

if([Positive %]/[({SAR_1<[Kick Outcome]={1}>}TOTAL <MatchID> DISTINCT MatchID_ScenarioMap)/Count({SAR_1<[Kick Outcome]={'-1',0,1>} TOTAL <MatchID> DISTINCT MatchID_ScenarioMap)])>$(vT2),$(vRAG2),

if([Positive %]/[({SAR_1<[Kick Outcome]={1}>}TOTAL <MatchID> DISTINCT MatchID_ScenarioMap)/Count({SAR_1<[Kick Outcome]={'-1',0,1>} TOTAL <MatchID> DISTINCT MatchID_ScenarioMap)])>$(vT3),$(vRAG3),

if([Positive %]/[({SAR_1<[Kick Outcome]={1}>}TOTAL <MatchID> DISTINCT MatchID_ScenarioMap)/Count({SAR_1<[Kick Outcome]={'-1',0,1>} TOTAL <MatchID> DISTINCT MatchID_ScenarioMap)])>$(vT4),$(vRAG4),

This is my first time working with if statements within another if statement so would really appreciate any help to make this code work!

6 Replies
alexandros17
Partner - Champion III
Partner - Champion III

It seems that the problem are unbalanced parenthesys, (the closing ones)

Follow this syntax:

If(cond1=true, 'X',

If(cond2=true, 'Y',

'Z'

)

)

JonnyPoole
Employee
Employee

The basic format is    IF(  CONDITION ,  TRUE EXPRESSION , FALSE EXPRESSION)

If you want to wrap an existing expression , conditional or otherwise,  you need to figure out if the existing expression is the TRUE or FALSE expression for the new condition

Either

if(   CONDITION ,    EXISTING STATEMENT ,   FALSE EXPRESSION)

or

if(   CONDITION ,   TRUE EXPRESSION,   EXISTING STATEMENT )

Not applicable
Author

I think I understand what you mean but am having difficulty translating it into the code itself

maxgro
MVP
MVP

example of nested if

if(y=2012,

     if(m='gen',20121, 

     if(m='feb',20122,  '2012???'

     )),

if(y=2013,

     if(m='gen',20131,

     if(m='feb',20132,  '2013???'

     )),

'other year'

))

alexandros17
Partner - Champion III
Partner - Champion III

1) thake all the conditions and write them row by row.

2) write the basic if (the script I sent  you is perfect) with all the nested if.

Now you have

Condition 1 ....

Condition 2 ...

and

If(cond1=true, 'X',

If(cond2=true, 'Y',

'Z'

)

)

now paste your condition into the if ... it is the only winning strategy

morganaaron
Specialist
Specialist

Hi Rob,


A few things you need to watch out for!

1. If(vKickOutcomeMatchTable=0) - You don't want the end bracket here as it's closing your If statement, so get rid of it.

2. Inside your statements you are doing 3 divisions, but I don't think you've wrapped the expression you want calculated first in brackets. If you want to divide "Positive %" field by your expression to calculate the average, you'll want to precede the [Count...] statement with a bracket and end it with a bracket after the second ScenarioMap statement.

3. As a rule of thumb, for every extra if statement you nest, you'll need an extra closing bracket when you reach the end, so if you haven't got the 4 closing brackets if you've got 4 if statements, you know you're in trouble - but use the syntax checker and this should tell you where you're going wrong, if you highlight an end bracket it will show you the bracket it's "closing" and so you should be able to see if you've missed any (not to mention it'll be highlighted red).

It may also be worth replacing your long expression with a variable that stores your expression, as this may help you to see where you're going wrong and reduce the size of the total expression!

Regards.