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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
feltaha
Frequent Visitor

write a measure for alternating opening balances and closing balances Year on year

Hi, 

 

Is there a way to create a Measuer for the opening balance that will change based on the selected year? the opening balance for the year 2019 is zero, while the closing balance for 2019 = OB2019 (Opening Balance for 2019) + Debit column - Credit Column. for the year 2020 opening balance = Closing Balance for 2019, and closing balance for 2020 = OB2020 + Debit column - Credit Column and so on and so forth. 

 

Below are the formulas that I have created, but I am sure there is a better way of doing this rather than the manual way. 

 

 

//separate measure for the opening balances
Actuals Opening Selected Period = 

var selectedperiod = SELECTEDVALUE(Date[Year])

Return 

switch
    (
    true(),
    selectedperiod=2019, OB2019,
    selectedperiod=2020, [Closing 2019],
    selectedperiod=2021, [Closing 2020],
    selectedperiod=2022, [Closing 2021],
    selectedperiod=2023, [Closing 2022]
)
//separate measure for the closing balances
Actuals Closing Selected Period = 

var selectedperiod = SELECTEDVALUE(Date[Year])

Return 

switch
    (
    true(),
    selectedperiod=2019, [Closing 2019],
    selectedperiod=2020, [Closing 2020],
    selectedperiod=2021, [Closing 2021],
    selectedperiod=2022, [Closing 2022],    
    selectedperiod=2023, [Closing 2023]   )

OB2019 = CALCULATE(([Credit]), Actuals[ACCDATE] = 2018) //which equals to zero because there are no transactions as of 2018. 

Closing 2019 = OB2019 + [Debit 2019] - [Credit 2019]

Closing 2020 = [Closing 2019] + [Debit 2020] - [Credit 2020]

Closing 2021 = [Closing 2020] + [Debit 2021] - [Credit 2021]

Closing 2022 = [Closing 2021] + [Debit 2022] - [Credit 2022]
//and so on and so forth.

 

These opening balance and closing balance measures will go in the below visual for each of the companies. Plus they will change based on the "Year" slicer at the top. 

feltaha_1-1665408468069.png

 

With my approach, I have to create a measure for 2019, 2020, 2021, etc. I am looking for a way to automate this. 

 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@feltaha , we have build that using running total approch

 

closing = CALCULATE(SUM(Table[debit]),filter(date,date[date] <=maxx(date,date[date]))) - CALCULATE(SUM(Table[credit]),filter(date,date[date] <=maxx(date,date[date])))

 

opening   = CALCULATE(SUM(Table[debit]),filter(date,date[date] <min(date,date[date]))) - CALCULATE(SUM(Table[credit]),filter(date,date[date] < min(date,date[date])))

 

very similar to Power BI Inventory On Hand: https://youtu.be/nKbJ9Cpb-Aw

View solution in original post

4 REPLIES 4
amitchandak
Super User
Super User

@feltaha , we have build that using running total approch

 

closing = CALCULATE(SUM(Table[debit]),filter(date,date[date] <=maxx(date,date[date]))) - CALCULATE(SUM(Table[credit]),filter(date,date[date] <=maxx(date,date[date])))

 

opening   = CALCULATE(SUM(Table[debit]),filter(date,date[date] <min(date,date[date]))) - CALCULATE(SUM(Table[credit]),filter(date,date[date] < min(date,date[date])))

 

very similar to Power BI Inventory On Hand: https://youtu.be/nKbJ9Cpb-Aw

Hi @amitchandak , 

 

Thank you for your guidance, for I was able to solve the issue using your suggested running total method. 
I got rid of the error by changing the filter expression of min (date, date[date]), but I was getting empty values. then I altered the formula a little bit using keepfilters instead of filter and now it works.

below is the code I used. 

 

//Opening balance code. 
OB2 = CALCULATE(
    [Rand Debit],
    KEEPFILTERS('Date'[Date] < MIN('Date'[Date]))
    )
     - 
     CALCULATE(
    [Rand Credit],
    KEEPFILTERS('Date'[Date] < min('Date'[Date]))
)

//Rand debit Code
Rand Debit = 
CALCULATE (
    SUMX (
        Actuals,
        IF (
            Actuals[Company] = "Z02",
            DIVIDE ( Actuals[Amount], RELATED ( 'Date'[ZAR] ) ),
            Actuals[Amount]
        )
    ),
    Actuals[Sign] = 1
)

//Rand Credit Code
Rand Credit = 
CALCULATE (
    SUMX (
        Actuals,
        IF (
            Actuals[Company] = "Z02",
            DIVIDE ( Actuals[Amount], RELATED ( 'Date'[ZAR] ) ),
            Actuals[Amount]
        )
    ),
    Actuals[Sign] = -1
)

 

//Closing balance code
Rand CB = [OB2] + [Rand Debit] -[Rand Credit]

 

 

Hi Amit, 

 

I am getting the following error when using your formula. The error I get is "The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value"

Below is the formula I used. 

Rand OB = 
CALCULATE(
	SUMX (
        Actuals,
        IF (
            Actuals[Company] = "Z02",
            DIVIDE ( Actuals[Amount], RELATED ( 'Date'[ZAR] ) ),
            Actuals[Amount]
        )
    ),
    Actuals[Sign] = 1,
FILTER('Date','Date'[Date] < MIN('Date', 'Date'[Date]))
) 

- 

CALCULATE(
SUMX (
        Actuals,
        IF (
            SELECTEDVALUE ( Actuals[Company] ) = "Z02",
            DIVIDE ( Actuals[Amount], RELATED ( 'Date'[ZAR] ) ),
            Actuals[Amount]
        )
    ),
    Actuals[Sign] = -1,
FILTER('Date','Date'[Date] < MIN('Date', 'Date'[Date]))
)

 For the closing balance, there is no error, however, I suspect that the calculation is a bit off. But will be able to better investigate after fixing the opening balance. Below is the formula for the closing balance: 

Rand CB = CALCULATE(SUMX (
        Actuals,
        IF (
            Actuals[Company] = "Z02",
            DIVIDE ( Actuals[Amount], RELATED ( 'Date'[ZAR] ) ),
            Actuals[Amount]
        )
    ),
    Actuals[Sign] = 1,
FILTER('Date','Date'[Date] <= MAXX('Date','Date'[Date]))
) 
- 
CALCULATE( SUMX (
        Actuals,
        IF (
            SELECTEDVALUE ( Actuals[Company] ) = "Z02",
            DIVIDE ( Actuals[Amount], RELATED ( 'Date'[ZAR] ) ),
            Actuals[Amount]
        )
    ),
    Actuals[Sign] = -1,
FILTER('Date','Date'[Date] <= MAXX('Date','Date'[Date]))
)

I thought I'll put some data that might help:

My data model:
feltaha_0-1665129575365.png
Sample data of the actuals table:
AccountACCDATECompanySignAmount
115010011/9/2019U07-123.43
115010011/10/2019U09-17.41
115010011/11/2019U10-173.28
115010011/12/2019U12-135.95
115010011/13/2019U21114.18
115010031/14/2019U09175.40
115010031/15/2019U15145.31
115010031/16/2019Z02143.36
115010031/17/2019U08123.87
115010041/18/2019U09162.80
115010041/19/2019U15145.51
115010041/20/2019U08153.53
115010051/21/2019U09-15.58
115010051/22/2019U13-14.84
115010051/23/2019U14-139.65
115010051/24/2019U15-199.27
115010051/25/2019Z02-121.32
115010051/26/2019U13-152.67
115010061/27/2019U08-179.46
115010061/28/2019U21161.56
115010071/29/2019U11184.15
115010071/30/2019U11165.63
115010091/31/2019U21113.85
115010102/1/2019U21134.94
115010112/2/2019Z0218.28
115010122/3/2019U09120.94
115010122/4/2019U21158.95
115010132/5/2019Z02118.27
115010132/6/2019U08193.30
115010132/7/2019U13133.46
115010142/8/2019U1512.53
115010142/9/2019U2113.34
115010142/10/2019U08134.80
115010142/11/2019U0812.41
115010152/12/2019Z02114.00
115010162/13/2019U21184.98
115010162/14/2019U15118.68
115010172/15/2019U09137.80
115010182/16/2019U151357.72
115010182/17/2019U211336.85
115010192/18/2019U131657.19
115010192/19/2019U081544.88
115010192/20/2019U091923.10
115010202/21/2019U131930.18
115010212/22/2019U151400.58
115010212/23/2019U211

762.21

 

Sample data of the 'Date' table with exchange rates:

DateZAR
1/1/20193.9
1/2/20193.6
1/3/20194.1
1/4/20194.2
1/5/20194.3
1/6/20194.4
1/7/20194.5
1/8/20194.6
1/9/20194.7
1/10/20194.8
1/11/20194.9
1/12/20194.2
1/13/20194.6
1/14/20194.7
1/15/20194.1
1/1/2020

4.2

1/2/2020

4.3

1/3/2020

4.2

 

Opening balance measure: I changed the formula a bit to get rid of the above "scalar value" error, but now the visual is returning empty cells. 

 

Rand Opening Balance = 
CALCULATE(
[Rand Debit],
FILTER('Date','Date'[Date] < MIN('Date'[Date]))
) 

- 

CALCULATE(
[Rand Credit],
FILTER('Date','Date'[Date] < MIN('Date'[Date]))
)

 

Rand Debit Dax formula:

 

Rand Debit = 
CALCULATE (
    SUMX (
        Actuals,
        IF (
            Actuals[Company] = "Z02",
            DIVIDE ( Actuals[Amount], RELATED ( 'Date'[ZAR] ) ),
            Actuals[Amount]
        )
    ),
    Actuals[Sign] = 1
)

 

The outcome matrix visual:

feltaha_0-1665486523860.png

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.