I have 2 columns in my table. "dB1" and "dB2" Now I want to calculate a third columnn "dB3" depending of the entries in column "dB1" and "dB2"

if "dB1" or "dB2" have an entry of "0" I want to calculate: "dB1" + "dB2"

if neither "dB1" nor "dB2" have an entry of "0" I want to calculate: 10*log(10^(0,1*"dB1")+10*(0,1*dB2))

I struggle with the syntax and the if clause here... Can someone give me the code as I theoretically know how it should be done but can't work around the syntax in python...


I'm guessing you mean the CalculateField tool when you refer to 'field editor with Python,' and you are in a locale that uses comas as a decimal mark (ie 0,1*dB1 = 0.1*dB1).

You can do this with a single expression:

!dB1! + !dB2! if 0 in (!dB1!,!dB2!) else 10*math.log(10**(0.1*!dB1!)+10*(0.1*!dB2!))

This uses a shorthand form of if/else:

<condition if true> if condition> else <condition if false>

0 in (!dB1!,!dB2!) is a short way of checking if either value is 0 and the rest is your formula.

This also uses uses the default base (e) for the math.log function which is the natural logarithm. If you wanted a different base, you will need to specify it to log.

Also note that field calculator uses ! to surround field names, where the SQL expression uses " (for many formats).

  • Thank you, now I am so far that the calculator calculates something....but not correct? If I take an example for a line: DB_Low: 75 DB_LOW_1: 55 Result: 173 But if I calculate manually it should result in: 75.04 This is my code: !DB_Low! + !DB_LOW_1! if 0 in ( !DB_Low! , !DB_LOW_1! ) else 10*math.log(10**(0.1* !DB_Low! )+10**(0.1* !DB_LOW_1! )) – M.S. Jun 6 '16 at 15:27
  • Ok I got it now! (I had to use log10) --> Question answered! Thanks! – M.S. Jun 9 '16 at 9:14
  • @M.S. I'm glad it worked for you...that's what I was referring to when I said log uses the default base. – Evil Genius Jun 9 '16 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.