Aller au contenu

Leçon 16 · Formulas and Summaries

Multi-Row and Multi-Field Formulas

Compare rows with the Alteryx Multi-Row Formula tool, group calculations correctly, apply one expression to many columns and generate new rows.

A normal Formula looks at one row at a time. Some questions need other rows: "How much did sales change since last month?" Other tasks need the same expression on many columns. Designer has a tool for each: Multi-Row Formula and Multi-Field Formula.

This lesson also introduces Generate Rows, which creates rows from a rule.

Multi-Row Formula

The tool updates one column, or creates one new column, with an expression that can refer to neighboring rows. References look like this:

TEXT
[Row-1:sales]     // the previous row's sales
[Row+1:sales]     // the next row's sales
[sales]           // the current row's sales

For month-over-month change:

TEXT
[sales] - [Row-1:sales]

In the Configuration window you also decide:

  • Group By fields: the calculation restarts for each group, such as each store.
  • How many rows the expression looks back or forward.
  • What to use for rows that do not exist, for example the first row, which has no previous row. Choices include zero or empty values (the default in the help), null, or the values of the closest valid row.

The tool uses the current row order. Sort by the group field and then by date immediately before it. Without a Sort and a Group By, the first month of store B would be compared with the last month of store A.

Seeing the effect

SQL Stand-in: previous-row change per store

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

PARTITION BY plays the role of Group By and ORDER BY plays the role of your Sort. The first month of each store has no previous row, so the result is null. SQL users can read more about window functions.

If instead your choice for missing rows is 0, the first change equals the whole sales value, which looks like a big increase. Choose with care.

Common patterns

Pattern Expression idea
Fill down blank categories IF IsEmpty([region]) THEN [Row-1:region] ELSE [region] ENDIF
Flag a new group Compare [customer] with [Row-1:customer]
Number rows within a group [Row-1:seq] + 1
Gap in days DateTimeDiff([date], [Row-1:date], "days")

Because the tool processes rows from top to bottom, a filled-down value can carry through several blank rows. That is useful for spreadsheet exports where a category appears only once.

Python Stand-in: fill down and number rows

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.

Multi-Field Formula

Multi-Field Formula applies one expression to many selected columns. Inside it, [_CurrentField_] means the column currently being processed:

TEXT
Trim([_CurrentField_])

You can overwrite the original columns or create new ones with a prefix or suffix, and often change their output type. Choose the field type filter first (text, numeric or all) so you only transform suitable columns.

Some uses:

  • Trim or uppercase every text column.
  • Divide all amount columns by 1,000.
  • Replace a placeholder such as "N/A" with null in many columns:
TEXT
IF [_CurrentField_] = "N/A" THEN Null() ELSE [_CurrentField_] ENDIF

If new columns arrive later, decide whether they should be selected automatically. Check whether your version has a dynamic or unknown field option.

Python Stand-in: one rule for many columns

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.

Generate Rows

Generate Rows creates rows in a loop. You set a starting value, a condition to continue and an expression that changes the value each step:

TEXT
Initialize: RowCount = 1
Condition:  RowCount <= 12
Loop:       RowCount + 1

It is handy for calendar tables, expanding a start and end date into one row per day and creating test data.

A condition that never becomes false can run for a very long time or fill your disk. Test with small limits first.

Performance note

These tools pass through every row, and the sort before Multi-Row Formula can be expensive on big data. Reduce rows and columns first, and consider doing window calculations in the database when the data is already there.

Checking row-based results

Row-based calculations deserve a manual check, because a wrong sort or group still produces numbers that look reasonable. Pick one group with only a few rows and calculate the result by hand. Then check these special places:

  • The first row of every group, where no previous row exists.
  • The last row of a group, if you look forward with Row+1.
  • A group with only one row.
  • Two rows with the same sort value, such as two orders on one day. Add a second sort field, for example an order number, so their order is fixed.
  • A gap in time. The previous row may be from three months ago, so "previous row" is not always "previous month". If you need calendar months, create the missing months first and then compare.

The last point causes wrong growth reports. The expression is correct, but the data does not have the shape that the expression assumes.

Summary or Multi-Row?

If you need only one result per group, use Summarize. Choose Multi-Row Formula when every detail row must stay and each one needs information from a neighbor.

Practice task

Create monthly sales for two stores and shuffle the rows. Leave out one month for the second store. Calculate the change from the previous row twice: first without a Group By, then with it. Always sort first.

Compare the two results and find the rows that differ. Then look at the month after the gap. Is its change a month-over-month change? Add the missing month with a sales value chosen according to a rule that you write down, and calculate again. At the end, use Multi-Field Formula or the stand-in to apply the same rounding rule to every numeric column.

Common mistakes

  • No Sort before Multi-Row Formula.
  • Missing Group By, so values leak between groups.
  • Zero used for non-existing rows, which creates fake changes.
  • Applying Multi-Field Formula to identifier columns by accident.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does [Row-1:sales] refer to?

    It reads the previous row's value for that field.
  2. 2. Why add a Sort before Multi-Row Formula?

    Neighbor references are meaningful only in a defined order.
  3. 3. What is [CurrentField] used for?

    The same expression is evaluated for each selected field.
  4. 4. What is a risk with Generate Rows?

    Set a safe condition and test with small limits.

Summary

  • Multi-Row Formula reads neighbors with Row-1 and Row+1 syntax.
  • Sort first, use Group By and choose how missing rows behave.
  • Multi-Field Formula applies one expression with CurrentField.
  • Generate Rows creates sequences; keep the loop bounded.

Next: Summarize and Running Totals

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