Text.BeforeDelimiter

Text

Returns the portion of a text value before a specified delimiter, optionally specifying which occurrence to use.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Text.BeforeDelimiter(text as nullable text, delimiter as text, optional index as any) as nullable text

Parameters

NameTypeRequiredDescription
texttextYesThe source text value.
delimitertextYesThe delimiter string to search for.
indexanyNoA 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

textThe 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
Result
1Sales

Example 2: Extract all text before the second occurrence

Text.BeforeDelimiter("US-WA-Seattle", "-", 1)
Result
Result
1US-WA

Example 3: Extract the path excluding the last segment

Text.BeforeDelimiter("Sales/East/Q1/Alice", "/", {0, RelativePosition.FromEnd})
Result
Result
1Sales/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], "@")
)
Result
Email
Username
1alice@example.comalice
2bob@contoso.combob

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks