Advent of Code is back! Unwrap daily challenges to sharpen your Alteryx skills and earn badges along the way! Learn more now.

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

If statement error

nylasor_17
5 - Atom

Hi Alteryx Community,

 

I'm having difficulty when using if statement , I still get parse error.

 

Kindly help me to resolve this concerns.

 

IF [Length]=6 THEN [Business Area]+[Doc Nbr]
ELSEIF [Length]=5 THEN [Business Area]+"0"+[Doc Nbr]
ELSEIF [Length]=4 THEN [Business Area]+"00"+[Doc Nbr]
ELSEIF [Length]=3 THEN [Business Area]+"000"+[Doc Nbr]
ELSEIF [Length]=2 THEN [Business Area]+"0000"+[Doc Nbr]
ELSEIF [Length]=1 THEN [Business Area]+"00000"+[Doc Nbr]
ELSE Null()
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF

 

Thank you.

Lyn

7 REPLIES 7
AngelosPachis
16 - Nebula

Hi Lyn ( @nylasor_17 )

 

By the looks of it, you don't need all the extra EndIFs at the end of your statement, so the below should be enough

 

IF [Length]=6 THEN [Business Area]+[Doc Nbr]
ELSEIF [Length]=5 THEN [Business Area]+"0"+[Doc Nbr]
ELSEIF [Length]=4 THEN [Business Area]+"00"+[Doc Nbr]
ELSEIF [Length]=3 THEN [Business Area]+"000"+[Doc Nbr]
ELSEIF [Length]=2 THEN [Business Area]+"0000"+[Doc Nbr]
ELSEIF [Length]=1 THEN [Business Area]+"00000"+[Doc Nbr]
ELSE Null()
ENDIF

 

If the issue remains, can you click on the metadata tab of your results pane and post a screenshot of the data type for the fields

 

[Length]

[Business Area] 

[Doc Nbr]

 

Cheers,

 

Angelos

nylasor_17
5 - Atom

Hi Angelos,

 

I tried that only 1 ENDIF however there still an error. attached the screenshot of data type.

 

nylasor_17_0-1620418004805.png

 

Hope you can help me. 

Thank you

Lyn

lchavezv
5 - Atom

Hi,

 

You have two options:

 

1) Length is V_Wstring so use [Length]='6' instead of [Length]=6 

 

2) Define Length as Int16 and don't change your formula.

 

 

Thank you,

Luis

AngelosPachis
16 - Nebula

@nylasor_17  so the issue is that the field [Length] is a V_WString so you are trying to compare a string to a numeric value.

 

Screenshot 2021-05-07 212700.jpg

 

You can change the data type for the field [Length] to an INT64 and that should make the error go away.

 

Let me know if that worked for you please.

 

Cheers,

 

Angelos

TheOC
15 - Aurora
15 - Aurora

hey @nylasor_17 
I believe you'll want the following:

TheOC_0-1620441377452.png


I also have below this, a switch statement, that does the exact same thing. Switch statements are generally the 'correct' way to do this type of if-statement, as they are marginally faster (and I think look cleaner). May be useful for you!

I've attached a workflow with it too.


Bulien
ELOCKER
6 - Meteoroid

Nylasor,  This should save you from writing longer case statements in the future 🙂  Try out: [Business Area]+PADLEFT([Doc Nbr],6,"0").

 

 

Alteryx is the best!

nylasor_17
5 - Atom

Thank you Angelos 

 

The formula is accepted. 😉😊

Labels