Code Explainer

Understanding the DATESINPERIOD Function in DAX

Learn about the DATESINPERIOD function in DAX, which returns a table of dates within a specified period for time-based calculations in Power BI, Excel, and SQL Server Analysis Services. Explore syntax, parameters, and examples for


Empty image or helper icon

Prompt

DATESINPERIOD(, , , )

Answer

Explanation of DATESINPERIOD Function

Overview

The DATESINPERIOD function in DAX (Data Analysis Expressions) returns a table that contains a column of dates within a specified period. This function is often used in Power BI, Excel, and SQL Server Analysis Services for time-based calculations.

Syntax

DATESINPERIOD(, , , )

Parameters

  1. <dates>: A column containing date values. This is typically a reference to a date column in your data model.
  2. <start_date>: The start date for the period. This can be a date literal or a reference to a date field.
  3. <number_of_intervals>: The number of intervals you want to retrieve. Positive numbers look forward from the start_date, and negative numbers look backward.
  4. <interval>: The type of interval. It specifies the granularity of the period and can be one of the following values: DAY, MONTH, QUARTER, or YEAR.

Functionality

The DATESINPERIOD function generates a table of dates starting from the start_date and covering the specified number of intervals based on the chosen interval type.

Breakdown of Parameters

dates

  • This parameter is a column containing the date values from which the function will generate the period.
  • Example: Sales[OrderDate]

start_date

  • Represents the point in time from which the period calculation begins.
  • Example: DATE(2023,1,1) for January 1, 2023.
  • Can also be dynamic, e.g., MAX(Sales[OrderDate]) to get the maximum date from the OrderDate column.

number_of_intervals

  • Defines how many periods to include in the output.
  • Positive values indicate future intervals from the start_date.
  • Negative values indicate past intervals from the start_date.
  • Example: -1 (which could mean one month back if used with the MONTH interval).

interval

  • Specifies the type of period to use (DAY, MONTH, QUARTER, YEAR).
  • Example: MONTH

Example Usage

Example 1: Previous Month

This example returns dates for the previous month relative to January 1, 2023.

DATESINPERIOD(
    Sales[OrderDate],
    DATE(2023,1,1),
    -1,
    MONTH
)
  • Sales[OrderDate] refers to the date column.
  • DATE(2023,1,1) is the start date (January 1, 2023).
  • -1 to look one month back.
  • MONTH to define the interval type.

Example 2: Next Quarter

This example returns dates for the next quarter from April 1, 2023.

DATESINPERIOD(
    Sales[OrderDate],
    DATE(2023,4,1),
    1,
    QUARTER
)
  • Sales[OrderDate] refers to the date column.
  • DATE(2023,4,1) is the start date (April 1, 2023).
  • 1 to look one quarter ahead.
  • QUARTER to define the interval type.

Key Concepts

  1. Time Intelligence: The DATESINPERIOD function is used extensively in time intelligence scenarios for building dynamic time-based calculations such as period-over-period growth, moving averages, or cumulative totals.
  2. Dynamic Date Ranges: By adjusting the start_date and number_of_intervals, you can dynamically calculate date ranges for custom reporting needs.
  3. Integration with Other DAX Functions: DATESINPERIOD can be effectively combined with other DAX functions such as SUMX, CALCULATE, and FILTER to perform more complex calculations.

Conclusion

The DATESINPERIOD function is a versatile tool in DAX for managing date ranges within your data. Its ability to dynamically calculate periods based on various intervals makes it essential for time-based analysis. For further learning and advanced use cases, consider exploring courses available on the Enterprise DNA Platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

Learn about the DATESINPERIOD function in DAX, which returns a table of dates within a specified period for time-based calculations in Power BI, Excel, and SQL Server Analysis Services. Explore syntax, parameters, and examples for effective use.