Power BI Advanced AnalyticsLesson 43 of 53
Lesson 43 of 53

Moving Averages & Rolling Windows: DATESINPERIOD & DATESBETWEEN

Overview & Learning Objectives

Daily sales charts are notoriously noisy: weekend spikes and weekday dips create jagged lines that obscure true business trends. To smooth out short-term fluctuations, analytics teams use **Rolling Moving Averages** (e.g. 7-day moving average, 30-day rolling revenue). In DAX, rolling windows are constructed using `DATESINPERIOD()` and `DATESBETWEEN()`.

  • Master the syntax of `DATESINPERIOD(DateColumn, StartDate, NumberOfIntervals, Interval)`.
  • Calculate a 7-day and 30-day rolling moving average in DAX.
  • Use `DATESBETWEEN()` for custom arbitrary date ranges.

Industry Standard

Standard: Analytics dashboards at Swiggy and Amazon use 7-day and 30-day moving averages as standard overlays to track core underlying order volume growth independent of weekend spikes.

Real-World Example: Burger Point Order Velocity

Burger Point sales spike drastically on Saturdays and drop on Tuesdays. A 7-day rolling average provides a smooth trend line.

DateDaily Sales (₹)7-Day Rolling Revenue Window7-Day Moving Avg (₹)
07-Oct-202614,00001-Oct to 07-Oct (₹ 70,000 total)10,000 (70k / 7 days)
08-Oct-202621,000 (Weekend spike!)02-Oct to 08-Oct (₹ 77,000 total)11,000 (77k / 7 days)
09-Oct-20268,000 (Tuesday dip)03-Oct to 09-Oct (₹ 77,000 total)11,000 (Smooth trend!)

Functions Covered in this Chapter

Here is the definition, exact syntax, parameter breakdown, and real-world business example for each function covered in this chapter:

Function 1

DATESINPERIOD Function

Definition: Returns a table of dates beginning with a specified start date and continuing for the specified number and type of date intervals. Perfect for 7-day, 30-day, or 90-day rolling averages.

Syntax:
Trailing 30 Days Sales = CALCULATE([Total Sales], DATESINPERIOD(Dim_Date[Date], MAX(Dim_Date[Date]), -30, DAY))
Parameters Breakdown:
  • Dates: Date column from a marked Date dimension.
  • StartDate: Anchor date to begin the range from (typically `MAX(Dim_Date[Date])`).
  • NumberOfIntervals: Number of intervals to look back or forward (-30 to look back 30 days).
  • Interval: Interval unit: `DAY`, `MONTH`, `QUARTER`, or `YEAR`.
Real-World Example (7-Day Rolling Moving Average):
Problem / Scenario: How do you smooth daily demand spikes by calculating a 7-day rolling average for dark-store orders?
Formula: 7D Moving Avg = DIVIDE(CALCULATE([Total Sales], DATESINPERIOD(Dim_Date[Date], MAX(Dim_Date[Date]), -7, DAY)), 7)
Result: ₹ 1,12,000 / day
Why it works: DATESINPERIOD dynamically captures the preceding 7 days relative to the active date row.
Function 2

DATESBETWEEN Function

Definition: Returns a table of dates between a specified fixed start date and end date.

Syntax:
Festival Period Sales = CALCULATE([Total Sales], DATESBETWEEN(Dim_Date[Date], DATE(2026, 10, 1), DATE(2026, 10, 31)))
Parameters Breakdown:
  • Dates: Date column from a marked Date dimension.
  • StartDate: Earliest date in the range (inclusive).
  • EndDate: Latest date in the range (inclusive).
Real-World Example (Fixed Promotion Window Analysis):
Problem / Scenario: How do you calculate total revenue earned specifically during the Great Indian Festival (Oct 10 - Oct 25)?
Formula: Sale Window Revenue = CALCULATE([Total Sales], DATESBETWEEN(Dim_Date[Date], DATE(2026, 10, 10), DATE(2026, 10, 25)))
Result: ₹ 75,00,000
Why it works: DATESBETWEEN isolates the exact two-week window for campaign ROI reporting.

Common Mistakes & Limitations

  • Using a positive interval (e.g. `30`) instead of `-30`, looking into the future instead of historical days.
  • Dividing by a hardcoded `7` instead of dividing by `COUNTROWS(DATESINPERIOD(...))` during the first 6 days of the dataset.

Keyboard Shortcuts

ActionWindowsMac
Quick Measure: Rolling AverageQuick Measure -> Time Intelligence -> Rolling averageQuick Measure
Format CurrencyMeasure Tools -> CurrencyCurrency

Pro Tips & Modern Alternatives

  • Use `AVERAGEX(DATESINPERIOD(Dim_Date[Date], MAX(Dim_Date[Date]), -7, DAY), [Total Sales])` for direct moving average computation.
  • Use `DATESBETWEEN` when filtering between two fixed explicit calendar dates.