Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Last active March 2, 2024 20:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typeoneerror/eb743867c7c6a1e08d0fe351be2e4fa0 to your computer and use it in GitHub Desktop.
Save typeoneerror/eb743867c7c6a1e08d0fe351be2e4fa0 to your computer and use it in GitHub Desktop.
Pseudo Notion rollup calculations as Formulas 2.0
lets(
values, prop("Values").map(current.prop("Value")).filter(!current.empty() || current == 0),
values.sum() / values.length()
)
prop("Values").length()
prop("Values")
.map(current.prop("Value"))
.filter(!current.empty() || current == 0)
.length()
lets(
/* Sorted list of non-empty dates */
dates, prop("Values").map(current.prop("Date")).filter(!current.empty()).sort(),
/* Create a date range with the first and last dates */
range, dateRange(dates.first(), dates.last()),
/* How many periods between the dates */
days, dateBetween(range.dateEnd(), range.dateStart(), "days"),
months, dateBetween(range.dateEnd(), range.dateStart(), "months"),
years, dateBetween(range.dateEnd(), range.dateStart(), "years"),
/* Some years and months have more/less days, but this is the simplest way to do this that is close enough to Notion's output */
count, ifs(
years > 0, days / 365,
months > 0, days / 30,
days
),
/* Notion's rollup displays years, months or days based on how big the range is */
period, ifs(
years > 0, "year",
months > 0, "month",
"day"
),
/* Round to nearest 10th */
count, round(count * 10) / 10,
/* Add period to count and "s" if only 1 */
count + " " + period + (count == 1 ? "" : "s")
)
lets(
values, prop("Values").map(current.prop("Value")),
values.max()
)
lets(
values, prop("Values").map(current.prop("Value")).filter(!current.empty() || current == 0).sort(),
mid, (values.length() / 2).floor(),
value, values.length() % 2 == 0 ? (values.at(mid) + values.at(mid - 1)) / 2 : values.at(mid),
value
)
lets(
values, prop("Values").map(current.prop("Value")),
values.min()
)
lets(
values, prop("Values").map(current.prop("Value")).filter(!current.empty() || current == 0).sort(),
ifs(values.length() > 1, values.last().toNumber() - values.first().toNumber(), 0)
)
prop("Values")
.map(current.prop("Value"))
.sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment