SkillNavoraData Science & AI Handbook
Advanced Excel · Lesson 5 of 12

Text Functions, Logical Functions & IF

Extract, clean, combine and reformat text with LEFT, RIGHT, MID, LEN, FIND, SEARCH, CONCAT, TEXTJOIN, TEXT and the new TEXTSPLIT/TEXTBEFORE/TEXTAFTER. Then make decisions with AND, OR, NOT, IF, nested IF, IFS, SWITCH and IFERROR.

● Live classes 3–4✦ Excel for Microsoft 365, 2026Beginner⏱ 120 min
By the end of this lesson you can
  • Extract parts of codes, names and emails with LEFT, RIGHT, MID, FIND and SEARCH
  • Combine text with &, CONCAT and TEXTJOIN and format numbers inside text with TEXT
  • Split text with Text to Columns and the TEXTSPLIT, TEXTBEFORE and TEXTAFTER functions
  • Build conditions with AND, OR, NOT and decisions with IF, nested IF, IFS and SWITCH
  • Handle errors cleanly with IFERROR and IFNA

Why text functions matter

Real data arrives as text: order IDs like IN-2025-00042, product codes, email addresses, "Last, First" names, addresses and free-text comments. Text functions let you pull out the part you need (the year, the region code, the domain) so you can analyse it.

Measuring and extracting text

Function What it returns Example Result
LEN(text) Number of characters =LEN("Pune") 4
LEFT(text, n) First n characters =LEFT("IN-2025-00042",2) IN
RIGHT(text, n) Last n characters =RIGHT("IN-2025-00042",5) 00042
MID(text, start, n) n characters from position start =MID("IN-2025-00042",4,4) 2025
FIND(find, text, [start]) Position of text (case-sensitive, no wildcards) =FIND("-","IN-2025-00042") 3
SEARCH(find, text, [start]) Position (not case-sensitive, wildcards allowed) =SEARCH("pune","PUNE-411001") 1
XExtracting parts of an order ID
ABCDEF
1Order IDCountry =LEFT(A2,2)Year =MID(A2,4,4)Order no. =RIGHT(A2,5)LengthYear as number
2IN-2025-00042IN202500042132025
3IN-2023-01519IN202301519132023
4IN-2024-02988IN202402988132024
  • B2=LEFT(A2,2)
  • C2=MID(A2,4,4)
  • D2=RIGHT(A2,5)
  • E2=LEN(A2)
  • F2=VALUE(MID(A2,4,4))
  • Each pattern is copied down/across to the other cells in its column.

Text functions always return text. MID(A2,4,4) returns the text "2025", not the number 2025. Wrap with VALUE() (or multiply by 1) to get a number you can compare or add.

When the position changes: FIND + MID

Codes rarely have fixed lengths. Combine FIND with LEFT, MID and RIGHT to cut at a delimiter wherever it is.

XSplitting emails at the @ sign
ABCD
1EmailPosition of @User nameDomain
2[email protected]11kavya.iyerabc.in
3[email protected]6imrandatacorp.com
4[email protected]11harpreet.kskillnavora.in
  • B2=FIND("@",A2)
  • C2=LEFT(A2,FIND("@",A2)-1)
  • D2=MID(A2,FIND("@",A2)+1,100)
  • Each pattern is copied down/across to the other cells in its column.

Using a large number like 100 in MID simply means "take everything to the end". Alternatively use =RIGHT(A2,LEN(A2)-FIND("@",A2)).

XFirst and last name from 'First Last'
ABCD
1Full nameFirst nameLast nameInitials
2Kavya IyerKavyaIyerKI
3Harpreet KulkarniHarpreetKulkarniHK
4Aarav KulkarniAaravKulkarniAK
FIND SEARCH
Case-sensitive Yes: FIND("a","Apple") → #VALUE! No: SEARCH("a","Apple") → 1
Wildcards * ? No Yes
Not found #VALUE! #VALUE!

A common pattern to test whether text contains a word: =ISNUMBER(SEARCH("chair",B2)) returns TRUE or FALSE.

Changing and cleaning text

Function Purpose Example → Result
UPPER, LOWER, PROPER Change case =PROPER("new delhi") → New Delhi
TRIM Remove extra spaces =TRIM(" a b ") → a b
CLEAN Remove non-printing characters Line breaks from web data
SUBSTITUTE(text, old, new, [n]) Replace by content =SUBSTITUTE("2025-04-27","-","/") → 2025/04/27
REPLACE(text, start, n, new) Replace by position =REPLACE("E101",1,1,"EMP") → EMP101
REPT(text, n) Repeat text =REPT("★",4) → ★★★★
EXACT(a, b) Case-sensitive comparison =EXACT("abc","ABC") → FALSE
TipCount how many times a character appears

=LEN(A2)-LEN(SUBSTITUTE(A2,",","")) counts commas in A2 (the length difference after removing them). Add 1 to count items in a comma-separated list.

Joining text

XThree ways to combine text
ABCDEF
1FirstLastCityWith &CONCATTEXTJOIN
2KavyaIyerMumbaiKavya Iyer (Mumbai)Kavya IyerKavya, Iyer, Mumbai
3ImranDelhiImran (Delhi)Imran Imran, Delhi

Numbers and dates inside text: TEXT

Joining a number or date directly shows its raw value: ="Sales: "&A2 gives Sales: 1234567.891, and a date shows as 46139. Use TEXT(value, format) to control how it appears.

XTEXT() inside labels
AB
1ValueLabel formula result
21,234,568Revenue: ₹1,234,568
327-04-2026Report date: 27 Apr 2026
40.1834Growth: 18.3%
527-04-2026Day: Monday

Splitting text

Text to Columns (Lesson 2) splits once into static values. Microsoft 365 adds formula versions that update automatically:

Function Example Result
TEXTBEFORE(text, delimiter, [instance]) =TEXTBEFORE("[email protected]","@") kavya.iyer
TEXTAFTER(text, delimiter, [instance]) =TEXTAFTER("IN-2025-00042","-",-1) 00042 (−1 = last hyphen)
TEXTSPLIT(text, col_delim, [row_delim]) =TEXTSPLIT("North,South,East",",") spills North / South / East across three cells
XTEXTSPLIT spills into neighbouring cells (Microsoft 365)
ABCDE
1Input
2IN-2025-00042IN
3FurnitureChairsApex Chair 412
4Furniture > Chairs > Apex Chair 412

The blue cells are a spill range: one formula fills several cells. If any of those cells already contain something, the formula shows #SPILL! (Lesson 6).

2026 updateRegular expressions in Excel (2025)

Microsoft 365 has added REGEXTEST, REGEXEXTRACT and REGEXREPLACE. For example =REGEXEXTRACT(A2,"\d{6}") pulls a 6-digit PIN code out of an address, and =REGEXTEST(A2,"^[\w.]+@[\w.]+\.\w+$") checks that an email looks valid. They are covered with Python regex in the Python track.

Logical functions: TRUE or FALSE

Comparisons such as =B2>100 return TRUE or FALSE. Logical functions combine them:

Function Returns TRUE when Example
AND(c1, c2, …) All conditions are TRUE =AND(B2>=4, C2="Sales")
OR(c1, c2, …) At least one condition is TRUE =OR(D2="Delhi", D2="Mumbai")
NOT(c) The condition is FALSE =NOT(E2="Done")
XOR(c1, c2) An odd number of conditions are TRUE Rarely needed
XAND, OR and NOT on employee data
ABCDEFGH
1NameDepartmentCityRatingSalarySales & rating≥4Metro cityNot HR
2Kavya IyerHRMumbai497000FALSETRUEFALSE
3Isha MehtaSalesDelhi357000FALSETRUETRUE
4Aadhya NairSalesBengaluru545000TRUEFALSETRUE
5Krishna KhanSalesMumbai3156000FALSETRUETRUE
TipTRUE = 1, FALSE = 0

In arithmetic Excel treats TRUE as 1 and FALSE as 0. =SUM(--(D2:D21>=4)) counts ratings of 4 or more (the double minus converts TRUE/FALSE to 1/0). This trick powers SUMPRODUCT in Lesson 6.

IF: making decisions

=IF(logical_test, value_if_true, value_if_false)

IF checks a condition and returns one of two results.
IF checks a condition and returns one of two results.
XIF in practice
ABCDEF
1NameSalaryRatingBandBonus (10% if rating ≥ 4)Pass/Fail target 80k
2Kavya Iyer970004Standard9,700Pass
3Imran Reddy1570003High0Pass
4Isha Mehta570003Standard0Fail
5Vikram Joshi750005Standard7,500Fail

IF can return text, numbers, another formula's result, or "" (an empty-looking cell).

Combining IF with AND and OR

XIF + AND / OR
ABCDEF
1NameDeptRatingYearsPromotion: rating ≥4 AND years ≥3Review: rating ≤2 OR years <1
2Kavya IyerHR410EligibleOK
3Saanvi RaoHR41Not yetOK
4Aarav KulkarniHR21Not yetReview
5Neha MenonSales30Not yetReview

Nested IF vs IFS

For more than two outcomes, you can put an IF inside another IF (nested IF), or use IFS, which is easier to read.

XGrading marks: nested IF and IFS give the same result
ABC
1MarksNested IFIFS
292AA
378BB
460CC
541FailFail
Common mistakesOrder matters in nested IF and IFS

Excel stops at the first TRUE condition. If you test A2>=60 first, a mark of 92 returns "C". Always test from the highest threshold to the lowest (or lowest to highest with <). In IFS, end with TRUE, "default" so values that match nothing do not return #N/A.

SWITCH: matching exact values

When you compare one value against a list of exact matches, SWITCH is cleaner than nested IFs.

XSWITCH maps codes to names
AB
1Region codeRegion name
2NNorth
3WWest
4XUnknown

For long mappings, a lookup table with XLOOKUP (Lesson 7) is better than a long formula, because the mapping can be edited without touching formulas.

Handling errors: IFERROR and IFNA

XIFERROR replaces any error with a value you choose
ABCD
1SalesOrdersAverage orderWith IFERROR
2120000403,0003,000
300#DIV/0!0
495000253,8003,800
Watch outDo not hide problems

Wrapping every formula in IFERROR can hide genuine mistakes (a deleted column, a typo). Use it where an error is expected and meaningful (division by zero, value not found), and prefer IFNA for lookups.

Case: building columns for the Superstore data

Here is how analysts typically enrich the Superstore order table with text and logical formulas before analysis. The same logic in pandas gives us the real results on all 6,323 rows:

Python
import pandas as pd
s = pd.read_csv("superstore_sales.csv")
s["order_year"] = s["order_id"].str[3:7].astype(int)            # =VALUE(MID(A2,4,4))
s["product_brand"] = s["product_name"].str.split(" ").str[0]     # =TEXTBEFORE(N2," ")
s["profit_flag"] = s["profit"].apply(lambda p: "Loss" if p < 0 else "Profit")       # =IF(R2<0,"Loss","Profit")
s["discount_band"] = pd.cut(s["discount"], [-0.01, 0, 0.2, 1], labels=["No discount", "Up to 20%", "Above 20%"])  # =IFS(...)
s[["order_id", "order_year", "product_name", "product_brand", "discount", "discount_band", "profit", "profit_flag"]].head(6)
Output
order_id order_year product_name product_brand discount discount_band profit profit_flag
0 IN-2023-00001 2023 Nova Phone 223 Nova 0.0 No discount 3,472.55 Profit
1 IN-2023-00001 2023 Orbit Appliance 798 Orbit 0.0 No discount 1,169.54 Profit
2 IN-2023-00001 2023 Orbit Table 248 Orbit 0.0 No discount 6,044.47 Profit
3 IN-2023-00002 2023 Crest Storage 946 Crest 0.2 Up to 20% 450.16 Profit
4 IN-2023-00003 2023 Crest Bookcase 170 Crest 0.3 Above 20% -5,512.33 Loss
5 IN-2023-00003 2023 Zenith Table 111 Zenith 0.3 Above 20% -1,598.43 Loss
6 rows × 8 columns
Python
pd.crosstab(s["discount_band"], s["profit_flag"], margins=True)
Output
profit_flag Loss Profit All
discount_band
No discount 8 3472 3480
Up to 20% 776 1167 1943
Above 20% 898 2 900
All 1682 4641 6323
4 rows × 3 columns

The crosstab already tells a business story: almost every order-line with a discount above 20% makes a loss. You will build this same view as a pivot table in Lesson 8.

New column Excel formula (row 2)
Order year =VALUE(MID(B2,4,4))
Brand =TEXTBEFORE(N2," ") or =LEFT(N2,FIND(" ",N2)-1)
Profit flag =IF(S2<0,"Loss","Profit")
Discount band =IFS(Q2=0,"No discount",Q2<=0.2,"Up to 20%",TRUE,"Above 20%")
Do it with AIDo it with AI

Prompt: "Column B has order IDs like IN-2025-00042. Write an Excel formula to extract the year as a number, and another to flag orders where discount (Q) is above 20% and profit (S) is negative as 'Review'. Explain each part." Expected answer: =VALUE(MID(B2,4,4)) and =IF(AND(Q2>0.2,S2<0),"Review","").

Summary

Key takeaways
Quick quiz6 questions · tap an answer to check it

1. What does =MID("SKU-7781-BLU",5,4) return?

Answer: B. MID starts at position 5 ("7") and takes 4 characters: 7781.

2. A2 contains [email protected]. Which formula returns ravi.verma?

Answer: A. FIND returns 11 (the @ position); take 10 characters from the left.

3. Which function joins A2:D2 with ", " and skips empty cells?

Answer: B. TEXTJOIN's second argument TRUE ignores empty cells.

4. =IF(A2>=60,"C",IF(A2>=75,"B","A")) is used to grade a mark of 85. What is the result?

Answer: A. The first test (85 >= 60) is TRUE, so "C" is returned immediately. The conditions are in the wrong order.

5. Why end an IFS formula with TRUE,"Other"?

Answer: B. TRUE always matches, so it acts as the "else" branch.

6. What does ="Total: "&TEXT(1234.5,"#,##0") return?

Answer: B. TEXT formats the number with separators and no decimals, rounding 1234.5 to 1,235.
ExerciseProduct codes

Codes look like FUR-CH-10001234 (category-subcategory-number). Write formulas for: (a) the category code; (b) the sub-category code; (c) the number as a value; (d) a flag "Furniture" when the category is FUR, otherwise "Other".

Show solution

(a) =LEFT(A2,3) or =TEXTBEFORE(A2,"-") (b) =MID(A2,5,2) or =TEXTBEFORE(TEXTAFTER(A2,"-"),"-") (c) =VALUE(RIGHT(A2,8)) or =VALUE(TEXTAFTER(A2,"-",-1)) (d) =IF(LEFT(A2,3)="FUR","Furniture","Other")

ExerciseCommission rules

Sales reps get: 10% commission if sales ≥ 5,00,000; 7% if ≥ 3,00,000; 4% if ≥ 1,00,000; otherwise 0. Reps in the "Trainee" grade get half. Write one formula (sales in B2, grade in C2).

Show solution

=B2*IFS(B2>=500000,10%,B2>=300000,7%,B2>=100000,4%,TRUE,0)*IF(C2="Trainee",0.5,1)

Interview cornerQuestions you may be asked

Q: Difference between FIND and SEARCH? FIND is case-sensitive and does not allow wildcards; SEARCH is not case-sensitive and supports * and ?. Both return #VALUE! when the text is not found.

Q: How would you extract the domain from an email address? =MID(A2,FIND("@",A2)+1,LEN(A2)) or, in Microsoft 365, =TEXTAFTER(A2,"@").

Q: Nested IF or IFS? IFS for readability when testing several conditions in order; SWITCH for exact matches of one value; a lookup table when the mapping is long or changes often.

Practise more online

Free, trusted places to test yourself on this topic. Links open the original site in a new tab.