Knowledge Base

Declaring Variables in Formulas | INTERMEDIATE

Adding declared variables to your QuickBase formula is a great way to simplify your code, making it concise and easy to read. A declared variable takes the place of a portion of code so that it can be reused.

Let’s say you want to calculate how much to charge a client for a project, based upon labor hours. In this case you can use a simple formula:

[Labor Hours] * [Rate]

However, let’s say you would like to give the client a 15% discount if the cost of their project exceeds $1,000. This expands your formula:

If(
  [Labor Hours] * [Rate] > 1000, 
  ([Labor Hours] * [Rate])*.85, 
  [Labor Hours] * [Rate]
)

Notice that in the above example we are reusing [Labor Hours] * [Rate] over and over. Let’s take a look at this formula if we rewrite is using a declared variable:

  1. First, declare the variable ‘totalCost’ by setting it equal to [Labor Hours] * [Rate].
var number totalCost = [Labor Hours] * [Rate]

Let’s look at the above one component at a time

  1. Var: This tells QuickBase we are about to declare a variable
  2. Number: Tells QuickBase the variable will be the type ‘number’
    1. totalCost: This is the name of the declared variable

EDITORS NOTE: Common types of variables include: text, number, date, duration and bool (short for boolean)

  1. Now, when we write the full formula using our newly declared ‘totalCost’ variable:
1000, $totalCost*.85, $totalCost)
  1. Notice we no longer need to type out the same formula over and over. We’ve taken care of that with our new variable ‘totalCost’. Additionally, it’s much easier to read our new formula because we can clearly see that we are comparing totalCost. As your formulas become increasingly long and complex you will find declared variables more useful to shorten code and clarify what is occurring within your formula

For more articles on fields and formulas click here.


Copyright ©2020 - Quandary Consulting Group