Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.
Hello everyone,
I'm currently building an accounting dashboard and I need to create a measure that calculates a running cash flow balance. The challenge is that for the whole year, I have the same initial balance value. For example, from January to December, all months start with an initial balance of 1,000,000.
With this in mind, the required calculation is as follows:
For Creditors (Acredores): Monthly Balance = Initial Balance (January) + Differential Value.
For Debtors (Deudores): Monthly Balance = Initial Balance (January) - Differential Value.
This calculation applies to January. For the subsequent months, the calculation needs to take the closing balance from the previous month and add/subtract its respective differential value.
Let's illustrate with an example for better understanding:
January
Creditors = 1,000,000 + 20,000
Debtors = 1,000,000 - 25,000
January Closing Balance (Creditors) = 1,020,000
January Closing Balance (Debtors) = 975,000
February
Creditors = 1,020,000 (Jan Closing Balance) + 30,000
Debtors = 975,000 (Jan Closing Balance) - 10,000
February Closing Balance (Creditors) = 1,050,000
February Closing Balance (Debtors) = 965,000
I've managed to implement the logic using the following DAX measure, which provides the correct values, but it's extremely slow and causes performance issues when aggregating many columns in a matrix visual:
"
CLOSING_BALANCE_SUMX_ADJUSTED =
var current_year =
IF(
SELECTEDVALUE(DM_TEMPO[ANO]),
VALUES(DM_TEMPO[ANO]),
VALUES(F_TEMPO[ANO_MES_ATUAL])) // Assuming DM_TEMPO is the Date Dimension (DM_TIME)
RETURN
VAR JANUARY_START =
CALCULATE(
SUM(FT_CONTABEL[CTA_SDOINICIAL]), -- Initial Balance Column
ALL(DM_TEMPO),
DM_TEMPO[MES] = 1,
DM_TEMPO[ANO] = current_year
)
VAR JANUARY_FINAL =
IF(SELECTEDVALUE(FT_CONTABEL[CTA_NATURALEZA]) = "Acre", -- "Acre" likely means Credit/Creditor
JANUARY_START
+
CALCULATE(
[Valor_Diferenca],
ALL(DM_TEMPO),
DM_TEMPO[MES] = 1,
DM_TEMPO[ANO] = current_year
),
JANUARY_START
-
CALCULATE(
[Valor_Diferenca],
ALL(DM_TEMPO),
DM_TEMPO[MES] = 1,
DM_TEMPO[ANO] = current_year
)
)
VAR FEBRUARY_START =
JANUARY_FINAL
VAR FEBRUARY_FINAL =
IF(SELECTEDVALUE(FT_CONTABEL[CTA_NATURALEZA]) = "Acre",
FEBRUARY_START
+
CALCULATE(
[Valor_Diferenca],
ALL(DM_TEMPO),
DM_TEMPO[MES] = 2,
DM_TEMPO[ANO] = current_year
),
FEBRUARY_START
-
CALCULATE(
[Valor_Diferenca],
ALL(DM_TEMPO),
DM_TEMPO[MES] = 2,
DM_TEMPO[ANO] = current_year
)
)
/* ... The code continues for all months up to December, following the same pattern ... */
/* ... (Skipping March to November VARs for brevity) ... */
VAR DECEMBER =
NOVEMBER_FINAL
VAR DECEMBER_FINAL =
IF(SELECTEDVALUE(FT_CONTABEL[CTA_NATURALEZA]) = "Acre",
DECEMBER
+
CALCULATE(
[Valor_Diferenca],
ALL(DM_TEMPO),
DM_TEMPO[MES] = 12,
DM_TEMPO[ANO] = current_year
),
DECEMBER
-
CALCULATE(
[Valor_Diferenca],
ALL(DM_TEMPO),
DM_TEMPO[MES] = 12,
DM_TEMPO[ANO] = current_year
)
)
RETURN
SWITCH(TRUE(),
MAX(DM_TEMPO[MES]) = 1,
JANUARY_FINAL, /* Fixed to return final balance of the month */
MAX(DM_TEMPO[MES]) = 2,
FEBRUARY_FINAL, /* Fixed to return final balance of the month */
MAX(DM_TEMPO[MES]) = 3,
MARCO_FINAL, /* Corrected logic to return final balance */
/* ... (Skipping March to November returns for brevity) ... */
MAX(DM_TEMPO[MES]) = 12,
DEZEMBRO_FINAL
)
"
Solved! Go to Solution.
@Gportari Try using
Use a Running Total Pattern
1. Calculate the Initial Balance for January
dax
Initial_Balance =
CALCULATE(
SUM(FT_CONTABEL[CTA_SDOINICIAL]),
FILTER(
ALL(DM_TEMPO),
DM_TEMPO[ANO] = SELECTEDVALUE(DM_TEMPO[ANO]) &&
DM_TEMPO[MES] = 1
)
)
2. Calculate the Cumulative Differential Value up to the Current Month
dax
Cumulative_Differential =
CALCULATE(
[Valor_Diferenca],
FILTER(
ALL(DM_TEMPO),
DM_TEMPO[ANO] = SELECTEDVALUE(DM_TEMPO[ANO]) &&
DM_TEMPO[MES] <= MAX(DM_TEMPO[MES])
)
)
3. Calculate the Running Balance
dax
Running_Balance =
VAR Initial = [Initial_Balance]
VAR CumulativeDiff = [Cumulative_Differential]
RETURN
IF(
SELECTEDVALUE(FT_CONTABEL[CTA_NATURALEZA]) = "Acre",
Initial + CumulativeDiff,
Initial - CumulativeDiff
)
Full Example Measure
dax
Running_Balance =
VAR CurrentYear = SELECTEDVALUE(DM_TEMPO[ANO])
VAR CurrentMonth = MAX(DM_TEMPO[MES])
VAR InitialBalance =
CALCULATE(
SUM(FT_CONTABEL[CTA_SDOINICIAL]),
FILTER(
ALL(DM_TEMPO),
DM_TEMPO[ANO] = CurrentYear &&
DM_TEMPO[MES] = 1
)
)
VAR CumulativeDiff =
CALCULATE(
[Valor_Diferenca],
FILTER(
ALL(DM_TEMPO),
DM_TEMPO[ANO] = CurrentYear &&
DM_TEMPO[MES] <= CurrentMonth
)
)
RETURN
IF(
SELECTEDVALUE(FT_CONTABEL[CTA_NATURALEZA]) = "Acre",
InitialBalance + CumulativeDiff,
InitialBalance - CumulativeDiff
)
Proud to be a Super User! |
|
@Gportari Try using
Use a Running Total Pattern
1. Calculate the Initial Balance for January
dax
Initial_Balance =
CALCULATE(
SUM(FT_CONTABEL[CTA_SDOINICIAL]),
FILTER(
ALL(DM_TEMPO),
DM_TEMPO[ANO] = SELECTEDVALUE(DM_TEMPO[ANO]) &&
DM_TEMPO[MES] = 1
)
)
2. Calculate the Cumulative Differential Value up to the Current Month
dax
Cumulative_Differential =
CALCULATE(
[Valor_Diferenca],
FILTER(
ALL(DM_TEMPO),
DM_TEMPO[ANO] = SELECTEDVALUE(DM_TEMPO[ANO]) &&
DM_TEMPO[MES] <= MAX(DM_TEMPO[MES])
)
)
3. Calculate the Running Balance
dax
Running_Balance =
VAR Initial = [Initial_Balance]
VAR CumulativeDiff = [Cumulative_Differential]
RETURN
IF(
SELECTEDVALUE(FT_CONTABEL[CTA_NATURALEZA]) = "Acre",
Initial + CumulativeDiff,
Initial - CumulativeDiff
)
Full Example Measure
dax
Running_Balance =
VAR CurrentYear = SELECTEDVALUE(DM_TEMPO[ANO])
VAR CurrentMonth = MAX(DM_TEMPO[MES])
VAR InitialBalance =
CALCULATE(
SUM(FT_CONTABEL[CTA_SDOINICIAL]),
FILTER(
ALL(DM_TEMPO),
DM_TEMPO[ANO] = CurrentYear &&
DM_TEMPO[MES] = 1
)
)
VAR CumulativeDiff =
CALCULATE(
[Valor_Diferenca],
FILTER(
ALL(DM_TEMPO),
DM_TEMPO[ANO] = CurrentYear &&
DM_TEMPO[MES] <= CurrentMonth
)
)
RETURN
IF(
SELECTEDVALUE(FT_CONTABEL[CTA_NATURALEZA]) = "Acre",
InitialBalance + CumulativeDiff,
InitialBalance - CumulativeDiff
)
Proud to be a Super User! |
|
Thank you very much, my problem was brilliantly solved!!!
Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!
Check out the October 2025 Power BI update to learn about new features.
User | Count |
---|---|
10 | |
7 | |
5 | |
4 | |
3 |
User | Count |
---|---|
12 | |
11 | |
10 | |
9 | |
8 |