> ## Documentation Index
> Fetch the complete documentation index at: https://helpdocs.gavel.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Calculations: Advanced Logic

> Create computed variables, set conditional values, automate pronoun handling, and perform numerical and date calculations in your Gavel workflows.

Calculations — previously called Invisible Logic — let you create new variables whose values Gavel computes automatically, without asking the client another question. You define a formula or a set of conditional rules, and Gavel evaluates them behind the scenes to produce a value that you can use anywhere in your document templates or question logic. This is the tool to reach for whenever you need to derive information from what the client already told you, rather than asking for it directly.

You access Calculations from the **Calculations** page at the top of the screen when editing a workflow.

***

## Why use Calculations?

<AccordionGroup>
  <Accordion title="Automate mathematical computations">
    Gavel can perform arithmetic on numeric answers so you don't have to ask for derived values. For example, divide an annual salary by 12 to produce a monthly figure, then reference that computed variable in your document.
  </Accordion>

  <Accordion title="Combine multiple answers into one variable">
    Merge separate fields — such as first name, middle name, and last name — into a single full-name variable. You collect the parts once and reference the assembled whole wherever you need it, without asking the client for their full name again.
  </Accordion>

  <Accordion title="Create conditional variables for document logic">
    Set a variable to different text values depending on conditions. The classic use case for legal documents is pronouns: assign "He," "His," and "Testator" or "She," "Her," and "Testatrix" based on the client's stated gender.
  </Accordion>

  <Accordion title="Derive dates from other dates">
    Calculate deadline dates, ages, or eligibility dates automatically. For example, compute whether a client's birthday means they were under 18 as of today, and use that result to control what pages they see.
  </Accordion>
</AccordionGroup>

***

## Creating a Calculation variable

<Steps>
  <Step title="Open the Calculations page">
    While editing your workflow, click the **Calculations** tab at the top of the screen.
  </Step>

  <Step title="Create a new variable">
    Add a new Calculation variable and give it a name. Choose the variable type (Text, Number, Date, etc.) to match the kind of value you want to produce.
  </Step>

  <Step title="Enter the fallback formula">
    In the formula field, enter the value the variable should have when no specific condition is met. For a conditional variable, this is typically an empty string `" "`. For a straightforward formula with no conditions, enter the expression directly here.

    To insert values in the formula field, click inside it to see a dropdown menu:

    * Click **Enter text** to type a text literal (Gavel adds the quotation marks)
    * Click **Enter space** to insert a blank space `" "`
    * Click a variable name (prefixed with `@`) to reference any existing variable
    * Scroll down to find functions such as add, concatenate, or format date
  </Step>

  <Step title="Add conditional logic (optional)">
    Click **Add Logic** to define conditions under which the variable should take a different value. Each condition specifies: *when \[variable] equals \[value], set this Calculation variable to \[output]*.

    You can group multiple Calculation variables together and apply the same logical condition to all of them at once — useful when a single condition (such as gender) drives several related variables (pronoun, possessive, title).
  </Step>
</Steps>

<Note>
  Each Calculation variable requires a fallback value. If your logic panel appears blank when you return to edit it, check that you have set a fallback in the formula field. This is often just an empty string for text variables.
</Note>

***

## Referencing Calculation variables in documents

You reference Calculation variables in your DOCX or PDF templates using the same double-curly-bracket syntax you use for any regular questionnaire variable:

```text theme={null}
{{ CalculationVariableName }}
```

Calculation variables work seamlessly across your entire workflow — in document templates, question logic conditions, and instruction blocks.

***

## Setting conditions for pronouns

Handling pronouns is one of the most common Calculations use cases in legal document automation. Instead of scattering conditional logic throughout your template, you create a small group of pronoun variables in Calculations and then reference those variables everywhere a pronoun appears.

<Steps>
  <Step title="Ask for gender">
    Add a single-select question with a variable such as `ClientGender` and choices like Male, Female, and Non-binary.
  </Step>

  <Step title="Create a pronoun Calculations group">
    In Calculations, create a group of new variables for the pronoun forms you need. For a will or trust, you might create:

    * `ClientPronounSubject` (He / She / They)
    * `ClientPronounPossessive` (His / Her / Their)
    * `ClientTitle` (Testator / Testatrix / Testator)
  </Step>

  <Step title="Set the conditional logic">
    Add logic to the group:

    | Condition                      | ClientPronounSubject | ClientPronounPossessive | ClientTitle |
    | ------------------------------ | -------------------- | ----------------------- | ----------- |
    | `ClientGender == "Male"`       | He                   | His                     | Testator    |
    | `ClientGender == "Female"`     | She                  | Her                     | Testatrix   |
    | `ClientGender == "Non-binary"` | They                 | Their                   | Testator    |

    Set the fallback for each variable to an empty string.
  </Step>

  <Step title="Repeat for additional parties">
    Create a second Calculations group for spouse pronouns, following the same pattern with a `SpouseGender` question as the source.
  </Step>

  <Step title="Use the variables in your template">
    Reference the pronoun variables anywhere in your document:

    ```text theme={null}
    {{ ClientTitle }} hereby declares that {{ ClientPronounSubject }} is of sound mind
    and wishes to dispose of {{ ClientPronounPossessive }} estate as follows.
    ```
  </Step>
</Steps>

***

## Numerical calculations

For numeric computations in your document templates, you can write mathematical expressions directly in your template using double curly brackets. Make sure the source questions use the **Number** or **Integer** question type.

<Tabs>
  <Tab title="Addition">
    ```text theme={null}
    {{ (variable1 + variable2)|float }}
    ```

    Displays the sum of two variables as a decimal number.
  </Tab>

  <Tab title="Multiplication / Division">
    ```text theme={null}
    {{ (100 * variable1 / variable2)|float }}
    ```

    Useful for expressing a value as a percentage of another.
  </Tab>

  <Tab title="Round down">
    ```text theme={null}
    {{ (variable)|round(2, 'floor') }}
    ```

    Rounds down and keeps two decimal places.
  </Tab>

  <Tab title="Round up">
    ```text theme={null}
    {{ (variable)|round(2, 'ceil') }}
    ```

    Rounds up and keeps two decimal places.
  </Tab>

  <Tab title="Exponentiation">
    ```text theme={null}
    {{ (variable1**variable2)|float }}
    ```

    Raises `variable1` to the power of `variable2`.
  </Tab>
</Tabs>

When combining rounding with number formatting, place the rounding call *inside* the parentheses:

```text theme={null}
{{ "{:,.0f}".format(variablename|round(0,'ceil')) }}
```

***

## Date calculations

Gavel provides a full set of date-calculation functions you can use directly in document templates.

### Calculate age or time elapsed

```text theme={null}
{{ (date_difference(starting=DateVariable).years|int) }}
```

Outputs the number of whole years between a date variable and today. This is the standard pattern for calculating a client's age from their date of birth.

### Calculate between two specific dates

```text theme={null}
{{ (date_difference(starting=StartDate, ending=EndDate).years|int) }}
```

### Add or subtract days, months, or years

```text theme={null}
{{ format_date( (DateVariable) + date_interval(years=2) ) }}
```

Use `-` instead of `+` to subtract. You can substitute `years`, `months`, `days`, or `weeks` for the unit. This is especially useful for computing statute of limitations deadlines and notice periods.

### Add or subtract business days

```text theme={null}
{{ format_date( (DateVariable) + date_interval(bus_days=10) ) }}
```

### Calculate months between two dates

```text theme={null}
{{ (relative_date_difference(starting=StartDate, ending=EndDate).months) }}
```

### Find the next or prior business day

```text theme={null}
{{ next_business_day(DateVariable) }}
{{ prior_business_day(DateVariable) }}
```

### Date-based conditional eligibility

You can use Calculations to create eligibility date variables and then drive page logic from them. For example, an attorney who only represents minors can compute:

```text theme={null}
eligibility_date = today() - date_interval(years=18)
```

Then set a page condition of `client_birthday > eligibility_date` to show the minor-specific page only to qualifying clients.

### Display conditional messages based on dates (in questions)

In instruction blocks within your questionnaire, you can show different text based on a date comparison:

```text theme={null}
${ "We are sorry. You are too late." if date_difference(starting=FilingDate).days > 30 else "Great. Let's continue." }

${ "You made it before the deadline" if DateVariable > as_datetime("2025-12-31") else "You are past the deadline." }
```
