At some point or another every DAX author realizes that the total row is **not **the sum of each row/cell in a given column. This can be quite confusing and is exactly why I am writing a third blog post dedicated to the total row. If you missed part one or part two in this series you can find them below:

Part 1: Unexpected Totals in DAX (Part 1)

Part 2: Unexpected Totals in DAX (Part 2)

## Problem

In this blog, we want to return **YTD Sales** for previous months and **Forecast YTD Sales** for the current month. Therefore, there should be only one measure and that measure should return YTD Sales or Forecast YTD based on the month.

In the following table there are three measures, the first measure is YTD Sales and it tracks the actual sales. The second measure is Forecast YTD Sales and this is the forecasted sales. The third measure is the Dynamic Measure, this is the proposed measure designed to replace the other two measures. However, the total row produces unexpected results, and therefore is perfect for this blog post:

In the above screenshot, the total row for the dynamic measure is displaying $19,582,120 but the sum of all the rows is actually $28,164,680. Let’s take a look at the dynamic measure and figure out why the total row is not what we might expect.

## Dynamic Measure

The “Dynamic Measure” is returning the YTD Sales for completed months and returning the Forecast YTD Sales for the current month. Let’s take a quick look at the measure just to understand better what is going on. Please note, for this example we assume there are sales for every day of the year.

- A = Two variables to make the code more readable (See descriptions below):
- LastSaleDate = Return the last day the company had a sale (in the current filter context)
- LastDayOfMonth = Return the last day from the date table (in the current filter context)

- B = The logical test performed by the IF function.
- If the two variables don’t match, then it is the current month and [Forecast YTD Sales] is returned.
- If the two variables do match, then it is a completed month and [YTD Sales] is returned.

## Filter Context of the Total Row

The total row is executing the dynamic measure within the context at the total row. What is the last day in the date table within the current filter context? At the total row it is 12/31/2008, there is a filter on the report page that filters the report down to only the year 2008. The last day that there was a valid sale was on 6/20/2008. Since 12/31/2008 does not equal 6/20/2008 the calculation returns **Forecasted YTD Sales**, which at the total level is $19,582,120.60.

Steps to solve this problem:

- Determine if the calculation is at the total row.
- For the total row perform the Dynamic Measure calculation for each month in the table separately.
- Sum the results of each result separately.

However, steps two and three above are a little bit more complex than they sound and therefore to complete the solution we need to introduce you to two new functions in the DAX language: VALUES and SUMX. The great news is this is a design pattern that you will be able to use in many different scenarios!

## The VALUES function in DAX

__MSDN Definition:__

Returns a one-column table that contains the **distinct **values from the specified table or column.

Remember step 2 from above? We want to execute our dynamic measure against each row in the table, the VALUES function will get us the distinct list of the months. Let’s take a look at the VALUES function in action. I have created a table using the following formula to display the results:

The VALUES function returned a distinct list of months. This is exactly what we need. Now, how do we execute our measure against each row in this table and then SUM all the results at the very end to get the expected value at the total row? **SUMX!!**

## Working with the SUMX Function in DAX

__MSDN Definition:__ Returns the sum of an expression evaluated for each row in a table.

Syntax:

Perfect! The SUMX function is going to iterate over the list of months and execute the dynamic measure against each month. After this process has completed the SUMX function will then SUM the results returned for each month.

SUMX accepts two parameters, the first parameter is a table and the second parameter is the expression. The table that is returned from the VALUES function is the first parameter and the dynamic measure is passed in as the second parameter (expression). Let’s take a look at the final solution.

## Solution

The following solution now returns the expected results for the total row:

Here are the results, pay special attention to the total row:

Thanks for reading my post!