Forum Discussion

Gportari's avatar
Gportari
New Member
10 months ago
Solved

Issue Building a Cash Flow Measure

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
)

"

  • 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
    )

2 Replies

  • 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
    )

    • Gportari's avatar
      Gportari
      New Member

      Thank you very much, my problem was brilliantly solved!!!