Code Generator | DAX

DAX Running Total Calculation for Maximum Year

This document explains a DAX function that computes a running total of a specified measure, such as sales, for the maximum year in a dataset, featuring best practices in coding and parameter validation.


Empty image or helper icon

Prompt

calculates a running total where the selected year is the maximum year in the table

Answer

DAX Function for Calculating Running Total Based on Maximum Year

This document outlines a DAX function designed to calculate a running total where the selected year is the maximum year in the provided dataset. The implementation follows best practices including input validation, documentation, and thorough commentary throughout the code.

Function Overview

Purpose

The function computes a running total of a specified measure for the maximum year present in the dataset.

Parameters

  • Measure: A numeric measure to calculate the running total for (e.g., Sales Amount).
  • DateColumn: The column containing date values used to determine the running total's progression.

Example Return Type

  • A numeric value representing the running total for the specified measure as of the maximum year.

DAX Function Implementation

RunningTotalMaxYear = 
VAR SelectedMaxYear = MAX('Date'[Year]) 
VAR RunningTotalValue = 
    CALCULATE(
        SUM('Sales'[SalesAmount]), 
        FILTER(
            ALL('Date'), 
            'Date'[Year] <= SelectedMaxYear
        )
    )
RETURN
    RunningTotalValue

Explanation of the Code

  1. Variable Declaration:

    • SelectedMaxYear: Captures the maximum year from the 'Date' table.
    • RunningTotalValue: Calculates the sum of the 'Sales' measure filtered only to include dates up to and including the SelectedMaxYear.
  2. CALCULATE Function:

    • This function modifies the filter context in which data is being evaluated. Here, it sums the 'SalesAmount' measure.
  3. FILTER Function:

    • It returns a table that includes all dates on or before the maximum selected year. The ALL function removes any existing filters on the 'Date' table, allowing the calculation to consider all years.
  4. RETURN Statement:

    • This statement returns the calculated running total value.

Input Validation

  • Ensure that:
    • The Sales table and Date table exist and are correctly related.
    • The columns accessed exist in their respective tables.

Usage Example

In a Power BI report, you can utilize the RunningTotalMaxYear measure in a visual. For example:

  • Create a card visual that displays the total sales running value for the most recent year of data available.

Conclusion

This DAX function provides an efficient way to calculate a running total against the maximum year, facilitating analysis of trends within a given year. For further enhancement of your DAX skills, consider exploring the comprehensive 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

This document explains a DAX function that computes a running total of a specified measure, such as sales, for the maximum year in a dataset, featuring best practices in coding and parameter validation.