# Example expressions

These examples show common patterns you can adapt when you [build an expression](<https://help.apteco.com/en/orbit/data/expressions/build/index.md>). Each one says what it returns, so you can check [where you can use it](<https://help.apteco.com/en/orbit/data/expressions/#where-you-can-use-an-expression>). The [FastStats expression reference](<https://help.apteco.com/en/faststats/expressions/index.md>) describes each function.

> [!NOTE]
>
> The variable names in these examples, such as `[DOB]` and `[Appeal]`, may not exist in your system. Replace each one with the exact name of a variable in your own FastStats system. Otherwise, Orbit shows an error such as `Variable 'Lastname' not found`.

## Dates

Date functions work out ages, months, and time between dates. See the FastStats [date functions](<https://help.apteco.com/en/faststats/expressions/date-functions/index.md>).

### Age

Work out each supporter's age in years from their date of birth:

Expression

```apteco
AgeYears([DOB])
```

Returns a number, such as `34`. Use it to filter supporters by age, or in message personalisation to target milestone birthdays or adjust images for different age groups.

### Birthday month

Work out the month of each supporter's birthday, from 1 for January to 12 for December:

Expression

```apteco
DateMonth([DOB])
```

Returns a number, such as `3` for March. Use it as a filter to send birthday messages to everyone with a birthday in the coming month.

### Time since a first gift

Work out how many years have passed since each supporter's first gift:

Expression

```apteco
DateDiff(Today(0),[FirstDonationDate],"years")
```

Returns a number of years, such as `7`. Use it to celebrate anniversaries of a supporter's first gift, or to thank long-standing supporters.

## Locations

Location functions work out distances from UK postcodes, in miles. See the FastStats [location functions](<https://help.apteco.com/en/faststats/expressions/location-functions/index.md>).

### Distance to a location

Work out the distance from each supporter's postcode to one location, such as your head office:

Expression

```apteco
UKPostcodeDistance([Postcode], "CV34 4EH")
```

Returns a number of miles, such as `82.05`. As the measure on a cube tile with a **Region** dimension, the **Mean** function shows the average distance for each region.

### Distance to the nearest location

Work out the distance from each supporter's postcode to the nearest of several locations, such as your regional offices or shops:

Expression

```apteco
UKPostcodeDistMin([Postcode], "SW1A 1AA", "EH99 1SP", "CF99 1NA")
```

Returns a number of miles to the nearest location. Filter on it to find supporters within 20 miles of their nearest office.

## Text

Text functions join and tidy text. The `If` examples under [Conditions](<#conditions>) also search text, with `StrContains`. See the FastStats [string functions](<https://help.apteco.com/en/faststats/expressions/string-functions/index.md>).

### Personalised greeting

Join text and variables into a greeting:

Expression

```apteco
AddStr("Dear ",[Title]," ",[Initial]," ",[Surname])
```

Returns text, such as `Dear Mrs J Smith`. Use it as a content field in message personalisation, or as a data grid column for a mailing export.

### Tidy capital letters

Capitalise the first letter of each word, for names that were entered in lower case:

Expression

```apteco
StrProper([Forename])
```

Returns text, such as `Sarah` for `sarah`. Use it in a greeting, so names look right in your messages.

## Numbers

Maths functions round and calculate numbers. See the FastStats [maths functions](<https://help.apteco.com/en/faststats/expressions/maths-functions/index.md>).

### Suggested gift amount

Suggest a gift 20% higher than each supporter's last gift, rounded to the nearest £5:

Expression

```apteco
Round([LastGiftAmount]*1.2,5)
```

Returns a number, such as `60` for a last gift of £50. Use it in message personalisation to ask for an amount that suits each supporter.

## Conditions

Conditions return different values for different supporters. These examples use the FastStats [logical functions](<https://help.apteco.com/en/faststats/expressions/logical-functions/index.md>), [missing value functions](<https://help.apteco.com/en/faststats/expressions/missing-values/index.md>), and [selector functions](<https://help.apteco.com/en/faststats/expressions/selector-functions/index.md>).

### Check an email address

Check whether each supporter's email address is in a valid format:

Expression

```apteco
ValidEmail([Email Address])
```

Returns `1` for a valid format and `0` for an invalid one. Filter on `1` to leave out badly formatted addresses before an email campaign.

### Flag missing values

Flag supporters who have no date of birth recorded:

Expression

```apteco
If(IsMissing([DOB]),"Unknown","Known")
```

Returns text: `Unknown` or `Known`. Use it as a data grid column to check your data before an age-based campaign.

### Simple `If` statement

An `If` statement returns one value when a condition is true and another when it's false. This one returns 1 for supporters whose segment code contains Y or Z, and 0 for everyone else:

Expression

```apteco
If(StrContains(CodeOf([Segment]),"Y", "Z"),1,0)
```

Returns `1` or `0`. Use it for basic segmentation, or as a flag for reporting.

### `If` statement with text values

An `If` statement can return text as well as numbers. This one asks supporters whose last gift was £50 or more for £75, and everyone else for £40:

Expression

```apteco
If([LastGiftAmount]>=50,"£75","£40")
```

Returns text: `£75` or `£40`. Use it in message personalisation to set the ask in an appeal.

### `If` statement with two conditions

Combine conditions with `And` or `Or`. This one returns 1 for supporters who gave to the emergency appeal and have agreed to email or SMS contact:

Expression

```apteco
If(And(DescOf([Appeal])="Emergency",StrContains(CodeOf([ContactPermissions]),"EM", "SMS")),1,0)
```

Returns `1` or `0`. Use it to target a follow-up to a specific group of donors.

### `If` statement with an exclusion

Combine conditions with `Not` to include some supporters and exclude others. `And` can take any number of conditions. This one returns 1 for supporters in segment Y or Z who gave to the emergency appeal, but haven't agreed to email or SMS contact:

Expression

```apteco
If(And(StrContains(CodeOf([Segment]),"Y", "Z"), DescOf([Appeal])="Emergency", Not(StrContains(CodeOf([ContactPermissions]),"EM", "SMS"))),1,0)
```

Returns `1` or `0`. Use it to find gaps, such as engaged donors you can only reach by post.

### Several bands with `Case`

When you need more than two outcomes, `Case` is easier to read than nested `If` statements. It takes pairs of a condition and the value to return when that condition is true. This one sorts supporters into tiers by their total giving:

Expression

```apteco
Case( [TotalGiven] >= 1000, "Major donor",
      And([TotalGiven] >= 100, [TotalGiven] < 1000), "Mid-level donor",
      [TotalGiven] < 100, "Regular donor")
```

Returns text, such as `Mid-level donor`. Use it as a data grid column, or in message personalisation to vary a message by tier.
