Code Generator | DAX

DAX Function for Dynamic Date Column Creation

This document describes a DAX function that generates a new date column in a table by adding a specified number of days to the current date, allowing for flexible future date calculations.


Empty image or helper icon

Prompt

that generates another date column by adding current to the specified number per row

Answer

DAX Function to Generate a New Date Column

This document outlines a DAX function that creates a new date column by adding a specified number of days to the current date for each row in a table. This operation is useful for creating future dates based on current data.

Function Explanation

Purpose

The function will create a new calculated column in a table that adds a specified number of days to the current (or a specified) date.

Parameters

  • Input Table: The table from which the current date will be taken.
  • Days to Add: An integer value representing the number of days to add to the current date.

Return Type

  • Returns a new date type column in the specified table.

Exceptions

  • Raises an error if the input for "Days to Add" is not a valid integer.

DAX Code

The following DAX code defines a calculated column in a table (let's assume the table is named YourTable and the integer column is DaysToAdd).

NewDateColumn = 
    VAR CurrentDate = TODAY()  // Retrieve the current date
    VAR DaysToAdd = 'YourTable'[DaysToAdd]  // Get the value from the DaysToAdd column
    RETURN 
        IF(
            ISERROR(DaysToAdd),  // Validate if DaysToAdd is an error
            BLANK(),  // If there is an error, return a blank value
            CurrentDate + DaysToAdd  // Return the date added to the specified days
        )

Code Explanation

  1. CurrentDate Variable:

    • Uses the TODAY() function to capture today’s date.
  2. DaysToAdd Variable:

    • References the DaysToAdd column in YourTable.
  3. IF Statement:

    • Checks if DaysToAdd is an error using ISERROR().
    • If true, it returns a BLANK, preventing any calculations on invalid inputs.
    • If valid, it adds DaysToAdd to CurrentDate to generate the new date.

Example Usage

Suppose you have a table named YourTable with the following data:

ID DaysToAdd
1 5
2 10
3 -3

After applying the DAX calculated column formula, the table will look like this:

ID DaysToAdd NewDateColumn
1 5 2023-10-20
2 10 2023-10-25
3 -3 2023-10-17

Conclusion

This DAX function effectively allows users to create a new date column based on a dynamic addition of days calculated from the current date. It demonstrates efficient handling of potential errors and adheres to best practices in DAX coding. For advanced DAX training, consider exploring courses 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

This document describes a DAX function that generates a new date column in a table by adding a specified number of days to the current date, allowing for flexible future date calculations.