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.

AND OR precedence rule

fmvizcaino
17 - Castor
17 - Castor

Hi everyone,

 

I just realized the precedence rule for AND OR is totally wrong. Am I crazy??

 

Considering AND as a multiplier and OR as an adder (according to logic gates theory), the following should give me 4 rows as a result, without the need to add parenthesis.

 

fmvizcaino_0-1659028182409.png

 

Best,

Fernando Vizcaino

 

10 REPLIES 10
FilipR
11 - Bolide

In order not to go crazy with AND/OR conditions, always put them in parenthesis in a logical way. 😁

It's good practice for whatever language you're writing in. 

fmvizcaino
17 - Castor
17 - Castor

Hey @FilipR ,

 

I agree with you that it's a good practice, but that doesn't correct the issue.

 

It would be similar to having the following:

2+2x2 = 8

2+(2x2) = 6

 

As math principles are, logic gates principles should also be consistent here.

 

Best,

Fernando Vizcaino

IraWatt
17 - Castor
17 - Castor

🤣 I have never thought about it. But after checking I think your right @fmvizcaino ! 

 

Does the Formula Tool have a specific programming language or is it an Alteryx creation which is transpiled to a language?

IraWatt
17 - Castor
17 - Castor

@fmvizcaino C based but what ever converts it to C ignores precedence. Seems this has been hiding in plain sight 😅 great spot. 

atcodedog05
22 - Nova
22 - Nova

This is defeinetely an interesting use case @fmvizcaino 😀

Just ran into this issue myself... also had the same thought: what is going on, am I crazy here?? since it's such a basic principle of logical operators. Apparently it hasn't been corrected yet, so excessive parenthesis it is :)

apathetichell
19 - Altair

Just seeing this --- but not sure what the issue is --- let's look at this in Node.js--

const oceanList=[
  {room:'single',
  view:'ocean'
  },
  {room:'double',
  view:'ocean'
  },
  {room:'single',
  view:'ocean'
  },
  {room:'single',
  view:'mountain'
  },
  {room:'single',
  view:'mountain'
  }
]


function myFunction() {
  for (i=0;i<oceanList.length;i++){
    if (oceanList[i].room=='single' | oceanList[i].room=='double' && oceanList[i].view=='ocean'){
      console.log(oceanList[i])
    }
    else{}
  }
}
 
this returns 3. just like Alteryx. AND/OR should be the same level of operator -- and the terms will be applied successivly. 

Use the boolean/logical OR operator (||) instead of the bitwise OR (|) in your js and you will get the "correct" answer of 4 items (last item in the array should be double mountain per the original post).

 

The tooltip for the formula syntax specifies that within the alteryx formula tool, the boolean operators are "||" "OR", which should not be applied consecutively but should instead respect precedence as others above noted (AND takes precedence, i.e. is executed first, before OR). This would be analogous to boolean/logical operators in other languages: SQL (AND/OR), Python (and/or), JavaScript (&&/||), etc.

 

Here's a simple example of the incorrect result being returned within the Formula Tool in test 1, fixed (which shouldn't be necessary) with parenthesis in test 2:

 

image.png

Labels