Lompat ke konten

Pelajaran 15 ยท Formulas and Summaries

The Formula Tool and Expressions

Write Alteryx Formula expressions: field references, IF and IIF logic, Switch, string, number and null functions, plus safe handling of division and types.

The Formula tool creates new columns or changes existing ones using an expression. It is the place for calculations, business rules and text cleanup. The same expression language appears in other tools, such as Filter.

Learn a small set of functions well, and you can solve most everyday problems.

The basics

In the Configuration window, choose an existing field to update, or type the name of a new field and set its type and size. Then write the expression. One Formula tool can hold several expressions, which run from top to bottom, so a later one can use a column created by an earlier one.

References and literals:

TEXT
[quantity] * [unit_price]
"North" + " region"
1000

Fields go in square brackets. Text goes in quotes. You can add comments with //.

Set the output type and size on purpose. A new text field with too small a size truncates values, and a numeric result stored as an integer loses decimals.

Conditional logic

The full form:

TEXT
IF [amount] >= 1000 THEN "large"
ELSEIF [amount] >= 100 THEN "medium"
ELSE "small"
ENDIF

IIF(condition, value_if_true, value_if_false) is a short version for two choices. Switch matches one value against a list of cases and returns the matching result, with a default for no match:

TEXT
Switch([code], "Unknown", "N", "North", "S", "South")

Conditions are checked in order. Put the most specific condition first, or a broad one will capture the rows you meant for a later branch.

Python Stand-in: order of conditions matters

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.

Useful functions

Task Examples
Clean text Trim([x]), Uppercase([x]), TitleCase([x])
Find text Contains([x], "abc"), StartsWith, EndsWith
Cut text Left([x], 3), Right([x], 4), Substring([x], 0, 3)
Change text Replace([x], "-", ""), PadLeft([x], 6, "0")
Numbers Round([x], 0.01), Abs([x]), ToNumber([x])
Convert ToString([x])
Missing values IsNull([x]), IsEmpty([x]), Null()

Substring counts positions from zero, so Substring("Alteryx", 0, 3) gives Alt. Some text functions have case settings; for example, Contains ignores case by default. Check the function reference when case matters, and test comparisons with = on your own data.

Round takes a multiple as its second argument: Round([x], 0.01) rounds to two decimals, and Round([x], 5) to the nearest 5.

Python Stand-in: text and number functions in Python

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.

Nulls and division by zero

Calculations with null often yield null or unexpected results, depending on the operation. Decide a rule first. For ratios, protect the denominator:

TEXT
IF IsNull([visits]) OR [visits] = 0 THEN Null()
ELSE [sales] / [visits]
ENDIF

Returning null shows "not defined", while returning 0 would claim a real ratio of zero.

Python Stand-in: a safe ratio

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.

Type conversion in expressions

You cannot reliably add text to a number. Convert explicitly:

TEXT
"Order " + ToString([order_number])
ToNumber([amount_text]) * 1.2

If ToNumber meets text with currency symbols or the wrong decimal separator, the result will not be what you expect. Clean first, convert second and check the Results messages.

Readable formulas

  • Break long logic into several steps with helpful column names.
  • Use a lookup table with Join or Find Replace instead of a very long chain of IF statements. A table is easier to change and review.
  • Add a comment for any rule that is not obvious, and name its owner.
  • Test with rows at the exact limits, such as 99, 100 and 101.

Update or create?

When you configure an expression, you choose between updating a field and creating a new one. Updating keeps the table narrow, which is fine for harmless cleanup such as trimming spaces. Creating a new field is safer when you apply a business rule, because you can compare the result with the source value.

An updated field also keeps its existing type. If you write a number with decimals into an integer column, you lose the decimals. When the result needs a different type, create a new field with the right type and remove the old one later with a Select tool.

Testing an expression

Before you apply a formula to a large table, test it on a handful of typed rows. Include a row for every branch, a null in each referenced column, a zero and a value at each limit. Write the expected result in an extra column of the Text Input, then add a second Formula that compares the two:

TEXT
IF [result] = [expected] THEN "pass" ELSE "FAIL" ENDIF

Filter for failures. When a rule changes later, you can run the same test again and see whether older cases still behave.

Rules in data, not in formulas

Imagine a discount rule for twenty product groups. As an IF chain it is long, hard to review and risky to edit. As a two-column table with group and discount, a business user can read it, and you can join it to the data. Keep expressions for logic, such as how to apply a discount, and keep changing values in tables. Add a check that every group in the data has a matching row in the rule table.

Practice task

Create a table with quantity, unit_price and customer_type. Include a zero quantity, a missing price and a customer type that no rule mentions. Write expressions for a line total, a size label with three bands and a price per unit that is safe when the quantity is zero.

For every expression, write the expected result next to each row before you run it. Pay special attention to values exactly on a band limit. Then change the order of your conditions on purpose and see which rows move to a different band. Restore the correct order and add a comment that explains why it matters.

Common mistakes

  • Overwriting an original column and losing the source value.
  • Using string + on a number, or forgetting ToString.
  • Choosing FixedDecimal precision too small for large values.
  • Forgetting the ENDIF.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Substring([x], 0, 3) return for the value Alteryx?

    Positions start at zero, so it takes the first three characters.
  2. 2. In an IF / ELSEIF chain, why does order matter?

    Once a condition is true, later ones are skipped.
  3. 3. How should a ratio with a zero denominator usually be handled?

    Guard the division and make the result honest.
  4. 4. What does Round([x], 0.01) do?

    The second argument is the multiple to round to.

Summary

  • Formula creates or updates fields with typed expressions.
  • Use IF, IIF and Switch, and order conditions from specific to general.
  • Learn core string, number and null functions; positions start at zero.
  • Protect divisions, convert types explicitly and keep rules readable.

Next: Multi-Row and Multi-Field Formulas

Progres Anda disimpan di browser ini. Tidak perlu akun.