Formulas
A formula computes a new column from the others, row by row, as in a spreadsheet. You write it in Prepare, with the Compute button (a computed column), or for a KPI value From a formula.
You don’t have to write it yourself:
- Describe what you want to do right in the Formula field, then Enter: Matr replaces your text with the formula. A formula that does not work is fixed the same way, with Write the formula with AI.
- The templates under the editor write the usual formulas in one click (if… then… else, difference, rate, tiers, time between two dates…). Columns are already picked: fix what is selected, Tab moves to the next slot. With no date column in the data, the date templates are greyed out.
- Type
[to pick a column (with its type), even in the middle of a description. As in a spreadsheet, a formula starts with a function or a column: as soon as the field starts one, Matr suggests columns and functions as you type. In a description, it only suggests the columns whose name starts with the word you are typing. Ctrl+Space (Alt+`on Mac) opens the list at any time. On the value compared to a text or number column ([status] = '…',[children] > 2,CONTAINS([label], '…')), Matr suggests the column’s values, most frequent first. Enter or Tab inserts the suggestion, Esc closes the list.
When your request can be read several ways, Matr asks a question with one to three answers: clicking an answer writes its formula.
Matr checks the formula while you write it. When something is wrong, the message says what to fix and points at the culprit.
The basics
Section titled “The basics”Referencing a column
Section titled “Referencing a column”A column goes between square brackets, with its exact name: [revenue], [order_date]. Case and accents matter: [Revenue] and [revenue] are two different columns.
Columns created by an earlier step of the transformation (another computed column, a join) are referenced the same way.
Writing a text
Section titled “Writing a text”A text goes between single quotes: 'Churned'. For an apostrophe inside the text, double it: 'O''Reilly'.
Double quotes ("Churned") are not accepted.
Writing a number
Section titled “Writing a number”A number uses a point for decimals: 12.5. The comma separates the arguments of a function: ROUND([price], 2).
Operators
Section titled “Operators”| Operator | Used to | Example |
|---|---|---|
+ - * / | Compute with numbers | [revenue] - [cost] |
& | Glue two texts | [first_name] & ' ' & [last_name] |
= <> | Equal, different | [status] = 'Churned' |
> >= < <= | Greater, smaller | [revenue] >= 1000 |
AND | Both conditions are true | [revenue] > 0 AND [status] = 'Active' |
OR | At least one condition is true | [country] = 'FR' OR [country] = 'BE' |
NOT | The opposite of a condition | NOT IS_EMPTY([email]) |
( ) | Group a calculation | ([revenue] - [cost]) / [revenue] |
A comparison gives a yes/no column (true or false). The words TRUE and FALSE stand for these two values.
Comparing texts is case-sensitive: [status] = 'churned' does not find “Churned”. To look for a piece of text regardless of case, use CONTAINS.
The empty value
Section titled “The empty value”NULL stands for the absence of a value. It is mostly used as a result: IF([revenue] = 0, NULL, [margin] / [revenue]).
To test whether a value is empty, use IS_EMPTY: [email] = NULL is refused, because that comparison would never be true.
Every column has a type: number, text, date or yes/no. The suggestions show it next to the name. Matr refuses to mix types, rather than return a wrong result without a word:
- you only compute with numbers: a text that holds digits is converted with
TO_NUMBER; - you only glue texts with
&: a number or a date is converted withTO_TEXT; - a date cannot be subtracted from another: the gap is computed with
DAYS_BETWEENorYEARS_BETWEEN.
Functions
Section titled “Functions”Function names are written in upper or lower case. Every function also has a French name, accepted the same way (shown in brackets): the editor of a French-speaking account suggests those.
Conditions and empty values
Section titled “Conditions and empty values”(SI) Returns one value when the condition is true, another otherwise.
- Expects:
IF(condition, if_true, if_false). All three arguments are required. - Returns:
if_trueorif_false. Both must have the same type (two numbers, two texts…);NULLgoes with every type. - Example:
IF([revenue] >= 1000, 'Large', 'Small')gives “Large” for a revenue of 2,500, “Small” for 300.
Several IF can be nested for more than two cases: see the recipe Group into tiers.
COALESCE
Section titled “COALESCE”(SINON) Returns the value, or a replacement when it is empty.
- Expects:
COALESCE(value, if_empty), or more arguments: the first one that is not empty wins. - Returns: the type of the values, which must have the same type.
- Example:
COALESCE([discount], 0)gives 0 where the discount is empty, the discount elsewhere.
IS_EMPTY
Section titled “IS_EMPTY”(EST_VIDE) Tests whether a value is empty.
- Expects:
IS_EMPTY(value). - Returns: yes/no.
- Example:
IS_EMPTY([email])is true for customers without an email.NOT IS_EMPTY([email])is true for the others.
Numbers
Section titled “Numbers”(ARRONDI) Rounds a number.
- Expects:
ROUND(number, decimals). The decimals are a whole number from 0 to 15; without them, the number is rounded to the unit. - Returns: a number.
- Example:
ROUND([price] * 1.2, 2)gives 14.39 for a price of 11.99.
(ABS) The absolute value: the number without its sign.
- Expects:
ABS(number). - Returns: a positive number or zero.
- Example:
ABS([planned] - [actual])gives 50 whether the gap is +50 or −50.
TO_NUMBER
Section titled “TO_NUMBER”(NOMBRE) Converts a text into a number.
- Expects:
TO_NUMBER(text). - Returns: a number. A value that does not read as a number becomes empty, and Matr warns you. Numbers written the French way (
12,5,1 234) are among them. - Example:
TO_NUMBER([amount_text])gives 1250 for the text “1250”.
TO_TEXT
Section titled “TO_TEXT”(TEXTE) Converts a value into text.
- Expects:
TO_TEXT(value): a number, a date or a yes/no. - Returns: a text. A date is written
2024-03-15. A number stored with decimals keeps its.0: 1042 stored as a decimal gives “1042.0”. - Example:
'Order #' & TO_TEXT([number])gives “Order #1042”.
(MAJUSCULE) Turns a text into upper case.
- Expects:
UPPER(text). - Returns: a text.
- Example:
UPPER([city])gives “PARIS” for “Paris”.
(MINUSCULE) Turns a text into lower case.
- Expects:
LOWER(text). - Returns: a text.
- Example:
LOWER([email])gives “john@example.com” for “John@Example.com”.
(SANS_ESPACES) Removes the spaces at the start and end of a text. Spaces in the middle stay.
- Expects:
TRIM(text). - Returns: a text.
- Example:
TRIM([code])gives “A12” for “ A12 ”.
LENGTH
Section titled “LENGTH”(LONGUEUR) The number of characters of a text.
- Expects:
LENGTH(text). - Returns: a number.
- Example:
LENGTH([zip_code])gives 5 for “75011”.
(GAUCHE) The first characters of a text.
- Expects:
LEFT(text, n), wherenis the number of characters kept. - Returns: a text.
- Example:
LEFT([zip_code], 2)gives “75” for “75011”: the French département.
(DROITE) The last characters of a text.
- Expects:
RIGHT(text, n). - Returns: a text.
- Example:
RIGHT([reference], 4)gives “2024” for “INV-2024”.
REPLACE
Section titled “REPLACE”(REMPLACER) Replaces every occurrence of a piece of text with another.
- Expects:
REPLACE(text, searched, replacement). - Returns: a text.
- Example:
REPLACE([phone], ' ', '')gives “0601020304” for “06 01 02 03 04”.
CONTAINS
Section titled “CONTAINS”(CONTIENT) Tests whether a text contains a piece of text, ignoring case.
- Expects:
CONTAINS(text, searched). - Returns: yes/no.
- Example:
CONTAINS([label], 'promo')is true for “PROMO summer” as well as for “Promo code”.
DAYS_BETWEEN
Section titled “DAYS_BETWEEN”(JOURS_ENTRE) The number of days between two dates.
- Expects:
DAYS_BETWEEN(start, end), two dates. A time of day, if any, is ignored: only days count. - Returns: a whole number of days, negative when the end is before the start.
- Example:
DAYS_BETWEEN([order_date], [delivery_date])gives 3 for an order of March 12 delivered on March 15.
YEARS_BETWEEN
Section titled “YEARS_BETWEEN”(ANNEES_ENTRE) The number of completed years between two dates.
- Expects:
YEARS_BETWEEN(start, end), two dates. - Returns: a whole number of years. A year only counts once it is complete, like an age.
- Example:
YEARS_BETWEEN([birth_date], [order_date])gives 33 for a customer born on May 15, 1990 who orders on March 1, 2024: her birthday has not come yet.
(ANNEE) The year of a date.
- Expects:
YEAR(date). - Returns: a number.
- Example:
YEAR([order_date])gives 2024 for March 15, 2024.
(MOIS) The month number of a date, from 1 to 12.
- Expects:
MONTH(date). - Returns: a number.
- Example:
MONTH([order_date])gives 3 for March 15, 2024.
(JOUR) The day of the month of a date, from 1 to 31.
- Expects:
DAY(date). - Returns: a number.
- Example:
DAY([order_date])gives 15 for March 15, 2024.
WEEKDAY
Section titled “WEEKDAY”(JOUR_SEMAINE) The day of the week of a date, as a number: 1 for Monday … 7 for Sunday.
- Expects:
WEEKDAY(date). - Returns: a number from 1 to 7.
- Example:
WEEKDAY([order_date])gives 5 for Friday, March 15, 2024.
START_OF_MONTH
Section titled “START_OF_MONTH”(DEBUT_MOIS) The first day of the month of a date. Handy to group by month with Group.
- Expects:
START_OF_MONTH(date). - Returns: a date.
- Example:
START_OF_MONTH([order_date])gives March 1, 2024 for March 15, 2024.
(AUJOURDHUI) Today’s date, at the time of the calculation: the result changes from one day to the next.
- Expects: nothing, but the parentheses are required:
TODAY(). - Returns: a date.
- Example:
DAYS_BETWEEN([due_date], TODAY())gives how many days an invoice is overdue.
Recipes
Section titled “Recipes”Ready-to-copy formulas. Replace the names in square brackets with those of your columns.
Margin and margin rate
Section titled “Margin and margin rate”- Margin:
[revenue] - [cost] - Margin rate in %, rounded to one decimal:
ROUND(([revenue] - [cost]) / [revenue] * 100, 1)
When the revenue is 0, the rate is empty (see Division by zero).
Year-over-year change
Section titled “Year-over-year change”ROUND(([revenue_2024] - [revenue_2023]) / [revenue_2023] * 100, 1) gives the change in %.
Group into tiers
Section titled “Group into tiers”IF([amount] >= 10000, 'Key account', IF([amount] >= 1000, 'Medium', 'Small'))
Thresholds are tested from the highest to the lowest: the first true condition wins. The Group into tiers template writes this formula for you.
Customer seniority
Section titled “Customer seniority”- Between two dates:
YEARS_BETWEEN([created_at], [last_order_date]) - Until today:
YEARS_BETWEEN([created_at], TODAY())
Delivery time in days
Section titled “Delivery time in days”DAYS_BETWEEN([order_date], [delivery_date])
Month of an order
Section titled “Month of an order”START_OF_MONTH([order_date]) brings each order back to the first day of its month. Then group by this column to get a total per month.
Weekend orders
Section titled “Weekend orders”WEEKDAY([order_date]) >= 6 is true for orders placed on a Saturday or a Sunday.
Replace an empty value
Section titled “Replace an empty value”- With a number:
COALESCE([discount], 0) - With a text:
COALESCE([region], 'Not provided')
Yes/no flag
Section titled “Yes/no flag”[status] = 'Churned' AND [revenue] > 0 is true for churned customers who still bring revenue. Then filter on this column.
Full name
Section titled “Full name”[first_name] & ' ' & UPPER([last_name]) gives “John SMITH”.
Clean up a code
Section titled “Clean up a code”UPPER(TRIM([customer_code])) gives “AB12” for “ ab12”. Useful before a join between two tables that do not write their codes the same way.
Spot a word in a label
Section titled “Spot a word in a label”IF(CONTAINS([label], 'refund'), 'Credit note', 'Sale')
Usual errors
Section titled “Usual errors”The column does not exist
Section titled “The column does not exist”“Column “revenu” does not exist here.” The name in square brackets must be exactly the column’s name, case and accents included. Type [ to pick from the list rather than typing the name.
Missing bracket
Section titled “Missing bracket”“Write [revenue] to reference the revenue column.” A name without brackets is not recognized as a column. “A closing bracket is missing” points at a [ that is never closed.
Text between double quotes
Section titled “Text between double quotes”A text goes between single quotes: 'Churned', not "Churned".
A text in a calculation
Section titled “A text in a calculation”“price is text: it cannot be used in a calculation.” The column holds digits but is stored as text. Convert it: TO_NUMBER([price]) * 2.
Two types compared
Section titled “Two types compared”“Comparing revenue (a number) to threshold (text): these are not comparable.” Convert one of them so that both have the same type: [revenue] > TO_NUMBER([threshold]).
A number glued to a text
Section titled “A number glued to a text”“& expects text: “age” is a number.” Convert the number: [first_name] & ' is ' & TO_TEXT([age]).
Two results of different types
Section titled “Two results of different types”“Both possible results must have the same type.” In an IF or a COALESCE, the results must all be numbers or all be texts: IF([revenue] > 0, 'Yes', 0) is refused, IF([revenue] > 0, 'Yes', 'No') is accepted.
An unknown function
Section titled “An unknown function”““AVERAGE” does not exist in Prepare formulas.” Only the functions on this page exist. Ctrl+Space in the editor shows the available ones.
A comma in a number
Section titled “A comma in a number”“ROUND expects 2 arguments, it received 3.” ROUND([price], 12,5) reads as three arguments: the comma separates arguments. Decimals use a point: 12.5.
Testing emptiness with “=”
Section titled “Testing emptiness with “=””[email] = NULL is refused. Write IS_EMPTY([email]).
Subtracting two dates
Section titled “Subtracting two dates”[delivery_date] - [order_date] is refused. Write DAYS_BETWEEN([order_date], [delivery_date]).
Division by zero
Section titled “Division by zero”It is not an error: a division by 0 gives an empty cell, and Matr points it out below the preview. To put another value instead, wrap the calculation in COALESCE: COALESCE([margin] / [revenue], 0).
What you cannot do
Section titled “What you cannot do”- Compute over several rows. A formula works row by row: no sum, average or count of a whole column. Use Group in Prepare.
- Read another table. A formula only sees the columns of the current table. First bring the other table’s columns with Join.
- Type a date by hand. There is no date literal: compare to a date column or to
TODAY(). YEARS_BETWEENon a live Redshift or Snowflake source. These sources cannot compute an exact age: Prepare refuses the step. Import the table, or useDAYS_BETWEEN.