Aller au contenu

Leçon 7 · Formulas

Date Functions

Group and compare dates in Sigma with DateTrunc, DatePart, DateAdd and DateDiff, and avoid time zone, week start and partial period mistakes in reports.

Almost every business question has a time in it: sales this month, tickets older than a week, growth compared with last year. Date functions answer these questions, and they also hide some of the most common reporting errors.

Date values first

Date functions need real date values. If a column holds text such as "2026-11-03", convert it with Date(). Sigma also offers DateParse for text in other layouts, and MakeDate to build a date from separate year, month and day numbers.

DateTrunc: snap to a period

DateTrunc(precision, date) keeps the date down to a chosen level and drops the rest. Documented precisions include year, quarter, month, week, day, hour, minute and second.

TEXT
DateTrunc("month", [Order Date])
DateTrunc("week_starting_monday", [Order Date])

A November 17 order becomes November 1 when truncated to the month. Group by this column to get monthly totals. Note the week options: according to the documentation, plain "week" starts on Sunday, and there are separate values for a Sunday or Monday start. Agree on your week definition before you publish.

DatePart: pull out one piece

DatePart(precision, date) returns a number, such as the month, day or weekday. The documentation says weekdays run from 1 to 7 starting on Sunday.

TEXT
DatePart("month", [Order Date])
DatePart("weekday", [Order Date])

Do not confuse the two functions:

Question Use Result for 2026-11-17
Which month period? DateTrunc("month", ...) A date: 2026-11-01
Which month number? DatePart("month", ...) A number: 11

Grouping by the month number alone mixes November 2025 with November 2026. For a time series, truncate.

SQL Stand-in: month period versus month number

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

DateAdd and DateDiff

DateAdd(unit, amount, date) moves a date. A negative amount moves backward:

TEXT
DateAdd("day", 30, [Invoice Date])
DateAdd("month", -1, [Report Date])

DateDiff(unit, start, end) measures the gap as end minus start, so it is negative when the end comes first:

TEXT
DateDiff("day", [Opened At], [Closed At])

The unit must be a fixed value such as "day", not a column. If either date is null, the result is null. How partial units are counted is a classic source of confusion in analytics tools, so test dates near the edges, for example the last day of a month and the first day of the next, before using results for money or service levels.

For open items, there is no closing date yet. Use the current time as a fallback only when you really mean "age so far":

TEXT
DateDiff("day", [Opened At], Coalesce([Closed At], Now()))

This number changes over time, so label it "Age so far (days)".

Current date helpers

Today() and Now() give the current date or time. InDateRange and InPriorDateRange help with relative periods, and a date range filter or control can choose a relative window without any formula.

Time zones

A moment in time is one thing, and the calendar day it belongs to depends on a time zone. A sale at 01:30 UTC on December 1 is still November 30 in New York. According to its documentation, Sigma shows truncated timestamps in your organization's time zone, The DatePart help page is less clear: it lists UTC as the default for the optional time zone argument but also says dates are read in the organization's time zone. When the answer depends on the zone, pass it explicitly, test a time near midnight, and use ConvertTimezone when you need a specific zone.

JS Stand-in: one instant, two calendar days

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

No output.

If a daily total differs between two reports, time zones are a prime suspect.

Compare periods fairly

On November 10, comparing "this month so far" with the whole of last November makes this year look terrible. Compare the same number of days:

  1. Find how many days of the current month have passed.
  2. Count last year's rows only up to the same day.
  3. Label the result "month to date".

Also remember that months have different lengths, and that holidays move between weekdays. A short note on the chart can prevent a wrong decision.

Fiscal calendars

Many companies use fiscal years that do not start in January, or 4-4-5 week calendars. Do not encode these rules in many separate formulas. Keep a calendar table with one row per date and columns for fiscal year, period and holiday flag, then join to it. Everyone then uses the same rules.

Common mistake: grouping by month name only, for example "November". It merges different years and sorts alphabetically. Group by a truncated date and format it as a month.

A worked example: ticket age bands

Suppose you need to report open support tickets in age bands. First, calculate the age in days from the opened date to the current time, only for tickets without a closed date. Second, classify the age with If, putting the strictest band first: over 30 days, then over 7 days, then everything else. Third, group by band and count the tickets.

Now test the borders. What happens to a ticket that is exactly 7 days old? Your comparison, either "greater than" or "greater than or equal to", decides which band it joins. Write the band names so that the border is obvious, such as "8 to 30 days". Also check a ticket opened today and a ticket with a missing opened date. The second one deserves its own "Unknown" band rather than a silent place in the youngest group.

Remember that this report changes as time passes, even if no data changes. If someone needs to know how the backlog looked at the end of last month, you need stored snapshots or a calculation based on a fixed reporting date instead of the current time.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which function gives a monthly time series key that keeps the year?

    Truncating returns the first day of that month, so years stay separate.
  2. 2. What does DateDiff measure?

    The documented result is end minus start.
  3. 3. Why can two reports show different daily totals?

    The same instant can fall on different calendar days in different zones.
  4. 4. On November 10, what is a fair year-over-year comparison?

    Compare equal parts of each period.

Summary

  • Use real date types; convert text first.
  • DateTrunc returns periods; DatePart returns numbers.
  • DateDiff is end minus start; handle open items with care.
  • Check week start days and time zones.
  • Compare partial periods fairly and use a calendar table for fiscal rules.

Next: Window Functions

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