Skip to main content
Version: 0.2

Formulas

This chapter covers the Formula property: how to add one, the formula editor, how a formula is written, and every function Leafen's formulas know, with an example and its result. It is for anyone who computes a value from a row's other properties, and for agents that write formulas through MCP. The other property types are in Properties; rollups, which compute over related rows without a formula, are in Relations and rollups.

Add a formula property​

  1. In a table, press + at the end of the column headers. A field for the property's name ("Type property name…") opens over the list of types under Select type.
  2. Choose Formula. A column named "Formula" is added.
  3. Press one of its cells. The Edit formula window opens.
  4. Type the formula, for example prop("Price") * prop("Quantity").
  5. Close the window with ✕, Escape or a press outside it. The formula is kept as written, even one with a problem; Discard all changes goes back to the formula the window opened with.

The formula runs on every row of the data source. A formula column has no values of its own: you change the formula, not the cells. To change it later, press a cell, or open the column's menu and choose Edit property › Edit formula. When the formula gives a number, the same Edit property menu also has Number format, Decimal places and Show as (Number, Bar or Ring), as a Number property has (Properties).

The formula editor​

The Edit formula window has the formula field ("Your formula") at the top. A property you name with prop("…") turns into a token with the property's name. Under the field:

  • Preview: the result on one row of the data source, with the row's name as a menu to pick another row, and Type: the result's type (Text, Number, Checkbox, Date, List, Page, User, or Unknown while there is nothing to run). A formula with a problem shows the problem instead, for example "Math doesn’t work between text and number values. [0,7]", where the numbers in brackets are the first and last character positions of the part with the problem
  • Debug mode, a switch that shows the value of each part of the formula. This browser remembers it.
  • A list of Properties (the data source's properties), Built-ins (the operators) and the functions by group: General, Text, Number, Date, People, List and Special. Typing narrows it to what fits where the caret is. Up and Down move in it and Enter writes the chosen entry at the caret. Beside the list, the chosen entry's description shows examples with their results, each with a copy button.

Writing a formula​

Values​

KindWritten asType shown
Number12, -3.5Number
Text"Plan B" or 'Plan B'; \", \\, \n and \t insideText
Checkboxtrue, falseCheckbox
List[1, 2, 3]List
Datefrom a date property, now(), today() or parseDate("2026-03-14")Date
Personfrom a Person, Created by or Last edited by propertyUser
Pagefrom a Relation propertyPage

A comment is written between /* and */.

Properties​

prop("Name") reads the property named Name on the same row. The editor shows each reference as a token with the property's name; renaming the property renames the reference. A property's value has the type its property type gives it:

Property typeType in a formula
NumberNumber
CheckboxCheckbox
Date, Created time, Last edited timeDate
Multi-select, Person, RelationList
Title, Text, URL, Email, Phone, Select, Status, Files & mediaText

A relation's pages are a list of pages. prop("Relation").first().prop("Due") reads the Due property of the first related page.

Calling functions​

A function is called by name, upper("loud"), or as a method of its first value, "loud".upper(). Both give the same result, and methods chain: "Blueberry".upper().length() is 9.

Inside map(), filter(), find(), findIndex(), some(), every(), count() and sort(), current is the list item being looked at and index its position, starting at 0. map([1, 2, 3], current * 10) is 10, 20, 30, and map(["a", "b"], current + index) is a0, b1.

sort() sorts by its items, or by the value its second value, an expression, gives each item: sort(["pear", "fig", "banana"], length(current)) is fig, pear, banana, and sort([3, 1, 2], current * -1) is 3, 2, 1. The lowest comes first and equal values keep their order. Numbers sort by value, dates by time and false before true when every value is one of these; anything else sorts by its text as a person reads it: letters in alphabetical order whatever their case, an upper case before its lower ("a", "B", "b", "C"), numbers inside text by their value ("x2", "X3", "x10"), and empty values last.

let(name, value, expression) names a value for use in the expression; lets() names several.

Empty values​

A property with no value is empty. empty() is true for an empty value, and for 0, "", false and []. A formula whose result is empty shows an empty cell.

empty() with nothing in the parentheses gives an empty value of no type of its own, so if(prop("Done"), now(), empty()) is a date formula: it filters and reads through the API as a date, empty where Done is unchecked. A formula that gives nothing but empty() is of undefined type; it filters and reads through the API as text.

Operators​

OperatorDoesExampleResult
+adds numbers, or joins text2 + 35
-subtracts10 - 46
*multiplies6 * 742
/divides7 / 23.5
%remainder7 % 31
^power2 ^ 101024
==equal2 == 2true
!=not equal"a" != "b"true
> >= < <=compare numbers, text or dates5 > 3true
and (&&)both truetrue and falsefalse
or (||)either truetrue or falsetrue
not (!)the oppositenot truefalse
? :the second value when the first is true, else the third10 > 5 ? "big" : "small"big

Functions​

Each table gives what the function takes (as the editor's definition names it; ? marks an optional value, ... more of the same), what it gives back, an example and the example's result.

General​

FunctionTakesGivesExampleResult
if()condition, ifTrue, ifFalsethe chosen valueif(3 > 5, "yes", "no")no
ifs()condition, ifTrue, condition2, ifTrue2, ..., elsethe value of the first true conditionifs(false, "first", true, "second", "neither")second
and()boolean, boolean, ...Checkboxand(true, 2 > 1)true
or()boolean, boolean, ...Checkboxor(false, 1 > 2)false
not()booleanCheckboxnot(2 > 1)false
empty()value?Checkbox, or an empty value when given noneempty("")true
length()valueNumber: characters of text, items of a listlength("orchard")7
format()valueTextformat(42)42
equal()value, valueCheckboxequal("x", "x")true
unequal()value, valueCheckboxunequal(1, 1)false
let()variable, value, expressionthe expression's valuelet(width, 4, width * width)16
lets()variable, value, variable2, value2, ..., expressionthe expression's valuelets(base, 6, height, 3, base * height / 2)9

Text​

Patterns in test(), match(), replace() and replaceAll() are regular expressions. A pattern that nests repetition, such as (a+)+, is refused.

FunctionTakesGivesExampleResult
substring()text, startIndex, endIndex?Textsubstring("Blueberry", 0, 4)Blue
contains()value, searchCheckboxcontains("Blueberry", "berry")true
startsWith()text, searchCheckboxstartsWith("Blueberry", "Blue")true
endsWith()text, searchCheckboxendsWith("Blueberry", "rye")false
test()text, patternCheckboxtest("Room 101", "[0-9]+")true
match()text, patternList of every matchmatch("a1 b22 c333", "[0-9]+")1, 22, 333
replace()text, pattern, replacement?Text, the first match replacedreplace("one-two-three", "-", " ")one two-three
replaceAll()text, pattern, replacement?Text, every match replacedreplaceAll("one-two-three", "-", " ")one two three
lower()textTextlower("QUIET")quiet
upper()textTextupper("loud")LOUD
trim()textText without spaces at either endtrim(" padded ")padded
repeat()text, countTextrepeat("ab", 3)ababab
padStart()text, targetLength, padStringTextpadStart("7", 3, "0")007
padEnd()text, targetLength, padStringTextpadEnd("7", 3, "*")7**
link()text, urlText that links to the addresslink("Example", "https://example.com")Example, linked
style()value, stylesText with stylesstyle("Late", "b", "red")Late in red
unstyle()value, styles?Text without the styles named, or without anyunstyle(style("Late", "b", "red"))Late
concat()list, list2, ...Listconcat([1, 2], [3])1, 2, 3
join()list, joinerTextjoin(["red", "green", "blue"], ", ")red, green, blue
split()text, separatorListsplit("red,green,blue", ",")red, green, blue

style() takes "b" (bold), "i" (italic), "u" (underline), "s" (strikethrough), "c" (code) and the colors "gray", "brown", "orange", "yellow", "green", "blue", "purple", "pink" and "red"; a color with _background added ("red_background") colors the background.

Number​

FunctionTakesGivesExampleResult
formatNumber()value, format?, precision?TextformatNumber(1234.5, "usd")$1,234.50
add()number, numberNumberadd(8, 5)13
subtract()number, numberNumbersubtract(8, 5)3
multiply()number, numberNumbermultiply(8, 5)40
mod()number, numberNumbermod(8, 5)3
pow()number, numberNumberpow(3, 4)81
divide()number, numberNumberdivide(9, 4)2.25
min()numbers or lists of numbersNumbermin(4, 9, 2)2
max()numbers or lists of numbersNumbermax([4, 9, 2])9
sum()numbers or lists of numbersNumbersum([1, 2], 3)6
median()numbers or lists of numbersNumbermedian(1, 5, 9, 20)7
mean()numbers or lists of numbersNumbermean(2, 4, 9)5
abs()numberNumberabs(-12)12
round()value, precisionNumberround(2.567, 2)2.57
ceil()numberNumberceil(2.1)3
floor()numberNumberfloor(2.9)2
sqrt()numberNumbersqrt(81)9
cbrt()numberNumbercbrt(27)3
exp()numberNumberexp(0)1
ln()numberNumberln(1)0
log10()numberNumberlog10(1000)3
log2()numberNumberlog2(8)3
sign()numberNumber: 1, -1 or 0sign(-3)-1
pi()nothingNumberround(pi(), 4)3.1416
e()nothingNumberround(e(), 4)2.7183
toNumber()valueNumbertoNumber("42")42

round() without a precision rounds to a whole number; its precision is a whole number of at most 12 decimal places. toNumber() reads text, a checkbox (true is 1) and a date (its timestamp in milliseconds); a list is refused.

formatNumber()'s format is "commas" (the default), "percent", "humanize", "bytes_binary", "bytes_decimal" or a currency code: "usd", "aud", "cad", "sgd", "eur", "gbp", "jpy", "rub", "inr", "krw", "cny", "brl", "try", "idr", "chf", "hkd", "nzd", "sek", "nok", "mxn", "zar", "twd", "dkk", "pln", "thb", "huf", "czk", "ils", "clp", "php", "aed", "cop", "sar", "myr", "ron", "ars", "uyu", "pen", "vnd", "pkr" or "ngn". Its precision is 0 to 12 decimal places.

ExampleResult
formatNumber(1234567, "commas")1,234,567
formatNumber(0.256, "percent", 1)25.6%
formatNumber(1500000, "humanize")1.5M
formatNumber(2048, "bytes_binary")2 KiB

Date​

FunctionTakesGivesExampleResult
now()nothingDate: now, with the timenow()the current date and time
today()nothingDate: today, without a timetoday()today's date
minute()dateNumber, 0–59minute(parseDate("2026-03-14T09:26Z"))26
hour()dateNumber, 0–23hour(parseDate("2026-03-14T09:26Z"))9 in UTC, 18 in Seoul
day()dateNumber: the weekday, 1 (Monday) to 7 (Sunday)day(parseDate("2026-03-14"))6
date()dateNumber: the day of the month, 1–31date(parseDate("2026-03-14"))14
week()dateNumber: the ISO week, 1–53week(parseDate("2026-03-14"))11
month()dateNumber, 1–12month(parseDate("2026-03-14"))3
year()dateNumberyear(parseDate("2026-03-14"))2026
dateAdd()date, num, unitDatedateAdd(parseDate("2026-01-31"), 1, "months")February 28, 2026
dateSubtract()date, num, unitDatedateSubtract(parseDate("2026-03-14"), 2, "weeks")February 28, 2026
dateBetween()date, date, unitNumber: the first date minus the seconddateBetween(parseDate("2026-03-14"), parseDate("2026-03-01"), "days")13
dateRange()start, endDate rangedateRange(parseDate("2026-03-01"), parseDate("2026-03-05"))March 1, 2026 → March 5, 2026
dateStart()dateRangeDatedateStart(dateRange(parseDate("2026-03-01"), parseDate("2026-03-05")))March 1, 2026
dateEnd()dateRangeDatedateEnd(dateRange(parseDate("2026-03-01"), parseDate("2026-03-05")))March 5, 2026
timestamp()dateNumber: milliseconds since January 1, 1970 UTCtimestamp(parseDate("2026-01-01T00:00Z"))1767225600000
fromTimestamp()timestampDate and time, to the minutefromTimestamp(1767225600000)January 1, 2026, 00:00 UTC
formatDate()date, format, timezone?TextformatDate(parseDate("2026-03-14"), "YYYY/MM/DD")2026/03/14
parseDate()dateTextDate, from ISO 8601 textparseDate("2026-03-14")March 14, 2026
nextWorkday()date, calendar?Date: the date if it is a working day, else the next onenextWorkday(parseDate("2026-03-01"))March 3, 2026
addWorkdays()date, count, calendar?Date moved by working daysaddWorkdays(parseDate("2026-03-13"), 2)March 17, 2026

A date with a time is read in the time zone of the person looking at it: hour() and minute() in a cell give the hour and minute there, and the server, for the API and MCP, uses UTC.

The unit of dateAdd(), dateSubtract() and dateBetween() is "years", "quarters", "months", "weeks", "days", "hours", "minutes", "seconds" or "milliseconds"; the singular ("day") works too.

formatDate() writes these parts of a date and keeps any other characters:

PatternWritesPatternWrites
YYYY2026ddddthe weekday's name
YY26dddthe weekday's short name
Qthe quarter, 1–4dthe weekday, 0 (Sunday) to 6
MMMMthe month's nameW, WWthe ISO week, WW with two digits
MMMthe month's short nameHH, Hthe hour, 0–23
MM, Mthe month, MM with two digitshh, hthe hour, 1–12
DD, Dthe day of the monthmm, mthe minute
Dothe day with its ordinal (14th)ss, sthe second
DDD, DDDDthe day of the yearA, aAM or PM, am or pm
Xseconds since 1970Z, ZZthe time zone offset, +09:00 or +0900

formatDate(parseDate("2026-03-14T09:05Z"), "dddd, MMMM Do, h:mm A", "UTC") is "Saturday, March 14th, 9:05 AM". The time zone value is a zone name such as "UTC" or "Asia/Seoul", or an offset such as "UTC+1"; without one, the date is written in the time zone of the person looking at it.

Working days​

nextWorkday() and addWorkdays() skip weekend days and public holidays, including substitute and temporary holidays, of a calendar. The calendar is "KR" (South Korea), which is also the default; it holds the holidays of 2024 to 2027, and in a year it does not hold only weekends are skipped. A calendar Leafen does not have gives an empty value, and so does a count of more than 3,660 working days. A negative count moves back. The time of day is kept.

People​

FunctionTakesGivesExampleResult
name()personTextname(prop("Created by"))the person's name
email()personTextemail(prop("Created by"))the person's email

For a list of people, use map(): prop("Assignee").map(name(current)).join(", ").

List​

FunctionTakesGivesExampleResult
at()list, indexthe item at the index, from 0at([10, 20, 30], 1)20
first()listthe first itemfirst([10, 20, 30])10
last()listthe last itemlast([10, 20, 30])30
slice()list, startIndex, endIndex?Listslice([10, 20, 30, 40], 1, 3)20, 30
splice()list, startIndex, deleteCount?, ...List with items removed and the rest insertedsplice([10, 20, 30], 1, 1, 99)10, 99, 30
sort()list, expression?List, sorted by its items or by the expressionsort(["pear", "fig", "banana"])banana, fig, pear
reverse()listListreverse([1, 2, 3])3, 2, 1
unique()listList without repeatsunique([1, 1, 2, 3, 3])1, 2, 3
includes()list, searchCheckboxincludes(["a", "b"], "b")true
find()list, conditionthe first item that matchesfind([5, 12, 30], current > 10)12
findIndex()list, conditionNumber: its index, or -1findIndex([5, 12, 30], current > 10)1
filter()list, conditionList of the items that matchfilter([5, 12, 30], current > 10)12, 30
some()list, expressionCheckbox: any item matchessome([5, 12, 30], current > 25)true
every()list, conditionCheckbox: every item matchesevery([5, 12, 30], current > 1)true
map()list, expressionList of each item's resultmap([1, 2, 3], current * 10)10, 20, 30
flat()list, list2?, ...List with inner lists openedflat([[1, 2], [3]])1, 2, 3
count()list, conditionNumber of matching items, or of all itemscount([1, 2, 3, 4], current > 2)2

Special​

FunctionTakesGivesExampleResult
id()value?Text: the ID of the page or person given, or of the row the formula is onid()the row's ID

Names, limits and defaults​

NameLimit or defaultNotes
Formula length4,096 charactersA longer formula shows "The formula is too long."
round() precision0 to 12 decimal places
formatNumber() precision0 to 12 decimal places
formatNumber() format"commas"
Working-day calendar"KR", years 2024–2027
addWorkdays() countat most 3,660 either wayMore gives an empty value.
Date unitsyears, quarters, months, weeks, days, hours, minutes, seconds, milliseconds
List positionsfrom 0at(), slice(), splice(), findIndex(), index

For agents and the API​

An agent adds a formula property with notion-create-database or notion-update-data-source, writing it in the schema as "Total" FORMULA('prop("Price") * prop("Quantity")'); to an existing data source, notion-update-data-source takes it in its statements as ADD COLUMN "Total" FORMULA('prop("Price") * prop("Quantity")'). It reads the values with notion-query-data-sources in rows mode, {"data": {"mode": "rows", "data_source_url": "collection://…"}}, or in view mode for a saved view's rows. notion-fetch and leafen_fetch on a row give its formulas' values too, the same values rows mode gives. A SQL query through notion-query-data-sources cannot read a formula column; its error says so and names the columns it can read. The server evaluates formulas with the same functions as the editor, so a value read through MCP or the API is the value the cell shows. The tools are described in the MCP reference.

Differences from Notion​

  • Leafen adds nextWorkday() and addWorkdays(), which count working days on a public holiday calendar.
  • startsWith() and endsWith() work, though the editor's list does not show them.
  • A formula that cuts an emoji in half writes the half as the replacement character (�).
  • An agent's notion-fetch of a row gives each formula's value; Notion's names it by a formulaResult:// address that its fetch does not open.
  • When a function call fails, the problem names the function, for example "round() could not run."
  • The browser and the server compute the same digits, so a cell, a filter and the API always agree on a formula's value.
  • When rows are sorted by a formula whose value is not a number (NaN), those rows go after the numbers, in either direction.