Strange CLOSINGBALANCE behaviour
TLDR: I am trying to get a closing balance for a measure using the CLOSINGBALANCEMONTH function. For some reason for the end-of-quarter values it is summing the monthly values instead of giving the end-of-quarter value. But if I change it slightly in tests I get the right answer.
A simplified version of the measure is as follows:
BTD with MTD Amount test =
VAR res = CALCULATE(
SUMX('Financial Data',
VAR amt = 'Financial Data'[BTD with MTD Amount lower] + 1000 * 'Financial Data'[BTD with MTD Amount upper]
RETURN
amt
))
RETURN
CLOSINGBALANCEMONTH(res, 'Fiscal Period'[Fiscal Month])My calendar table (Fiscal Period) and fact table (Financial Data) are at a monthly granularity with Fiscal Month as the relationship column.
Using the above measure if I look by Quarter and Fiscal month I am getting a table like this, which is obviously not expected (I'd expect the quarterly figures to be 100)
| Fiscal Quarter | BTD with MTD Amount Test |
| 2025-Q1 | 300 |
| 2025-Q2 | 300 |
| 2023-Q3 | 300 |
| 2025/07/31 | 100 |
| 2025/08/31 | 100 |
| 2025/09/30 | 100 |
| 2025-Q4 | 100 |
| 2025/10/31 | 100 |
If I replace the measure with the following however, I get the correct answer:
BTD with MTD Amount test = CLOSINGBALANCEMONTH(CALCULATE(SUMX('Financial Data', 'Financial Data'[BTD with MTD Amount lower] + 1000 * 'Financial Data'[BTD with MTD Amount upper])), 'Fiscal Period'[Fiscal Month])To me these two versions are logically equivalent, so why are they giving different answers?
In practice my SUMX has a whole bunch more logic inside it which is why I'm separating it out into a separate VAR to keep the code manageable.
But I am also getting the correct answer in the real one if I move everything out of the VAR into the CLOSINGBALANCEMONTH.
Hi MBZA,
Thats weird behavior but it seems like in your first version
BTD with MTD Amount test = VAR res = CALCULATE( SUMX('Financial Data', VAR amt = 'Financial Data'[BTD with MTD Amount lower] + 1000 * 'Financial Data'[BTD with MTD Amount upper] RETURN amt )) RETURN CLOSINGBALANCEMONTH(res, 'Fiscal Period'[Fiscal Month])that the variable res is evaluated first in the current filter context (which is at quarter level includes all months in quarter) then CLOSINGBALANCEMONTH operates on that already aggregated value
And in your second version (working solution you provided)
BTD with MTD Amount test = CLOSINGBALANCEMONTH(CALCULATE(SUMX('Financial Data', 'Financial Data'[BTD with MTD Amount lower] + 1000 * 'Financial Data'[BTD with MTD Amount upper])), 'Fiscal Period'[Fiscal Month])that he entire expression is evaluated inside the CLOSINGBALANCEMONTH function (so it can properly apply the end of month context)
So here is some Approach you can try (I hope it works):- Use a measure reference
BTD with MTD Amount test = VAR BaseMeasure = [Your Base Measure] // Reference another measure RETURN CLOSINGBALANCEMONTH(BaseMeasure, 'Fiscal Period'[Fiscal Month])- Put the all calculation in CALCULATE
BTD with MTD Amount test = VAR complexCalculation = SUMX('Financial Data', // Your complex logic here ) RETURN CLOSINGBALANCEMONTH(CALCULATE(complexCalculation), 'Fiscal Period'[Fiscal Month])Use iterator functions
BTD with MTD Amount test = SUMX( VALUES('Fiscal Period'[Fiscal Month]), CLOSINGBALANCEMONTH( CALCULATE( SUMX('Financial Data', // Your complex logic ) ), 'Fiscal Period'[Fiscal Month] ) )
So the idea here is that time intelligence functions need to control the filter context themselves (they cant work properly on pre-calculated results)if this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.