Daily DAX : Day 433 DATESBETWEEN

DAX Function: DATESBETWEEN

Description

The DATESBETWEEN function in Power BI DAX is a time intelligence function that returns a one-column table containing all dates between a specified start date and end date (inclusive). It is commonly used inside CALCULATE to filter calculations over a custom date range.

Syntax

DATESBETWEEN(<Dates>, <StartDate>, <EndDate>)

Parameters

  • <Dates>: A reference to a date column (usually from a marked Date table, e.g., 'Date'[Date]).
  • <StartDate>: The starting date of the range. Can be a fixed date, BLANK() for the earliest date, or a dynamic expression.
  • <EndDate>: The ending date of the range. Can be a fixed date or dynamic expression.

Common Use Cases

  • Calculating metrics for a specific fixed period (e.g., a fiscal quarter or campaign).
  • Life-to-date (LTD) or cumulative calculations from the beginning of data to the current context.
  • Custom rolling periods where start/end dates are dynamically computed.
  • Comparing sales or other measures in arbitrary date ranges.

Examples

1. Fixed Date Range (e.g., Q1 Sales)

Sales_Q1_2025 = 
CALCULATE(
    SUM(Sales[SalesAmount]),
    DATESBETWEEN(
        'Date'[Date],
        DATE(2025, 1, 1),
        DATE(2025, 3, 31)
    )
)

2. Life-to-Date (Cumulative) Customers

Customers_LTD = 
CALCULATE(
    DISTINCTCOUNT(Sales[CustomerKey]),
    DATESBETWEEN(
        'Date'[Date],
        BLANK(),  // From the earliest date
        MAX('Date'[Date])  // Up to the latest date in context
    )
)

3. Dynamic Range (e.g., Last 30 Days)

Sales_Last_30_Days = 
CALCULATE(
    SUM(Sales[SalesAmount]),
    DATESBETWEEN(
        'Date'[Date],
        TODAY() - 30,
        TODAY()
    )
)

Note: Always use a proper Date table marked as such in Power BI for best results with time intelligence functions.

Comments

Popular posts from this blog

Daily DAX : Day 131 SELECTEDMEASURE

Daily DAX : Day 446 INFO.CSDLMETADATA

Daily DAX : Day 453 ENDOFWEEK