Skip to content

Lesson 6 ยท Formulas

Logic and Text Functions

Classify rows with Sigma's If and Switch functions, use conditional aggregates, and clean text with Trim, Lower, SplitPart and Contains, with examples.

Real data rarely arrives in the categories you want. You need to label orders by size, group messy spellings together, or pull a code out of a long string. Logic and text functions do this work.

If

Sigma's If takes pairs of condition and value, and an optional final value for everything else:

TEXT
If(condition 1, value 1, [condition 2, value 2], ..., [else])
TEXT
If([Amount] >= 1000, "Large", [Amount] >= 200, "Medium", "Small")

Conditions are meant to be read in order, so put the strictest condition first. If you wrote [Amount] >= 200 first, a 5,000 order would match it and be labeled "Medium". If you leave out the final value and nothing matches, the result is null.

Switch

When you compare one value against several fixed cases, Switch is shorter:

TEXT
Switch([Country Code], "PE", "Peru", "NO", "Norway", "IN", "India", "Other")

The first argument is the value to test, then pairs of case and result, then an optional default. Without a default, unmatched values give null. Use Switch for exact matches and If for ranges and combined conditions.

SQL Stand-in: condition order in a CASE expression

Press Run (or Ctrl + Enter) to see the result here.

SQL's CASE behaves like a multi-branch If here. Order 3 shows the bug.

Conditional aggregates

To total only some rows, use a conditional aggregate instead of building a separate filtered table:

TEXT
SumIf([Revenue], [Status] = "Paid")
CountIf([Status] = "Refunded")
CountDistinctIf([Customer ID], [Region] = "North")

For SumIf, the first argument is the column to add, followed by one or more conditions. For CountIf, every argument is a condition. When you give several conditions, a row must pass all of them. For "either", write one condition with OR:

TEXT
SumIf([Revenue], [State] = "TX" OR [State] = "CA")

Argument order can differ between functions, so check the function's help page when you try a new one.

Membership and ranges

In and Between can make some conditions easier to read. In(value, candidate 1, candidate 2, ...) is true when the value matches any candidate. Between(value, lower, upper) is true when the value lies in the range, including both ends:

TEXT
In([Region], "North", "South")
Between([Amount], 200, 999)

Be careful with ranges that include timestamps. A range that ends on a date may cut off times later that day.

Clean text

Messy text breaks grouping, because "Lima", "lima " and "LIMA" count as three values. Common functions:

Function Purpose
Trim, LTrim, RTrim Remove surrounding spaces
Lower, Upper, Proper Change letter case
Replace Swap one piece of text for another
Left, Right, Mid Take part of a string
Len Length of a string
Contains, StartsWith, EndsWith Test for text
SplitPart Take one piece of a delimited string
RegexpExtract, RegexpMatch, RegexpReplace Pattern matching

SplitPart(string, delimiter, position) counts positions from 1, and negative positions count from the end:

TEXT
SplitPart("EU-RETAIL-0042", "-", 2)    gives the second piece
SplitPart("EU-RETAIL-0042", "-", -1)   gives the last piece

Contains is case sensitive according to the documentation. To ignore case, lower both sides:

TEXT
Contains(Lower([Notes]), "urgent")

You can pass several substrings, and the result is true if at least one of them is found.

Python Stand-in: normalize before you group

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The Python function only imitates the idea of SplitPart, including counting from 1.

Mapping tables beat long formulas

If a classification has dozens of cases, such as mapping product codes to families, do not build a giant Switch. Keep a small mapping table in your data platform or in an input table, then look up the values with Lookup. Business people can maintain the mapping without editing formulas, and you have one place to audit.

A classification checklist

  1. List all categories and agree on their borders.
  2. Decide what happens to nulls and unexpected values.
  3. Order If conditions from strict to loose.
  4. Count rows per category and look for surprises.
  5. Keep an "Unmapped" label so new values are visible.

Common mistake: letting the default branch hide problems. If you label every unknown value "Other", a new region or a typo disappears silently. Use a label such as "Unmapped" and watch its count.

Practice

Take a list of ten email addresses with mixed case and extra spaces. Make a lowercase, trimmed version, split off the domain with SplitPart, then count addresses by domain. Notice how many groups merge after cleaning. Use invented addresses only.

A worked cleanup

Suppose a web form produced a column of company types with values such as "ltd", "Ltd.", "LIMITED", "gmbh " and many blanks. Work in small, visible steps.

First, make a helper column that trims spaces and converts the text to lowercase. Second, make another that removes periods with Replace. Third, group by the cleaned helper and count the rows. This list of distinct values is your real to-do list, and it is usually much shorter than the raw one.

Only then write the classification. Map each cleaned value to a standard label, either with Switch for a handful of cases or with a mapping table for many. Treat blank values as their own category, such as "Not given", because a blank is different from an unfamiliar value.

When you finish, group by the final label and compare the total row count with the original. The counts must match. Keep the raw column hidden rather than overwritten, so that anyone can check how a label was produced. Review the "Unmapped" count again after the next data load, since new spellings tend to appear over time.

Test the unusual rows

After you build a classification, filter to each label in turn and read a sample of rows. Reading real values finds problems that counts alone cannot, such as a phrase that matches your search text by accident.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In an If formula with overlapping conditions, how should you order them?

    A loose condition placed first would capture rows meant for a stricter one.
  2. 2. With two conditions in SumIf, which rows are added?

    Multiple conditions combine with AND. Write OR inside one condition for either.
  3. 3. How do you search text without caring about case?

    Contains is case sensitive, so normalize case first.
  4. 4. What is better than a very long Switch for hundreds of product codes?

    A mapping table is easier to maintain, audit and share.

Summary

  • If handles ranges and combined conditions; Switch matches one value against cases.
  • Conditional aggregates total only matching rows.
  • Normalize text before grouping, and remember that Contains is case sensitive.
  • SplitPart counts from 1 and accepts negative positions.
  • Use mapping tables and an "Unmapped" label for large classifications.

Next: Date Functions

Your progress is saved in this browser. No account needed.