Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
arnie
Helper I
Helper I

column grand total not summing up properly

* My dax below is giving me correct row subtotal and row grand totals. BUT it is giving me incorrect column grandtotals.

 

* The grand totals should be the sum of the [Tonnes Actual] component up to current monthyear + [Tonnes Budget] component for future monthyear values from current monthyear value.

 

* Instead it is giving me only the [Tonnes Budget] values across all dates. 

 

Please help.

 

_Calc. Budget =
VAR CurrentMonthYear = VALUE(FORMAT(TODAY(), "YYYYMM"))
VAR ColumnDate = MAX('Date'[Date])
VAR ColumnMonthYear =
    IF(
        ISBLANK(ColumnDate),
        BLANK(),
        VALUE(FORMAT(ColumnDate, "YYYYMM"))
    )

VAR IsGrandTotal = ISBLANK(ColumnDate)

VAR ActualValue =
    IF(
        ColumnMonthYear <= CurrentMonthYear,
        [Tonnes Actual],
        BLANK()
    )

VAR BudgetValue =
    IF(
        ColumnMonthYear > CurrentMonthYear,
        [Tonnes Budget],
        BLANK()
    )

VAR ActualTotal =
    CALCULATE(
        [Tonnes Actual],
        FILTER(
            ALL('Date'),
            VALUE('Date'[Month Year]) <= CurrentMonthYear
        )
    )

VAR BudgetTotal =
    CALCULATE(
        [Tonnes Budget],
        FILTER(
            ALL('Date'),
            VALUE('Date'[Month Year]) > CurrentMonthYear
        )
    )

RETURN
IF(
    IsGrandTotal,
    // For Grand Total: Sum all Actual and Budget values across all relevant dates
    ActualTotal + BudgetTotal,
    // For regular rows, return Actual or Budget based on the month
    ActualValue + BudgetValue
)
1 ACCEPTED SOLUTION
arnie
Helper I
Helper I

I have the solution DAX below just to give closure to this thread. Thank you for your efforts.

 

_Calc. Budget =
VAR CurrentDate = TODAY()
VAR CurrentMonthYear = FORMAT(CurrentDate, "YYYYMM")
VAR StartMonthYear = "202407"

-- Check the level of the calculation
VAR IsRowLevel =
    ISINSCOPE(SalesData[Product Type]) ||
    ISINSCOPE(SalesData[Order Group]) ||
    ISINSCOPE(SalesData[Sales Class Alt.]) ||
    ISINSCOPE(SalesData[Region]) ||
    ISINSCOPE(SalesData[Market])

VAR IsColumnLevel = ISINSCOPE('Date'[Year-Month Code])

-- Calculate actual and budget values for the current Year-Month Code
VAR ColumnDate = MAX('Date'[Year-Month Code])

VAR ActualTonnesForMonth =
    CALCULATE(
        [Tonnes Actual],
        'Date'[Year-Month Code] = ColumnDate,
        'Date'[Year-Month Code] >= StartMonthYear,
        'Date'[Year-Month Code] <= CurrentMonthYear
    )

VAR BudgetTonnesForMonth =
    CALCULATE(
        [Tonnes Budget],
        'Date'[Year-Month Code] = ColumnDate,
        'Date'[Year-Month Code] > CurrentMonthYear
    )

-- Calculate actual and budget values for all Year-Month Codes in the row's scope
VAR ActualTonnesForRow =
    CALCULATE(
        [Tonnes Actual],
        'Date'[Year-Month Code] >= StartMonthYear,
        'Date'[Year-Month Code] <= CurrentMonthYear,
        REMOVEFILTERS('Date'[Year-Month Code])
    )

VAR BudgetTonnesForRow =
    CALCULATE(
        [Tonnes Budget],
        'Date'[Year-Month Code] > CurrentMonthYear,
        REMOVEFILTERS('Date'[Year-Month Code])
    )

-- Total values for columns or rows
VAR TotalForMonth = ActualTonnesForMonth + BudgetTonnesForMonth
VAR TotalForRow = ActualTonnesForRow + BudgetTonnesForRow

-- Grand Total Calculation
VAR GrandTotal =
    CALCULATE(
        ActualTonnesForRow + BudgetTonnesForRow,
        REMOVEFILTERS('Date'[Year-Month Code]),  -- Remove filters for Year-Month Code
        REMOVEFILTERS(SalesData)  -- Remove filters for SalesData
    )

-- Determine the value based on context
RETURN
IF(
    IsRowLevel && IsColumnLevel,
    -- At the intersection of row and column (specific Year-Month Code and row level)
    IF(
        ColumnDate <= CurrentMonthYear && ColumnDate >= StartMonthYear,
        [Tonnes Actual],
        IF(
            ColumnDate > CurrentMonthYear,
            [Tonnes Budget],
            BLANK()
        )
    ),
    IF(
        IsRowLevel,
        -- Subtotal for rows (aggregate across all months for the row's scope)
        TotalForRow,
        IF(
            IsColumnLevel,
            -- Subtotal for columns (aggregate across all rows for the column's scope)
            TotalForMonth,
            -- Grand total (bottom-right cell)
            GrandTotal
        )
    )
)

View solution in original post

7 REPLIES 7
arnie
Helper I
Helper I

arnie_0-1732069312330.png

 

arnie
Helper I
Helper I

I have the solution DAX below just to give closure to this thread. Thank you for your efforts.

 

_Calc. Budget =
VAR CurrentDate = TODAY()
VAR CurrentMonthYear = FORMAT(CurrentDate, "YYYYMM")
VAR StartMonthYear = "202407"

-- Check the level of the calculation
VAR IsRowLevel =
    ISINSCOPE(SalesData[Product Type]) ||
    ISINSCOPE(SalesData[Order Group]) ||
    ISINSCOPE(SalesData[Sales Class Alt.]) ||
    ISINSCOPE(SalesData[Region]) ||
    ISINSCOPE(SalesData[Market])

VAR IsColumnLevel = ISINSCOPE('Date'[Year-Month Code])

-- Calculate actual and budget values for the current Year-Month Code
VAR ColumnDate = MAX('Date'[Year-Month Code])

VAR ActualTonnesForMonth =
    CALCULATE(
        [Tonnes Actual],
        'Date'[Year-Month Code] = ColumnDate,
        'Date'[Year-Month Code] >= StartMonthYear,
        'Date'[Year-Month Code] <= CurrentMonthYear
    )

VAR BudgetTonnesForMonth =
    CALCULATE(
        [Tonnes Budget],
        'Date'[Year-Month Code] = ColumnDate,
        'Date'[Year-Month Code] > CurrentMonthYear
    )

-- Calculate actual and budget values for all Year-Month Codes in the row's scope
VAR ActualTonnesForRow =
    CALCULATE(
        [Tonnes Actual],
        'Date'[Year-Month Code] >= StartMonthYear,
        'Date'[Year-Month Code] <= CurrentMonthYear,
        REMOVEFILTERS('Date'[Year-Month Code])
    )

VAR BudgetTonnesForRow =
    CALCULATE(
        [Tonnes Budget],
        'Date'[Year-Month Code] > CurrentMonthYear,
        REMOVEFILTERS('Date'[Year-Month Code])
    )

-- Total values for columns or rows
VAR TotalForMonth = ActualTonnesForMonth + BudgetTonnesForMonth
VAR TotalForRow = ActualTonnesForRow + BudgetTonnesForRow

-- Grand Total Calculation
VAR GrandTotal =
    CALCULATE(
        ActualTonnesForRow + BudgetTonnesForRow,
        REMOVEFILTERS('Date'[Year-Month Code]),  -- Remove filters for Year-Month Code
        REMOVEFILTERS(SalesData)  -- Remove filters for SalesData
    )

-- Determine the value based on context
RETURN
IF(
    IsRowLevel && IsColumnLevel,
    -- At the intersection of row and column (specific Year-Month Code and row level)
    IF(
        ColumnDate <= CurrentMonthYear && ColumnDate >= StartMonthYear,
        [Tonnes Actual],
        IF(
            ColumnDate > CurrentMonthYear,
            [Tonnes Budget],
            BLANK()
        )
    ),
    IF(
        IsRowLevel,
        -- Subtotal for rows (aggregate across all months for the row's scope)
        TotalForRow,
        IF(
            IsColumnLevel,
            -- Subtotal for columns (aggregate across all rows for the column's scope)
            TotalForMonth,
            -- Grand total (bottom-right cell)
            GrandTotal
        )
    )
)
Bibiano_Geraldo
Solution Sage
Solution Sage

HI @arnie ,
Please Try the following DAX code:

_Calc. Budget =
VAR CurrentMonthYear = VALUE(FORMAT(TODAY(), "YYYYMM"))
VAR ColumnDate = MAX('Date'[Date])
VAR ColumnMonthYear =
    IF(
        ISBLANK(ColumnDate),
        BLANK(),
        VALUE(FORMAT(ColumnDate, "YYYYMM"))
    )

VAR IsGrandTotal = ISINSCOPE('Date'[Month Year]) = FALSE

VAR ActualValue =
    IF(
        ColumnMonthYear <= CurrentMonthYear,
        [Tonnes Actual],
        BLANK()
    )

VAR BudgetValue =
    IF(
        ColumnMonthYear > CurrentMonthYear,
        [Tonnes Budget],
        BLANK()
    )

// Correcting the Actual Total to sum up until the current month-year
VAR ActualTotal =
    CALCULATE(
        [Tonnes Actual],
        REMOVEFILTERS('Date'),
        'Date'[Month Year] <= CurrentMonthYear
    )

// Correcting the Budget Total to sum from future months beyond the current month-year
VAR BudgetTotal =
    CALCULATE(
        [Tonnes Budget],
        REMOVEFILTERS('Date'),
        'Date'[Month Year] > CurrentMonthYear
    )

RETURN
IF(
    IsGrandTotal,
    // For Grand Total: Combine the corrected Actual and Budget totals
    ActualTotal + BudgetTotal,
    // For individual rows, show the Actual or Budget based on the current month-year
    ActualValue + BudgetValue
)
I hope this helps! 
If you found this answer helpful:
✔️ Mark it as the solution to help others find it faster.
 Give it a kudo to show your appreciation!

Thank you for being an awesome community member! 

Hi @Bibiano_Geraldo , sorry that didn't work, thank you for trying.

Kedar_Pande
Resident Rockstar
Resident Rockstar

@arnie 

Modified version of your DAX measure:

_Calc. Budget =
VAR CurrentMonthYear = VALUE(FORMAT(TODAY(), "YYYYMM"))
VAR ColumnDate = MAX('Date'[Date])
VAR ColumnMonthYear =
IF(
ISBLANK(ColumnDate),
BLANK(),
VALUE(FORMAT(ColumnDate, "YYYYMM"))
)

VAR IsGrandTotal = ISINSCOPE('Date'[Date]) = FALSE

VAR ActualValue =
IF(
ColumnMonthYear <= CurrentMonthYear,
[Tonnes Actual],
BLANK()
)

VAR BudgetValue =
IF(
ColumnMonthYear > CurrentMonthYear,
[Tonnes Budget],
BLANK()
)

VAR ActualTotal =
CALCULATE(
[Tonnes Actual],
FILTER(
ALL('Date'),
VALUE('Date'[Month Year]) <= CurrentMonthYear
)
)

VAR BudgetTotal =
CALCULATE(
[Tonnes Budget],
FILTER(
ALL('Date'),
VALUE('Date'[Month Year]) > CurrentMonthYear
)
)

RETURN
IF(
IsGrandTotal,
ActualTotal + BudgetTotal,
ActualValue + BudgetValue
)

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

Sorry @Kedar_Pande , that gave the same result as @Bibiano_Geraldo  did.

* Hi Kedar, please see my screenshots below. 

 

* As you can see, Tonnes + Budget does add up correctly for corresponding date, but the column grand total (3rd table) is the same as the total for budget (2nd table). I need the highlighted totals for Actuals to ADD up with the budget values from Dec 24 onwards to give grand total of 364,007.

 

* Your solution is not adding up also, but thanks for trying.

arnie_0-1731649380126.png

arnie_1-1731649635193.png

 

 

Helpful resources

Announcements
Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.