This article provides a comprehensive guide on utilizing Equation fields in your Knack app.
This article covers the following topics:
What is an equation?
Equation fields function by merging field values, numbers, and other mathematical functions to construct an equation that will be calculated for each record. These fields are a specific type of field found in a table within the Data section of the Builder.
There are two equation types available: Number and Date
Numeric Equation example: (Sales - Expenses) * 12
Date Equation example: {Date Completed} - {Date Started}
Managing Equations
Adding an Equation
To add a field to a table in the Builder's Data section, start by selecting the desired table from the left menu. Next, click on the "Add Field" button located at the top of the page.
This will give you the option to choose the Equation field type from the left side of the screen:
After selecting the Equation field type, a popup will appear where you can customize the settings for your Equation field. Once you have configured the field, simply click on the "Add Field" button located at the bottom of the pop-up window.
Editing an Equation
To modify an existing equation, choose the table that includes the equation field and then select the additional options icon (represented by "...") located beside the field's name.
Deleting an Equation
To remove an equation, navigate to the table that contains the equation field and click on the additional options icon (represented by "...") next to the field's name. From the dropdown menu, select the "Delete" option.
Caution: Please note that deleting a field will result in the removal of all record values associated with that field. Therefore, it is important to confirm that this is the correct action for your app before proceeding.
Building an Equation
Equations can take many different forms. See the Equation Types section for details on these options.
To build to your equation:
-
Type the name of the field directly in the formula input box.
-
Use the "Fields" dropdown list to select any available fields, or
-
Type numbers or mathematical operators directly into the input box.
Below the equation editor, the Example Output shows you an example output of the current formula:
Accessing Connected Fields
As with text formulas, equations give you the option of retrieving data from connected records. If you start typing the name of a field on a connected table, you'll see the available options in a list.
You can also select the "Fields" dropdown and select the field. Fields available will include any fields from the data table you are currently in and any fields from connected tables.
Note: Fields from tables connected with a many-to-many relationship will not be available since multiple connected record values exist.
Tip: Check out our quick guide on how to include connected values in equations: How to Include Connected Values in Equations
Using Multiple Choice Fields in Equations
Multiple choice fields can be utilized in equations, whereby these values are automatically converted into numerical values for calculation purposes.
Non-numeric characters are automatically removed and the remaining value is considered for calculation. In case there is no number left, zero will be used as the default value.
Note: Equations using multiple choice fields will not display live results, even if the multiple choice field is used in the form.
Using Yes / No Fields in Equations
Yes/No fields can be utilized in equations. In these equations, the values of No and Yes are converted to numerical values, where No is represented as 0 and Yes is represented as 1, which are then used in the equation.
Using Equations in Forms
Requirements:
-
All fields required for the equation must be included on the form. Similarly, if you are setting a value in a record rule using a formula field, that field must also be present on the form.
-
The equation must be a Numeric equation and cannot be a Date Type.
-
The equation cannot use other formulas (sum, count, etc.)
Equation fields can be incorporated into forms to create dynamic calculators that update in real time. Let's take a look at a basic example:
Note: Currently, form submissions cannot be validated using equation fields.
Operators
You can use the following operators in your equations:
Operator |
Meaning |
+ |
adds |
- |
subtracts |
* |
multiples |
/ |
divides |
== |
equals |
!= |
not equals |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
& |
and |
| |
or |
Order of Operations
If your equation field relies on other formulas or fields with conditional rules, it is important to arrange them in the correct order. Knack processes these fields in the order they are listed in the table under the "Fields" section.
In simpler terms, if an equation depends on a field, a field with a conditional rule, or another equation, make sure to list those fields above the equation that utilizes them.
Equation Types: Settings and Functions
Numeric Equations
Numeric equations are powerful tools that allow you to calculate field values, numbers, and perform various mathematical functions. Additionally, you have the flexibility to format your results in different number formats, including decimals, rounding, and currency.
To activate a numeric equation, simply select the "Equation Type" option and set it to "Numeric".
Numeric Equation Settings
There are numerous settings available for numeric equations. See a full list of numeric equation settings here.
Numeric Equation Functions
Many regular math functions (such as ceil
or max
) can be used in Knack numeric equations. Others, like getDateDayOfMonth
provide numeric values for a date. Learn more here.
Note: If the "currentTime()" function is used on an equation set up in the Numeric Equation Type, the value will only be reprocessed on record update. If you need this to process on a schedule, see the Date Equation Type.
Date Equations
Date equations are capable of performing calculations involving dates and formatting the results in various units such as hours, days, weeks, and more. To enable a date equation, simply select the "Equation Type" option and set it to "Date".
Selecting the "Date" equation type reveals the "Date Type" and "Result Type" options.
Note: When blank date values are used in an equation, they are considered as "1/1/1970", which is the default start date for date values in Knack.
Date Equation Settings
There are a variety of options available for date equations. See the full list of date equations settings here.
Date Equation Functions
See our article with our comprehensive list of date equation functions: Date Equation Functions
Detailed Equation Examples
Conditional Equations
Your equation can accept conditional rules using the ternary operator. A ternary operation allows three operands and can be used as a shortcut for the if
statement.
An example:
Condition ? Equation_1 : Equation_2
This means if your condition is true, Knack will use the equation or value followed by the "?" symbol. If your condition is false, Knack will use the second equation or value followed by the ":" symbol.
Note that you can nest ternary statements to have more than two conditions/results like so:
Condition A ? Result 1 : (Condition B ? Result 2 : Result 3)
Also, you can use an "&" symbol to represent AND and a "|" symbol to represent OR in an equation like so:
Condition A > 0 & Condition B > 10 ? Equation 1 : Equation 2
Note: Unfortunately, it is not feasible to evaluate whether a field "is blank" or "is not blank" within a conditional equation.
Conditional Example 1:
There may be times when you need to track total elapsed time. This could easily be done using the currentTime() function, but what happens when your date field is blank?
A blank date would be interpreted as "0", which Knack would interpret as "January 1, 1970", resulting in a very long negative number. Using a conditional rule we can make sure our equation remains "0" instead:
{Invoice Date} > 0 ? {Invoice Date} - currentTime() : 0
Conditional Example 2:
This example shows how to apply an additional 3% late fee to a charge:
{Time of Payment} > {Due Date} ? {Cost} * 1.03 : {Cost}
Conditional Example 3:
To trigger an equation when values are equal you can use the "==" operator:
{Budget Period} == 1 ? {Q1 Date} : {Budget Period}==2 ? {Q2 Date} : 0
Conditional Example 4
This complex example uses the following logic:
-
If
Time of Payment
is later thanDue Date
, charge an extra 3% -
If
Time of Payment
is earlier than theDue Date
, charge 1% less -
Otherwise (if
Time of Payment
equalsDue Date
), charge the normal amount
{Time of Payment} > {Due Date} ? ({Cost} 1.03) : ({Time of Payment} < {Due Date} ? {Cost 0.99} : {Cost})
Conditional Example 5
This complex example uses the following logic:
-
If
Start Date
is blank, then nothing should be calculated -
If
End Date
is blank, then calculate the difference between the current date and theStart Date
-
If
Start Date
andEnd Date
are both populated, then calculate the difference between theEnd Date
and theStart Date
-
Otherwise (if they're both blank), set the value to zero
{End Date} > 0 & {Start Date} > 0 ? {End Date} - {Start Date} : {Start Date} > 0 ? currentTime() - {Start Date} : 0
Conditional Equations with Text Fields
Conditional rules don’t currently work with text fields, but you can map your text fields to number fields and use the number fields in your equation. Assuming you already have a short text field called Text
:
-
Add a number field (we’ll call it
Num
). -
Add a conditional rule to the
Num
for each option you want to account forText
; e.g. setNum
to "1" ifText
is "First Choice", setNum
to "2" ifText
is "Second Choice", etc. -
Run a batch update on your data (even on a new temporary field) to populate the
Num
field via its conditional rules. -
Use
Num
in place ofText
in your equations.
Conditional Equation with Text Fields Example
If we want the following conditions tested:
-
If
Text
is "First Choice", ie:Num
is 1, set the equation to "1" -
If
Text
is "Second Choice", ie:Num
is 2 set equation to "2" -
If
Text
is anything else, set the equation to "0"
In the equation, you would need to utilize the Num
field since equations cannot use text fields. Therefore, the equation would be structured as follows:
{Num} ==1 ? 1 : {Num} ==2 ? 2 : 0
Using Equations in Your App
Use Equations in Equation or Text Formula Fields
Just as in any other field on a table connected to another table, equation fields can be used in text formulas or other equation fields.
Viewing Equations in the Builder
Equation values will be displayed in the "Records" tab in a table in the Builder. Here you can view and reference these values, as well as use record filters to display particular values for this equation field.
Viewing Equations in the Live App
Except for maps, menus, and rich text views, equation fields can be added and used to display values in all page views.
How To Guides
Troubleshooting
Can't Access Connected Field
If you're unable to access a connected field in a formula, check the connection between the two tables. You can only access fields from a table with a many-to-one or one-to-one relationship with the table you're adding the equation field to.
For example, let's say that many Tasks can be connected to one Project, and there's a number field called "Hourly Rate" in the Project table. You can create an equation in the Tasks table that pulls in the value from the Hourly Rate field in the Project's table because there's only one hourly rate value to pull in.
However, if you had a number field called "Hours Worked" in the Tasks table, you wouldn't be able to access that field in an equation in the Projects table. There would be multiple "Hours Worked" values to pull in and an equation field can't do that.
In a case like that, you could use a formula field to access all the connected "Hours Worked" values. Then, the formula field could be used in the equation.