Aller au contenu

Leçon 5 · Formulas

Formula Basics

Write your first Sigma formulas: column references in brackets, operators, row versus aggregate calculations, type conversion, and safe handling of nulls.

Formulas are how you create new information from existing columns. If you know spreadsheet formulas, you will feel at home, with one change: you refer to columns by name instead of cells by address, and the formula applies to every row (or every group).

Reference a column

Put column names in square brackets:

TEXT
[Quantity] * [Unit Price]

When you refer to a column in another element or a source table, Sigma uses a prefix with the element name:

TEXT
[Customers/Customer Name]

You can also refer to a control by its ID in square brackets, which you will use with controls. Because names are part of formulas, choose clear and stable names.

Add a calculated column

  1. Use the add column action on the table.
  2. Type a formula in the formula bar.
  3. Rename the new column in plain words.
  4. Check a few rows by hand.

Operators

Sigma's documentation lists these operator groups:

Group Operators
Arithmetic + - * / ^ %
Join text &
Compare = != < <= > >=
Logic AND OR NOT
Grouping ( )
TEXT
([Revenue] - [Cost]) / [Revenue]
[First Name] & " " & [Last Name]
[Status] = "Open" AND [Amount] > 1000

Text values go in double quotes. Column names do not.

Row calculations and aggregates

This is the most important distinction in Sigma.

  • A row calculation uses values from the same row: [Quantity] * [Unit Price].
  • An aggregate combines many rows: Sum([Revenue]), Avg([Price]), CountDistinct([Customer ID]).

Where you put an aggregate decides its meaning. In a grouping it works once per group. In a summary it works once for the whole table.

The classic trap is a ratio. The margin of a group is total profit divided by total revenue, not the average of each row's margin.

TEXT
Right:  Sum([Profit]) / Sum([Revenue])
Wrong:  Avg([Profit] / [Revenue])
SQL Stand-in: average of ratios versus ratio of totals

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

The two answers are far apart because the small deal counts as much as the large one in the first method.

Nulls

A null means the value is missing. Sigma's documentation says that arithmetic with a null returns null. So [Price] - [Discount] is null when the discount is missing, even though you may have wanted to treat it as zero.

Function What it does
Zn([x]) Returns 0 when x is null
Coalesce([a], [b], "n/a") Returns the first value that is not null
IsNull([x]) True when x is null
IsNotNull([x]) True when x has a value
TEXT
[Price] - Zn([Discount])
Coalesce([Preferred Name], [Legal Name], "Unknown")

Only turn nulls into zero when zero is the true meaning. A missing temperature is not zero degrees. A missing discount may well be no discount, but ask the data owner.

Python Stand-in: null arithmetic and replacing nulls

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.

Division

Check the denominator before you divide. A guard makes your intent clear:

TEXT
If([Revenue] = 0, Null, [Profit] / [Revenue])

Here Null means "this ratio has no meaning", which is more honest than showing zero.

Conversions

Types matter. Use Text() to turn a value into text, Number() for numbers and Date() for dates. If a conversion fails on some rows, find out why. A stray letter or symbol in a numeric column often points to a data quality problem.

Make formulas readable

  • Break a long formula into helper columns with clear names.
  • Hide the helpers once they work.
  • Use consistent units, and put them in the name: "Weight (kg)".
  • Write down business rules, such as "Net revenue excludes tax and refunds".

Common mistake: assuming that a formula is right because it shows no error. A formula can be valid and still answer the wrong question. Test it on a handful of rows whose answer you can calculate yourself.

Test with edge cases

Pick rows that include a normal case, a zero, a negative value, a null, and an unusually large value. If the formula behaves well on all five, you have much better evidence than one happy example. Sigma also offers AI help for writing and explaining formulas (see the extensions lesson); treat its suggestions as drafts and test them the same way.

Reading a formula aloud

Before you save a formula, say it as a sentence. Sum([Profit]) / Sum([Revenue]) becomes "all the profit in this group divided by all the revenue in this group". If the sentence sounds wrong, or you cannot say which rows are included, the formula needs more work.

The same habit helps when you read other people's work. Start from the innermost parentheses and move outward. Note each column, check its type and its unit, and ask what happens when it is empty. Mixing units is an easy mistake that no error message will catch: adding a price in dollars to a price in euros produces a number that means nothing. Convert to one unit first, in a clearly named helper column.

When a result surprises you, do not rewrite the whole formula at once. Add each part as a temporary column, look at the values for a few rows, and find the first step that differs from what you expected. Remove or hide the temporary columns afterward.

Name columns for the next reader

A good column name says what the value is, its unit and, when needed, its rule. "Net revenue (USD)" and "Days open (so far)" are clear. "Calc 2" and "New column" are not. Rename calculated columns as soon as you create them, while you still remember why. Clear names also make later formulas easier to read, since the names appear inside them.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. How do you reference a column called Unit Price?

    Column references go in square brackets; double quotes mark literal text.
  2. 2. How should you calculate the margin of a group?

    Ratios of totals weight large and small rows correctly.
  3. 3. What does arithmetic with a null return?

    A missing input makes the result missing unless you replace it, for example with Zn.
  4. 4. When is it right to replace a null with zero?

    Replacing an unknown value with zero can create false data.

Summary

  • Reference columns as [Name] and other elements as [Element/Column].
  • Know whether you need a row calculation or an aggregate.
  • Compute group ratios from totals.
  • Handle nulls on purpose with Zn, Coalesce and null checks.
  • Test formulas with edge cases.

Next: Logic and Text Functions

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.