Use ending balance of previous row in next row for calculation

  Kiến thức lập trình

I need to calculate the opening balance and ending balance based on date.
Here are three main measures involved.

OpeningBalance
For 1st day, the initial amount is coming from another table. I have a measure for each year that fetches the value based on year selected.
For the rest of the dates, the value of EndingBalance of previous date is the opening balance of next date.

Sales
This column contains sales amount.

EndingBalance
MonthlyEndingBalance = MonthlyOpeningBalance + MonthSales

Example.
For 1st day
OpeningBalance=10

Sales=15

EndingBalance (OpeningBalance+Sales) =25

For next day

OpeningBalance=25 (EndingBalance of previous row)

Sales=20

EndingBalance (EndingBalance of previous row +Sales) = 45

repeat same logic until the last available date.

Here is a simple example for understanding.

Simple Example.

Here is the PBIX file with dummy data. Logic is same but it tells the whole story .
Download PBIX here

Thanks in advance

Opening Balance Adjusted =

VAR _SelectedMonth = SELECTEDVALUE(Activitylist[FX_MonthNumber])
VAR _SelectedYear = SELECTEDVALUE(Activitylist[FX_Year])

RETURN
IF(
MAX(Activitylist[IsFirstMonth]) = 1 && _SelectedYear = SELECTEDVALUE(Activitylist[FX_Year]),
[OB Initial Column Measure], // Use OB2024 measure for the first month

CALCULATE(
    [Closing Balance Measure],

    FILTER(
        ALL('Activitylist'),
        'Activitylist'[FX_MonthNumber] = _SelectedMonth - 1 &&
        'Activitylist'[FX_Year] = _SelectedYear &&
        'Activitylist'[LoB] = SELECTEDVALUE(Activitylist[LoB])
    )
)

)

Closing Balance Measure =
VAR _SelectedMonth = SELECTEDVALUE(Activitylist[FX_MonthNumber])
VAR _SelectedYear = SELECTEDVALUE(Activitylist[FX_Year])
VAR _OBInitial = SELECTEDVALUE(Activitylist[OBInitialColumn])
VAR _OBdjusted = SELECTEDVALUE(Activitylist[OBAdjustedColumn])

RETURN
IF(
MIN(Activitylist[IsFirstMonth]) = 1 && SELECTEDVALUE(Activitylist[FX_Year]) = _SelectedYear,
_OBInitial + [Monthly Variation], // For the first month
CALCULATE(
ROUND( [OB Initial] + [Monthly Variation],0)
// here is the issue. instead of [OB Initial] , [Opening Balance Adjusted] should be here but circular dependency error occurs.
)

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT