How to Use Notion Formula slice() With Negative Index for End Extraction
🔍 WiseChecker

How to Use Notion Formula slice() With Negative Index for End Extraction

Notion formulas provide a slice() function that extracts a substring from a text property. When you need to extract characters from the end of a string, using a negative index for the end parameter lets you specify positions relative to the last character. This article explains how slice() interprets negative indices and how to use them to extract text from the end of a string reliably. You will learn the exact syntax, see practical examples, and understand common pitfalls.

Key Takeaways: Using slice() With Negative End Index

  • slice(start, end) with negative end: Extracts characters from start up to length + end positions from the string end.
  • Example: slice(-4, -1): Returns the substring starting 4 characters from the end and ending 1 character before the string end.
  • Common mistake: slice(2, -2) vs slice(-5, -2): The first extracts from index 2 to near the end; the second extracts from near the end to near the end.

ADVERTISEMENT

How the slice() Function Handles Negative Indices in Notion

The slice() function in Notion formulas accepts two arguments: start and end. Both can be positive, zero, or negative integers. A positive index counts from the beginning of the string, starting at 0. A negative index counts from the end of the string, where -1 is the last character, -2 is the second last, and so on. The extracted substring includes characters from the start position up to, but not including, the end position. When the end argument is negative, Notion calculates the actual end position as string length + end. For example, if the string has 10 characters and you set end to -3, the extraction stops at position 7 (10 + -3). This behavior lets you extract a fixed number of characters from the end without knowing the string length in advance.

Syntax and Behavior Rules

The full syntax is slice(text, start, end). The text must be a string property or a string literal. The start and end arguments are numbers. If start is greater than or equal to end, the result is an empty string. If end is omitted, the extraction goes to the end of the string. Negative indices are converted to positive positions using the formula actualIndex = length + negativeIndex. If that conversion results in a value less than 0 (for example, length is 5 and negative index is -10), the actual index is clamped to 0. This clamping can cause unexpected results if you use a negative index larger in magnitude than the string length.

Steps to Write a slice() Formula With a Negative End Index

Follow these steps to create a Notion formula that extracts a substring from the end of a text property using a negative end index.

  1. Open the database and add a formula property
    Go to your Notion database. Click the + button in the last column header. Select Formula from the property type list. Name the property, for example “Last Three Chars”.
  2. Write the slice() expression
    In the formula editor, type slice(prop("Your Text Property"), -3, -1). Replace “Your Text Property” with the exact name of your text column. This extracts characters starting from 3 positions from the end up to 1 position from the end — effectively the last 2 characters.
  3. Test with sample data
    Enter a string like “Notion” in the text property. The formula should return “on” because the string length is 6, start is -3 (position 3), end is -1 (position 5), so it extracts characters at indices 3 and 4 (“o” and “n”).
  4. Adjust to extract more characters
    To extract the last N characters, use slice(prop("Text"), -N, -1) and then remove the last character manually, or use slice(prop("Text"), -N, length(prop("Text"))) to include the last character. For example, to get the last 3 characters of “Notion”, use slice(prop("Text"), -3, length(prop("Text"))) which returns “ion”.
  5. Use a variable for dynamic length
    If you need to extract a variable number of characters from the end, you can calculate the start index as length(prop("Text")) - N where N is a number property. For instance, slice(prop("Text"), length(prop("Text")) - prop("Count"), length(prop("Text"))) extracts the last Count characters.

Alternative Method: Using slice() With a Negative Start and Positive End

If you want to include the last character of the string, the easiest approach is to omit the end argument entirely. For example, slice(prop("Text"), -5) returns the last 5 characters of the string. This method is simpler and avoids confusion with negative end indices. Use the negative end index only when you need to stop extraction before the string end.

ADVERTISEMENT

Common Mistakes When Using Negative End Indices

slice(-3, -1) Returns Only Two Characters, Not Three

Many users expect slice(-3, -1) to return the last three characters. But because the end index is exclusive, the extraction stops one character before the end. The result is only the third-last and second-last characters. To get the last three characters, use slice(-3, 0) or simply slice(-3). Note that slice(-3, 0) works because 0 is treated as the end of the string, not the beginning, when used with a negative start. This is a known quirk in Notion’s formula engine.

Negative End Index Larger Than String Length Produces Empty String

If the string length is 5 and you use slice(0, -10), the actual end index becomes 5 + (-10) = -5, which is clamped to 0. The start is 0 and the end is 0, so the result is an empty string. To avoid this, ensure the absolute value of the negative end index is less than or equal to the string length. You can guard with an if statement: if(length(prop("Text")) >= 5, slice(prop("Text"), -5, -1), "").

Using a Positive Start With a Negative End May Not Extract From the End

A formula like slice(prop("Text"), 2, -2) starts at index 2 (third character) and ends at length - 2. This extracts a portion from the middle of the string, not from the end. If your goal is to extract from the end, both start and end should be negative, or start should be negative and end omitted.

slice() With Negative End: Common Use Cases Compared

Use Case Formula Result on “Notion” (length 6)
Last 2 characters (excluding last char) slice(prop(“Text”), -3, -1) “on”
Last 3 characters (including last char) slice(prop(“Text”), -3) “ion”
Last 3 characters (using length) slice(prop(“Text”), -3, length(prop(“Text”))) “ion”
Middle extraction (start 2, end -2) slice(prop(“Text”), 2, -2) “ti”
All but last character slice(prop(“Text”), 0, -1) “Notio”

You can now use slice() with negative end indices to extract substrings from the end of any text property in Notion. Experiment with the formulas in a test database to see how different combinations behave. For advanced use, combine slice() with length() to create dynamic extractions based on other property values.

ADVERTISEMENT