Text.BeforeDelimiter
TextReturns the portion of a text value before a specified delimiter, optionally specifying which occurrence to use.
Syntax
Text.BeforeDelimiter(text as nullable text, delimiter as text, optional index as any) as nullable textParameters
| Name | Type | Required | Description |
|---|---|---|---|
text | text | Yes | The source text value. |
delimiter | text | Yes | The delimiter string to search for. |
index | any | No | A zero-based occurrence index (number) or a two-item list {index, RelativePosition.FromEnd} to count from the end. Defaults to 0 (first occurrence). |
Return Value
text — The text before the specified occurrence of delimiter.
Remarks
Text.BeforeDelimiter returns all text that precedes the specified delimiter occurrence. By default it uses the first occurrence (index 0). This is the complement of Text.AfterDelimiter — together they let you extract either side of any delimiter without needing to split the string into a list first.
The index parameter offers two forms of occurrence control:
- A non-negative integer n: the zero-based occurrence index (0 = first, 1 = second, etc.)
- A two-element list {n, RelativePosition.FromEnd}: count n occurrences from the end of the string
Unlike Text.AfterDelimiter, when the delimiter is not found, Text.BeforeDelimiter returns the entire original text — not an empty string. This difference is worth noting if you use both functions together: a missing delimiter will cause them to return different sentinel values. If text is null, the function returns null.
The delimiter search is always case-sensitive. There is no comparer parameter, so if you need case-insensitive delimiter matching, use Text.PositionOf with Comparer.OrdinalIgnoreCase to locate the position, then Text.Start to extract the prefix.
Examples
Example 1: Extract the first segment before a delimiter
Text.BeforeDelimiter("Sales/East/Q1", "/")Result | |
|---|---|
| 1 | Sales |
Example 2: Extract all text before the second occurrence
Text.BeforeDelimiter("US-WA-Seattle", "-", 1)Result | |
|---|---|
| 1 | US-WA |
Example 3: Extract the path excluding the last segment
Text.BeforeDelimiter("Sales/East/Q1/Alice", "/", {0, RelativePosition.FromEnd})Result | |
|---|---|
| 1 | Sales/East/Q1 |
Example 4: Extract the username from an email column
Table.AddColumn(
#table({"Email"}, {{"alice@example.com"}, {"bob@contoso.com"}}),
"Username",
each Text.BeforeDelimiter([Email], "@")
)Email | Username | |
|---|---|---|
| 1 | alice@example.com | alice |
| 2 | bob@contoso.com | bob |