Emulación de la función TOTALYTD

The TOTALYTD function is easy to use and allows us to quickly calculate the result of an expression for the period between January 1st and the current context's last date, but sometimes we need more control over the evaluated expression. In this scenario, we will simulate the TOTALYTD function using other functions that would allow us this additional control.

We start with a table called Movements where we find financial movements associated with a date (we will assume that these are sales):

Tabla de movimientos

On the other hand, of course, we have a table containing a calendar (table Calendar):

Calendario

Both tables are related by the date field. To simulate the TOTALYTD function, we want to calculate the accumulation of the Movements[Total] field from the beginning of the year (of the period involved in the current context) to the last date of the "current period".

The base expression will be the sum of the Movements[Total] field, an expression that will serve as the first argument of the CALCULATE function, to which we must add as a second argument the modification of the filter context to be applied. In pseudo-code, the measure to be created is as follows:

Running Total = 
    CALCULATE(
        SUM(Movements[Total]),
        Period between January 1st of the current year and the last day of the current period
    )

To calculate the period in question (between January 1st of the corresponding year and the last day of the current context), we can use the FILTER function to select those calendar records that involve the dates of interest. That is, our pseudo-code would look like this:

Running Total = 
    CALCULATE(
        SUM(Movements[Total]),
        FILTER(
            Calendar,
            Period between January 1st of the current year and the last day of the current period
        )
    )

With the MAX function applied to the calendar, we can calculate both the last day of the current context (by applying the function to the Date field) and the year being considered (by applying the function to the Año -year- field), so to extract the period of interest we could impose two conditions:

  1. That the date of the calendar is earlier than the last day of the current context
  2. That the year of the calendar is equal to the year of the context

That is:

 Running Total = 
    CALCULATE(
        SUM(Movements[Total]),
        FILTER(
            Calendar,
            Calendar date earlier than last day of current context AND
            Calendar year equal to year of context
        )
    )

Taking the real fields and functions to the previous pseudo-code, we have:

Running Total = 
    CALCULATE(
        SUM(Movements[Total]),
        FILTER(
            ALL('Calendar'),
            'Calendar'[Date] <= MAX('Calendar'[Date]) &&
            'Calendar'[Año] = MAX('Calendar'[Año]
            )
        )
    )

Código final

We must make sure that the Calendar table is not being filtered, so the ALL function is applied (otherwise it would be contextualized and would not allow us to always extract the period from the beginning of the year).

DAX functions involved
Difficulty
Intermediate
Submitted by admin on Mon, 01/09/2023 - 09:46