Fix Incorrect Totals

When using a measure column in a visual, it is not uncommon in Power BI to find that the totals are incorrect. The Fix Incorrect Totals DAX pattern can be used to correct this issue.

When a DAX measure is providing the correct detail value it often, unfortunately, provides an incorrect value for the total row. This is due to there being no evaluation context for the total row. For example, when comparing the [Total Sales] to the [Sales LY] to find the minimum for a customer using the simple DAX formula:

Minimum (Incorrect Total) = 
MIN( [Total Sales], [Sales LY] )

The values for the detail rows would be correct but value for the total row would be incorrect.

In this example, the total row shows the minimum between the 2 total values rather than the total of the minimum column. To get the total of the minimum column, a virtual table can be constructed in the DAX measure

Minimum 1 (Correct Total) = 
// DAX PATTERN NAME: Fix Incorrect Totals
// NOTES: Use ADDCOLUMNS to create a virtual table (consisting of the columns used in your table) inside an iterator
// TIP: Select the text to change, then use CRTL + SHIFT + L to rename all occurrences at once
// TIP: Use CRTL + mouse scroll wheel to zoom the text size
SUMX(
    ADDCOLUMNS(
        VALUES( Customers[Customer Name] ), -- column 1 of the table visual and virtual table
            "Sales", [Total Sales],         -- column 2 of the table visual and virtual table
            "Sales LY", [Sales LY]          -- column 3 of the table visual and virtual table
    ),
    MIN( [Sales], [Sales LY] )              -- return the minimum of column 2 and column 3
)

NOTE: SUMMARIZE can also be used in place of ADDCOLUMNS, but be aware that when using calculated columns, it can lead to inconsistent results and performance issues.

Another method is to calculate separate values for the detail and total rows, and to use the HASONEVALUE() or ISINSCOPE() functions to test the context to see if one is on a detail row, e.g.:

Minimum 2 (Correct Total) = 
// DAX PATTERN NAME: Fix Incorrect Totals
// NOTES: Use ADDCOLUMNS to create a virtual table (consisting of the columns used in your table) inside an iterator
// TIP: Select the text to change, then use CRTL + SHIFT + L to rename all occurrences at once
// TIP: Use CRTL + mouse scroll wheel to zoom the text size
IF(
    HASONEVALUE( Customers[Customer Name] ),
    MIN( [Total Sales], [Sales LY] ),
    SUMX(
        ADDCOLUMNS(
            VALUES( Customers[Customer Name] ), -- column 1 of the table visual and virtual table
                "Sales", [Total Sales],         -- column 2 of the table visual and virtual table
                "Sales LY", [Sales LY]          -- column 3 of the table visual and virtual table
        ),
        MIN( [Sales], [Sales LY] )              -- return the minimum of column 2 and column 3
    )
)


Minimum 3 (Correct Total) = 
// DAX PATTERN NAME: Fix Incorrect Totals
// NOTES: Use ADDCOLUMNS to create a virtual table (consisting of the columns used in your table) inside an iterator
// TIP: Select the text to change, then use CRTL + SHIFT + L to rename all occurrences at once
// TIP: Use CRTL + mouse scroll wheel to zoom the text size
IF(
    ISINSCOPE( Customers[Customer Name] ),
    MIN( [Total Sales], [Sales LY] ),
    SUMX(
        ADDCOLUMNS(
            VALUES( Customers[Customer Name] ), -- column 1 of the table visual and virtual table
                "Sales", [Total Sales],         -- column 2 of the table visual and virtual table
                "Sales LY", [Sales LY]          -- column 3 of the table visual and virtual table
        ),
        MIN( [Sales], [Sales LY] )              -- return the minimum of column 2 and column 3
    )
)

This situation can also occur when performing a Pareto analysis, where once again the context must be taken into account. To get, for example, the total sales of the Top 20% of Customers in each state, one might try a single measure:

Sales of Top 20% (Incorrect Total) = 
VAR CustomerTop20Percent = DISTINCTCOUNT( Sales[Customer ID] ) * 0.2
RETURN
CALCULATE( [Total Sales],
    FILTER( VALUES( Sales[Customer ID] ),
        RANKX( VALUES( Sales[Customer ID] ), [Total Sales], , DESC ) <= CustomerTop20Percent ) )

Again, the values for the detail rows would be correct but the grand total would be incorrect, as it ignores the state context.

To get the correct grant total, use the measure branching technique and create another measure that includes the state context.

Sales of Top 20% (Correct Total) = 
// DAX PATTERN NAME: Fix Incorrect Totals
// NOTES: Use ADDCOLUMNS to create a virtual table (consisting of the columns used in your table ) inside an iterator
// TIP: Select the text to change, then use CRTL + SHIFT + L to rename all occurrences at once
// TIP: Use CRTL + mouse scroll wheel to zoom the text size
SUMX(
    ADDCOLUMNS(
        VALUES
           ( Locations[State Code] ),                                                   -- column 1 of the table visual and virtual table
            "Total Sales", [Total Sales],                                               -- column 2 of the table visual and virtual table
            "Total Sales of Top 20% Customers", [Sales of Top 20% (Incorrect Total)]    -- use the base measure as column 3 of the virtual table
    ),
   [Total Sales of Top 20% Customers]                                                   -- return column 3
)

DAX Patterns - Fix Incorrect Totals.pbix (364.4 KB)

Reference:

HASONEVALUE
Returns TRUE when the context for columnName has been filtered down to one distinct value only.

ISINSCOPE
Returns TRUE when the specified column is the level in a hierarchy of levels.

8 Likes
Matrix Totals with Switch measure and ISINSCOPE
How to Sum total from each group
Totals not summing
Is it possible to use If statement to select a measure?
Challenge in growth rate calculation
Forecast based on current YoY growth
BOM ( Manufacturing Bill of Material ) totals is incorrect for final assembly cost
Wrong values on total
Accounts Receivable Aging
Calculate % of tickets responded to in time
Table total not calculating correctly
Total by Customer when If statement used
YTD value not showing sub total
Keeping a filter associated with a measure
Multi period excess earning method valuation
Matrix Percentages - hasonevalue issue
Cannot Get a Measure to Sum As Expected
Totalling Issue - Maybe!
Calculation based on last known value in time
Computing daily Cash Closing Balance & forecast using DAX
Group By Division, IF Statement & Proper Subtotal Calculation
Distinctcount not showing total
Replicate AverageX
Unique values for a new Column
Combine two measures into one measure by date
Multiple Date Filter
Multiple Date Filter
Incorrect Total using ISINSCOPE
Measures Total doesn't work
Total Not Showing in Table
Multiplication Measure and Column issue
DAX Dynamic virtual table Grouping and keep Max date and Max Number
Need help with sales in first 7 days
Total is not coming correct in the matrix visual at Parent Level
Multi days (date/time) rental daily revenue
Wrong Total in matrix column
Convert Text from a measure into an Integer
SUMX On DateDiff?
Total count after application of segmentation is incorrect
Cumulative Total Giving Unexpected Results
Lookupvalue and min
Issues with Totals for Dax Measure
Correct Row Totals but Incorrect Row Totals on Table Matrix
Nested If measures (DAX)
Can't get the correct total
The total in a measure is different than expected
Sum of numbers does not equal the total
Distinct count of employees who have taken a leave of more than 10 Days (Personal Leave & Vacation)
Incorrect Total
Adjustment of an Item
Problem of the Week #3 (DAX) - Timesheet and Wages
Savings upon saving
Create a Moving Range Measure
Forecast Total Issue
Quick Dax Question - Please
Maybe SUMX issue?
New/Lost Customers not totaling correctly
Calculating Sum of savings not total
Pesty sumx with absolute value
Formula incorrect at Total level
Dax calculation to sum based on criteria
Switch Measure Not Showing Grand Total
Measure does not calculate value for all areas
Dealing with ship date filters when some lines have not shipped
ALLexcept help to calculate correct total
Why Table Total missing when using a filter with a high margin?
Exclude Zero results in Overall Calculation?
Total of table is not correct
Count rows in matrix
Differing treatment of results from same IF statement with different condition
Weekly Average Excluding Dates with No (Zero) Orders
Price index dashboard - wrong subtotal
Calculate Max Value in a Column Based on Another Column (Group)
Incorrect Totals when using Custom Time Intelligence DAX
Measure showing wildly different values in chart than in table

Just adding some keywords to make this pattern easier to locate via forum search:

DAX pattern, incorrect total, correct total, measure total, grand total, ADDCOLUMNS, SUMMARIZE, HASONEVALUE, ISINSCOPE, Pareto.

Related Content:

Below are a few examples of issues related to the Fix Incorrect Totals DAX Pattern from the eDNA resources.

Enterprise DNA Courses:

Enterprise DNA Forum (Search):

https://forum.enterprisedna.co/t/fixing-complex-total-errors-dax-formula-concepts/598

https://forum.enterprisedna.co/t/fixing-errors-with-your-totals-efficiently/4948

Enterprise DNA Forum (EDNA):

Enterprise DNA TV (YouTube):

Fixing Complex Total Errors - DAX Formula Concepts

Getting Totals Correct When Using Advanced Logic - Power BI & DAX

Why Your Total Is Incorrect In Power BI - The Key DAX Concept To Understand