TEXTBEFORE
Short answer
Grab text before or after a delimiter
TEXTBEFORE(text, delimiter, [instance_num], [match_mode], [match_end], [if_not_found])
- Category
- Text & Data Cleanup
- Excel version
- 2024+
- Also searched as
- textafter, extract before delimiter, extract after delimiter, left of delimiter, last segment of account code, vendor name from description
What it returns
TEXTBEFORE returns the text that sits before a delimiter. TEXTAFTER, its twin, returns the text after it. Same arguments, same order. This page covers both.
=TEXTBEFORE("100-4200-Sales", "-") returns 100. =TEXTAFTER("100-4200-Sales", "-") returns 4200-Sales. By default both work from the first delimiter.
The instance_num argument changes which delimiter counts. =TEXTBEFORE("100-4200-Sales", "-", 2) returns 100-4200. A negative instance counts from the end, so =TEXTAFTER("100-4200-Sales", "-", -1) returns Sales regardless of how many segments come before it.
If the delimiter is not there, both return #N/A. That is a feature in a model: a code with the wrong shape announces itself.
Unlike TEXTSPLIT, both functions accept a whole column and spill one result per row. Point them at GL[Segment] and you get a column back.
Arguments that matter
| Argument | Required | What it does |
|---|---|---|
text | Yes | The string to search. A cell, or a column such as GL[Memo], in which case the result spills down. |
delimiter | Yes | The character or string to split at. An array constant such as {"-","."} matches any of them. |
instance_num | No | Which occurrence to use. 1 by default. Negative counts from the end. 0 is an error. |
match_mode | No | 0 case-sensitive (default). 1 case-insensitive. Matters for word delimiters like " INV ". |
match_end | No | 0 (default) or 1. With 1, the end of the text counts as a delimiter. |
if_not_found | No | What to return when the delimiter or instance is missing. Default is #N/A. |
Two deserve attention.
instance_num is the whole point. Counting delimiters used to mean FIND nested inside FIND. Now it is a number. Negative values matter most for codes whose segment count varies: some entities add a project segment, some do not, and -1 still lands on the last one.
if_not_found carries the same trade-off as it does in XLOOKUP. A visible flag such as "BAD CODE" can be counted. An empty string drops the row out of every downstream total with no trace.
In a financial model
Segmented account strings and free-text memo fields are where this pair earns its keep. The entity prefix decides which company a row belongs to. The natural account decides its statement line. The vendor name sits after a fixed label in the memo.
Each of those is one segment, and a model usually wants one segment per column. That is the job TEXTBEFORE and TEXTAFTER do better than TEXTSPLIT, because they return a single value per row and so work inside a Table.
The extract stays as delivered, per raw data stays raw. The parsed columns sit beside it. The account column then feeds the mapping table described in one mapping table per dimension, and XLOOKUP attaches the statement line.
Worked example
A GL extract formatted as a Table named GL, with columns Segment and Memo. Segment strings look like 100-4200-Sales. Some entities add a project, 210-6100-Ops-P07. Memos look like AP INV 88213 / Northwind Freight.
Entity, as a calculated column in the Table:
=TEXTBEFORE([@Segment], "-")
Natural account, the text between the first and second hyphen, converted to a number so it matches the mapping table:
=--TEXTBEFORE(TEXTAFTER([@Segment], "-"), "-")
Vendor, the text after the slash, with a visible flag when the memo has no slash:
=TRIM(TEXTAFTER([@Memo], "/", -1, , , "NO VENDOR"))
| Segment | Memo | Entity | Account | Vendor |
|---|---|---|---|---|
| 100-4200-Sales | AP INV 88213 / Northwind Freight | 100 | 4200 | Northwind Freight |
| 210-6100-Ops-P07 | AP INV 90114 / Contoso Leasing | 210 | 6100 | Contoso Leasing |
| 100-7300-Fin | Accrual reversal | 100 | 7300 | NO VENDOR |
The account formula ignores the project segment because it only reads up to the second hyphen. Add a check cell with =COUNTIF(GL[Vendor], "NO VENDOR") so AP rows missing a vendor are counted, not hidden.
Old way vs new way
The entity prefix, the old way:
=LEFT([@Segment], FIND("-", [@Segment])-1)
The new way:
=TEXTBEFORE([@Segment], "-")
The last segment of a variable-length code, old way:
=TRIM(RIGHT(SUBSTITUTE([@Segment],"-",REPT(" ",100)),100))
New way:
=TEXTAFTER([@Segment], "-", -1)
The old versions count characters and rely on tricks nobody reviewing the model will recognize. The new ones name the delimiter and the instance, which is what the reader needs to check.
Where it breaks
TEXTBEFORE returns #N/A on some rows. The delimiter does not appear in those strings, or instance_num asks for more occurrences than exist. A two-segment code has no second hyphen. Fix: decide whether the short code is legitimate. If it is, set match_end to 1 so the end of the string counts as a delimiter. If it is not, set if_not_found to a flag and count it.
TEXTBEFORE returns #VALUE!. instance_num is 0, or larger than the length of the text. Fix: use a positive number to count from the start or a negative one to count from the end.
The account matches nothing in the mapping table. The extracted 4200 is text and the mapping key is a number. Fix: convert with -- as in the example, or keep both sides as text.
Results carry stray spaces. Delimiters padded with spaces, as in INV 88213 / Northwind, leave a space on each side of the result. Fix: include the spaces in the delimiter, " / ", or wrap in TRIM.
A word delimiter misses some rows. " INV " with the default case-sensitive match skips memos typed as inv. Fix: set match_mode to 1.
#NAME? in an older version. Both functions need Excel 2024 or Microsoft 365. Fix: if the file must open in 2021 or earlier, parse the column in Power Query at load.
Related functions
- TEXTSPLIT. Every segment at once, spilled across. Use it for one string at a time. Use TEXTBEFORE and TEXTAFTER when you need one segment for a whole column or inside a Table.
- REGEXEXTRACT. When the value is defined by its shape, such as an eight-digit PO number that can sit anywhere in a memo, rather than by a delimiter.
- XLOOKUP. Maps the extracted account to its statement line.
- COUNTIFS. Counts the not-found flags in a check row so malformed codes surface at load.
- SUBSTITUTE. Normalizes inconsistent delimiters before you parse, such as turning dots into hyphens.
