Ir para o conteúdo

Lição 13 · Parsing and Transforming

Parsing and Calculating Dates

Convert text into real Alteryx dates, use format codes with DateTimeParse and DateTimeFormat, calculate differences and avoid ambiguous date mistakes.

Dates cause more bugs than almost any other data. A source may send 03/04/2026, and nobody knows whether it means March 4 or April 3. Alteryx stores dates in ISO form (2026-04-03), so your job is to convert other formats correctly.

This lesson covers conversion, calculation and common traps.

Date types

Designer has Date, Time and DateTime types. A real date looks like 2026-03-31, and a date-time like 2026-03-31 14:05:00. A text field that merely looks like a date is not a date: sorting and arithmetic may give wrong answers.

Converting text to dates

There are two main ways:

  1. The DateTime tool in the Parse category. You pick the field, the direction (text to date or date to text) and the format, either from a list or typed by you.
  2. The DateTimeParse function in a Formula tool:
TEXT
DateTimeParse([order_date_text], "%d/%m/%Y")

The format string uses codes similar to those in many programming languages:

Code Meaning
%Y Four-digit year
%m Month number
%d Day of month
%H, %M, %S Hour, minute, second
%B, %b Month name, full and short

Recent versions accept extra optional arguments, such as a language or a time zone. Check the function reference for the version you use.

To go the other way, use DateTimeFormat([order_date], "%d.%m.%Y"). The result is text, so do this only at the end, when you prepare output for people.

Python Stand-in: parsing and formatting dates

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.

In Designer, a value that cannot be parsed typically becomes null and shows a message. Filter for nulls after converting.

Ambiguous dates

03/04/2026 is valid in both day-first and month-first formats, so no error appears. To protect yourself:

  • Ask the source which format it uses.
  • Look for a day above 12, which proves the order for that file.
  • Never mix formats in one column. If several sources differ, convert each one before the union.

Date arithmetic

Function Use
DateTimeToday() Today's date
DateTimeNow() Current date and time
DateTimeAdd(date, n, "days") Add or subtract (use a negative n)
DateTimeDiff(date1, date2, "days") date1 minus date2 in the given unit
DateTimeFormat(date, format) Date to text

DateTimeDiff returns a whole number. The help explains that month and year differences count only complete units, and that smaller units drop fractions rather than round. So the difference between January 31 and February 28 may not count as one month. Test the edge cases your business cares about.

TEXT
DateTimeDiff([ship_date], [order_date], "days")

The first argument comes first in the subtraction. If you swap them, you get negative numbers.

Python Stand-in: complete months between dates

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.

End-of-month cases like these are exactly where a business rule must be written down. "One month after January 31" has no single obvious answer.

Periods and fiscal years

Reports often group by month or fiscal year. You can create a month key with DateTimeFormat([order_date], "%Y-%m"). For a fiscal year that starts in July:

TEXT
IF ToNumber(DateTimeFormat([order_date], "%m")) >= 7
THEN ToNumber(DateTimeFormat([order_date], "%Y")) + 1
ELSE ToNumber(DateTimeFormat([order_date], "%Y"))
ENDIF

This labels the fiscal year by the year in which it ends. Confirm your company's rule.

Time zones matter for timestamps from different systems. Record which zone each source uses, and convert to one zone before you compare or group by day. Workflows that run on a server may also use a different clock from your laptop.

Missing periods

If no sales happened in a month, that month does not appear in your data. Charts then skip it. Build a calendar table with a Generate Rows tool and join your data to it, so empty months show as zero or blank.

Converting several formats

Sometimes one column really does contain several formats, perhaps because people typed dates by hand. Do not guess. First find out which formats exist by looking at the shapes of the values. Then handle each recognizable and unambiguous shape with its own branch: filter with a pattern, convert with the matching format and union the branches. Send everything else to a review output.

For example, 2026-03-04 is clear, and so is 4 March 2026. A value such as 03/04/2026 stays ambiguous unless you know the source's rule. A process that tries one format after another and accepts the first success silently chooses a meaning for ambiguous values, which is worse than rejecting them.

After conversion, add range checks. A year of 1900 or 2999 often points to a default value or a typing mistake, even though it parses without any error.

Make runs reproducible

A rule such as "last 30 days" gives a different answer every day. That is fine for a live report but awkward when someone asks you to reproduce last week's file. Make the run date an input value and use it in all date rules. Write the run date into the output as well.

Practice task

Make a Text Input column with these values: 31/01/2026, 28/02/2026, 29/02/2026, 2026-03-05, an empty value and 05/03/2026. Convert the column with a day-first format. Before you run, mark which values you expect to fail. Remember that 2026 is not a leap year.

Next, calculate the days from each valid date to a fixed run date that you type as a second column. Add a month key and check that sorting by it gives calendar order. Finally, send all unconverted rows to a separate output with the original text, so that someone can correct them at the source.

Common mistakes

Never format a date as text and then use it for sorting or arithmetic. Keep a real date column for logic, and add a formatted copy only for display.

  • Assuming the date order from one example.
  • Ignoring null results after parsing.
  • Using today's date in a way that makes yesterday's run impossible to reproduce. Consider a run-date input.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. In which form does Alteryx store a date?

    Dates use ISO ordering of year, month and day.
  2. 2. What does DateTimeDiff(a, b, units) calculate?

    The second date is subtracted from the first.
  3. 3. Why is 03/04/2026 risky?

    Both readings are valid, so wrong parsing is silent.

Summary

  • Convert text with the DateTime tool or DateTimeParse and the right format codes.
  • Format dates as text only for output.
  • DateTimeDiff and DateTimeAdd work with units; test end-of-month cases.
  • Confirm date order, fiscal rules and time zones, and fill missing periods.

Next: Transpose and Cross Tab

Seu progresso fica salvo neste navegador. Não precisa de conta.