Variables and calculations
A variable is a value KyraForm works out from answers as people fill in the form, such as a total price, an age, a score or a deadline. Variables can be:
- shown on the form with a Display Value,
- quoted in question text or end screens with Insert answer → Variables,
- used as validation and date limits,
- used in conditions,
- used as a payment amount.
Create a variable
Section titled “Create a variable”-
Open any question, then Advanced → Calculate from other answers. This opens Data calculations, which lists every variable on the form.
-
Select Create New Variable.
-
Fill in:
Setting What to enter Variable Name A short name with no spaces, such as totalAmountoruserAge. Required.Type Number, Text, True/False or Date. Description (Optional) What it calculates, for your teammates. Calculation Mode Formula, Function or Value mapping. See below. -
Select Create Variable.
Variables update immediately as respondents type. The value is recalculated on the server when the form is submitted, so it can’t be tampered with.
Formula mode
Section titled “Formula mode”Write a Calculation Formula using question IDs, variables and functions. Click a question under Available Fields (Click to insert) to insert its ID, and the function buttons to insert functions.
Examples:
| Need | Formula |
|---|---|
| Line total | quantity * unit_price |
| Total with 18% GST | subtotal * 1.18 |
| Average of three marks | avg(maths, science, english) |
| Age from date of birth | age(dob) |
| Age on a given date | ageFromDate(dob, event_date) |
| Due date 30 days after the start | addDays(start_date, 30) |
| Full name | concat(first_name, " ", last_name) |
Functions
Section titled “Functions”| Function | Returns |
|---|---|
sum(a, b, …) | Total of the values |
avg(a, b, …) | Average of the values |
Math.min(a, b, …) / Math.max(a, b, …) | Smallest / largest value |
abs(x), floor(x), ceil(x), sqrt(x), pow(x, y) | Absolute value, round down, round up, square root, x to the power y |
age(dob) | Whole years from a date of birth until today |
ageFromDate(dob, date) | Whole years from a date of birth until another date |
addDays(date, n), addMonths(date, n), addYears(date, n) | A date moved forward (or back, with a negative n) |
concat(a, b, …) | Text joined together |
Table functions
Section titled “Table functions”Use these to total a Table Grid column:
| Function | Returns |
|---|---|
tableSum("table_id", "column_id") | Sum of the column |
tableAvg("table_id", "column_id") | Average of the column |
tableMin("table_id", "column_id") / tableMax(…) | Smallest / largest value in the column |
tableCount("table_id") | Number of rows |
tableCountFilled("table_id", "column_id") | Rows where the column is filled in |
Put the table’s ID and the column’s ID in quotes. Clicking them in the editor inserts them for you. The column must be a Number or Calculated column. Otherwise you see “No numeric columns — add a Number or Calculated column to use aggregate functions.”
If the formula has a mistake, the editor shows Formula error with the reason.
Function mode
Section titled “Function mode”For logic a formula can’t express, write a small function.
- Under Parameters, select Add Parameter for each question you need, and give it a param name.
- Write the Function Body in JavaScript-style code that
returns the value. - Select Test Function. “Test runs with sample values (numbers = 1, text = "").”
Example, a tiered discount:
if (qty >= 100) return price * qty * 0.8;if (qty >= 10) return price * qty * 0.9;return price * qty;Value mapping mode
Section titled “Value mapping mode”Turn each option of a choice question into a number or text. Pick the Source Field, then fill in the Mapped Value for each option under Map Options to Values.
Example: Plan → Basic = 499, Standard = 999, Premium = 1999. Then use the variable in a total, or directly as the payment amount.
- Name variables for what they mean (
totalFee, notv1). They appear in exports and in other formulas. - Build in small steps: a
subtotalvariable, thentax = subtotal * 0.18, thentotal = subtotal + tax. Variables can use other variables, and KyraForm works out the order. - Use Preview to test with real answers before publishing.