20 Replies Latest reply: May 21, 2013 8:00 AM by Robert Ebner

# if - elseif construction

Hi all,

I would like to use the following statement in my script, but it doesn't work:

"reisezeit" is a variable which can be 0 or 1.

if reisezeit = 1 then

sum(if(Reisezeit='Nein', Stunden) )as Stunden_Projekt;

elseif

reisezeit = 0

sum(Stunden) as Stunden_Projekt;

ELSE

sum(Stunden) as Stunden_Projekt;

end if

Does someone has an idea why?

• ###### Re: if - elseif construction

Hi robert2012,

Can you change if reisezeit to if \$(reisezeit) then try again.

Regards,

Sokkorn

• ###### Re: if - elseif construction

Hi Sokkorn,

thank you very much.

I changed the first and both, but it still doens't work.

It says: Syntax Fehler

You know why?

• ###### Re: if - elseif construction

try this:

if(\$(Reisezeit) = 1,sum(if(\$(Reisezeit)='Nein', Stunden,if(\$(Reisezeit) = 0,sum(Stunden))))) as Stunden_Projekt

HTTH

Sushil

• ###### Re: if - elseif construction

Hi,

thx, but there is still a mistake as QlikView says:

Error in expression:

')' expected

But I don't see where!

You?

• ###### Re: if - elseif construction

This should work ...

if(reisezeit=1,sum(if(Reisezeit='Nein',Stunden)),sum(Stunden))  as Stunden_Projekt;

Message was edited by: Anand K

• ###### Re: if - elseif construction

Hi,

I added \$ for the variables:

if(\$(reisezeit) = 1,sum(if(\$(reisezeit)='Nein',Stunden)),sum(Stunden))  as Stunden_Projekt

Now QlikView says:

If takes 2-3 parameters

• ###### Re: if - elseif construction

Remove the '\$' sign and it should work for you

• ###### Re: if - elseif construction

There is still a problem with "

:

If takes 2-3 parameters"

and if I put \$ away QlikView doens't find the field as it is a varible ?!

Hope you know why and you can help me

• ###### Re: if - elseif construction

Try this :

if (\$(reisezeit) = 1,

sum(if(Reisezeit='Nein', Stunden)),

if(\$(reisezeit)=0,

sum(Stunden),

sum(Stunden)

)

)
as Stunden_Projekt ;

If your variable takes only values 1 or 0, you can also write this :

if (\$(reisezeit) = 1,

sum(if(Reisezeit='Nein', Stunden)),

sum(Stunden)

)
as Stunden_Projekt

• ###### Re: if - elseif construction

Thank you!

But there is "Error in expression: If takes 2-3 parameters"

for both ideas.

What can I do?

• ###### Re: if - elseif construction

If you use other fields in your load statement, you must have a Group By Clause in your script.

dimension1,

If (\$(reisezeit) = 1,

sum(if(Reisezeit='Nein', Stunden)),

sum(Stunden)

)
as Stunden_Projekt

RESIDENT Data

GROUP BY dimension1 ;

I used dmension1 as example.

• ###### Re: if - elseif construction

I use like you wrote:

Projekte_Monat:

left (MAID_Z, 14) as MAID_gesamt_Z,

If (\$(reisezeit) = 1,

sum(if(Reisezeit='Nein', Stunden)),

sum(Stunden)

) as Stunden_Projekt

Resident ControllerExport_tmp1

Group by MAID_Z;

But there is always:

Error in expression: If takes 2-3 parameters

• ###### Re: if - elseif construction

It's strange because i use the exact script you wrote in a test application and it works. (I have created an inline table with the 3 fields Reisezeit, Stunden and MAID_Z).

These 3 fields exist in the table ControllerExport_tmp1 ?

• ###### Re: if - elseif construction

Thank you very much for your help! " "

• ###### Re: if - elseif construction

Hi Rob!

There are 2 kinds of "IF" keyword.

The 1st is when you use it in Data Loading, use this syntax:

IF (condition) THEN

{statement_if_true}

ELSE

{statement_if_false}

END IF

The 2nd if is available in the Expression dialog with syntax:

=IF(condition, statement_if_true, statement_if_false)

It think what you're looking for is in the expression, which is 2nd if.

Hope that helps

A

• ###### Re: if - elseif construction

I changed to:

Projekte_Monat:

left (MAID_Z, 14) as MAID_gesamt_Z,

If \$(reisezeit) = 1 then

sum(if(Reisezeit='Nein', Stunden)) as Stunden_Project;

else

sum(Stunden) as Stunden_Projekt;

ENDIF

Resident ControllerExport_tmp1

Group by MAID_Z;

But there is still a mistake with my syntax, I can't solve

• ###### Re: if - elseif construction

Hi,

try this one.

Projekte_Monat:

MAID_Z,
left (MAID_Z, 14) as MAID_gesamt_Z,
If(\$(reisezeit)=1,if(Reisezeit='Nein', Sum(Stunden)),sum(Stunden)) as Stunden_Project
Resident ControllerExport_tmp1
Group by MAID_Z;

where you are decler this variable. send your full script. reisezeit variable value standard or random.

• ###### Re: if - elseif construction

I just wrote

set reisezeit = 0; //at the beginning of the skript

But theere is invalid expression!

Is there a problem with the variable?

• ###### Re: if - elseif construction

Write this within load statement where your field Stunden exists in table

if(\$(Reisezeit) = 1,sum(if(\$(Reisezeit)='Nein', Stunden)),

if(\$(Reisezeit) = 0,sum(Stunden))) as Stunden_Projekt

• ###### Re: if - elseif construction

Hi I changed to:

if(\$(reisezeit) = 1,sum(if( Reisezeit ='Nein', Stunden))),

if(\$(reisezeit) = 0,sum(Stunden)) as Stunden_Projekt

This works, but the results are wrong!

If I compare to:

sum (Stunden) as Stunden_Projekt

there are different results => Do you have an explanation/idee?

Thank you!