Arte SheetsLearn. Try. Understand.

Text

Find the longest or shortest matching title with FILTER and character lengths

Return the longest or shortest text value within a specific category or department by pairing a LEN helper column with MAXIFS, FILTER, and XMATCH.

Audit longest titles, descriptions, or SKUs inside a specific category!

Calculate a character-length helper for each title, then filter both titles and lengths with the same category condition. MAX or MIN of those filtered lengths finds the desired size; XMATCH returns the first tie in source-row order.

Using MAX(FILTER(...)) rather than combining a literal equality filter with wildcard-aware MAXIFS keeps category matching consistent even if the category contains * or ?. This exercise treats categories as literal labels and excludes empty titles. It does not trim whitespace or measure visual rendered width; LEN counts characters according to the spreadsheet engine.

Dept (A)Document Title (B)Char Length LEN (C)Role for E2 = "Legal"
LegalNDA Template12Shortest Legal title
SalesGlobal Enterprise Master Pricing Schedule41Ignored (Sales != Legal)
LegalMaster Services Agreement v428Longest Legal title!
LegalDPA Addendum12Tied shortest
SalesRenewal Order Form18Ignored (Sales != Legal)

Return longest text string matching category E2

Wrap in IFERROR(..., "No Match") so non-existent categories fail gracefully.

=IF(COUNTBLANK(E2)>0,"No Match",IFERROR(LET(titles,FILTER(B2:B6,(A2:A6=E2)*(C2:C6>0)),lengths,FILTER(C2:C6,(A2:A6=E2)*(C2:C6>0)),INDEX(titles,XMATCH(MAX(lengths),lengths,0))),"No Match"))

Return the first shortest nonempty title in the category

MIN finds the smallest filtered length. Ties return the first source row, so both Legal titles of length 12 yield NDA Template. Empty-string titles are excluded; whitespace-only titles are retained. IFERROR also hides other formula errors, so inspect source data when troubleshooting.

=IF(COUNTBLANK(E2)>0,"No Match",IFERROR(LET(titles,FILTER(B2:B6,(A2:A6=E2)*(C2:C6>0)),lengths,FILTER(C2:C6,(A2:A6=E2)*(C2:C6>0)),INDEX(titles,XMATCH(MIN(lengths),lengths,0))),"No Match"))

Try the example

Use the original values or change them to test your understanding.

Further reading