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
BotBot1
Helper III
Helper III

Daily Balance Calculation

Hi,

I am trying to get the daily closing balance of my data. I currently have dates given by 'Date'[Date] showing the working days (weekends are blank) of debits and credits being logged.

At the moment, I only have one value for the closing balance each month, which is the 'GL_Period'[FinancialPeriod] date, but I have the daily net debit/credit balance within a measure called [Daily Net Balance] to use to calculate the daily closing balance (see below). The closing balance is given in 'GL_Account'[ClosingBalance].

BotBot1_0-1678361539755.png

So what I need is for the Daily Closing Balance to be the previous financial period's closing balance and to have added all daily net balances up to the date being looked at.
E.g. For 02/03/2023: Closing Balance = 12,248,807.97 - 5,990.75 - 222,586.15 + 26,775.00 

 

Please see the below Excel for what the ideal results would be like:

BotBot1_1-1678362015996.png

Thank you.

 

1 ACCEPTED SOLUTION

Hi @BotBot1 ,

Please update the formula of measure as below and check if it can return the expected result...

DailyClosingBalance =
VAR CurrentDate =
    SELECTEDVALUE ( 'GL_Account'[glt_trdate] )
VAR CurrentPeriod =
    SELECTEDVALUE ( 'GL_Period'[id_glperiod] )
VAR PreviousPeriod =
    MAXX (
        FILTER (
            ALLSELECTED ( 'GL_Period'[id_glperiod] ),
            'GL_Period'[id_glperiod] < CurrentPeriod
        ),
        'GL_Period'[id_glperiod]
    )
VAR PreviousClosingBalance =
    CALCULATE (
        MAX ( 'GL_Account'[ClosingBalance] ),
        FILTER (
            ALLSELECTED ( 'GL_Period' ),
            'GL_Period'[id_glperiod] = PreviousPeriod
        ),
        ALL ( 'Date' )
    )
VAR PreviousDailyNetBalance =
    SUMX (
        FILTER ( ALLSELECTED ( 'GL_Account' ), 'GL_Account'[glt_trdate] <= CurrentDate ),
        'GL_Account'[Daily Net Balance]
    )
RETURN
    PreviousClosingBalance + PreviousDailyNetBalance

If the above one can't help you, please provide some raw data in your table  'GL_Account' and 'GL_Period' (exclude sensitive data) with Text format and your expected result with backend logic and special examples? By the way, is there any relationship between these two tables? If yes, please provide the related info. It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

19 REPLIES 19
tamerj1
Super User
Super User

Good morning @BotBot1 
Please try the following. Probably won't work and sure not optimum but there is a chance that it would. If not, please let me know if we can connect on Saturday (any time before 2:00pm Dubai time)

DailyClosingBalance =
VAR CurrentDate =
    SELECTEDVALUE ( 'GL_Account'[glt_trdate] )
VAR CurrentPeriod =
    SELECTEDVALUE ( 'GL_Period'[id_glperiod] )
VAR PreviousPeriod =
    MAXX (
        FILTER (
            ALLSELECTED ( 'GL_Period'[id_glperiod] ),
            'GL_Period'[id_glperiod] < CurrentPeriod
        ),
        'GL_Period'[id_glperiod]
    )
VAR PreviousClosingBalance =
    CALCULATE (
        MAX ( 'GL_Account'[ClosingBalance] ),
        'GL_Period'[id_glperiod] = PreviousPeriod,
        ALL ( 'Date' )
    )
VAR CurrentPeriodTable =
    CALCULATETABLE ( 'GL_Period', ALL ( 'Date' ) )
VAR TableOnAndBefore =
    FILTER ( CurrentPeriodTable, 'GL_Period'[glt_trdate] <= CurrentDate )
VAR PreviousDailyNetBalance =
    SUMX ( CurrentPeriodTable, 'GL_Account'[Daily Net Balance] )
RETURN
    PreviousClosingBalance + PreviousDailyNetBalance

Hi @tamerj1 ,

 

Thanks again for your help, it's really appreciated. I added SELECTEDVALUE to the 'GL_Period'[glt_trdate] in the TableOnAndBefore variable but it has returned the same values as that in the Daily Net Balance measure.

 

Unfortunately I cannot share much more information, i.e. have a call with you/sample file as the overwhelming majority of the dataset contains confidental information, but is there anything I can do to help with reworking the DAX to help you in any way? Thank you

@BotBot1 

no worries I understand. 

What results did you get without using SELECTEDVALUE? What is the reason for adding SELECTEDVALUE?

Hi @tamerj1 ,

 

The DAX says "Cannot find name [glt_trdate]" with the following message when the measure is added to the table:

BotBot1_0-1678464431650.png

@BotBot1 

Oh! My mistake. Please try

DailyClosingBalance =
VAR CurrentDate =
SELECTEDVALUE ( 'GL_Account'[glt_trdate] )
VAR CurrentPeriod =
SELECTEDVALUE ( 'GL_Period'[id_glperiod] )
VAR PreviousPeriod =
MAXX (
FILTER (
ALLSELECTED ( 'GL_Period'[id_glperiod] ),
'GL_Period'[id_glperiod] < CurrentPeriod
),
'GL_Period'[id_glperiod]
)
VAR PreviousClosingBalance =
CALCULATE (
MAX ( 'GL_Account'[ClosingBalance] ),
'GL_Period'[id_glperiod] = PreviousPeriod,
ALL ( 'Date' )
)
VAR CurrentPeriodTable =
CALCULATETABLE ( 'GL_Account', ALL ( 'Date' ) )
VAR TableOnAndBefore =
FILTER ( CurrentPeriodTable, 'GL_Account'[glt_trdate] <= CurrentDate )
VAR PreviousDailyNetBalance =
SUMX ( CurrentPeriodTable, 'GL_Account'[Daily Net Balance] )
RETURN
PreviousClosingBalance + PreviousDailyNetBalance

Hi @tamerj1 ,

 

Thank you for your continued help on this. It is now returning the sum of the daily net balances that are within the financial period.

BotBot1_0-1678699001613.png

 

@tamerj1,

 

After swapping the 'Date'[Date] out of the table for the transaction date, 'GL_Account'[glt_trdate], I actually have the Daily Net Balance returning as the DailyClosingBalance:

BotBot1_0-1678702296475.png

 

 

Hi @BotBot1 ,

Please update the formula of measure as below and check if it can return the expected result...

DailyClosingBalance =
VAR CurrentDate =
    SELECTEDVALUE ( 'GL_Account'[glt_trdate] )
VAR CurrentPeriod =
    SELECTEDVALUE ( 'GL_Period'[id_glperiod] )
VAR PreviousPeriod =
    MAXX (
        FILTER (
            ALLSELECTED ( 'GL_Period'[id_glperiod] ),
            'GL_Period'[id_glperiod] < CurrentPeriod
        ),
        'GL_Period'[id_glperiod]
    )
VAR PreviousClosingBalance =
    CALCULATE (
        MAX ( 'GL_Account'[ClosingBalance] ),
        FILTER (
            ALLSELECTED ( 'GL_Period' ),
            'GL_Period'[id_glperiod] = PreviousPeriod
        ),
        ALL ( 'Date' )
    )
VAR PreviousDailyNetBalance =
    SUMX (
        FILTER ( ALLSELECTED ( 'GL_Account' ), 'GL_Account'[glt_trdate] <= CurrentDate ),
        'GL_Account'[Daily Net Balance]
    )
RETURN
    PreviousClosingBalance + PreviousDailyNetBalance

If the above one can't help you, please provide some raw data in your table  'GL_Account' and 'GL_Period' (exclude sensitive data) with Text format and your expected result with backend logic and special examples? By the way, is there any relationship between these two tables? If yes, please provide the related info. It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
tamerj1
Super User
Super User

Hi @BotBot1 
You are sorting the dates on descending order. Are you sure this is the correct order of calculation?

Hi @tamerj1 ,

Yes I already had in the model a variable for debits/credits linked to the Date, so my Daily Net Balance was just:


Daily Net Balance =
CALCULATE(SUM('GL_Account'[glt_amt_doc]), GROUPBY('Date', 'Date'[date])).

In terms of the closing balance, this is correct also. It is the closing balance whereby it stops at the FinancialPeriod date, i.e. 12,177,354.57 is the closing balance today and whatever the closing balance is on 31st March, the closing balance will show as this value for all dates 25th February 2023 - 31st March 2023. Thanks

@BotBot1 

This should work as either calculated column or measure.

Closing Balance Final =
VAR CurrentDate =
    MAX ( 'Table'[Date] )
VAR CurrentPeriodTable =
    CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[FinancialPeriod] ) )
VAR CurrentClosingBalance =
    MINX ( CurrentPeriodTable, 'Table'[ClosingBalance] )
VAR TableOnAndBefore =
    FILTER ( CurrentPeriodTable, 'Table'[Date] <= CurrentDate )
VAR PreviousDailyNetBalance =
    SUMX ( TableOnAndBefore, 'Table'[DailyNetBalance] )
RETURN
    CurrentClosingBalance + PreviousDailyNetBalance

Hi @tamerj1,

Thanks for this.

 

I adjusted the DAX to the following (as it was not picking up the 'GL_Account'[ClosingBalance] and 'Date'[Date] in TableOnAndBefore):

 

DailyClosingBalance = 
VAR CurrentDate =
    MAX ( 'Date'[Date] )
VAR CurrentPeriodTable =
    CALCULATETABLE ( 'GL_Period', ALLEXCEPT ( 'GL_Period', 'GL_Period'[FinancialPeriod]))
VAR CurrentClosingBalance =
    MINX ( CurrentPeriodTable, SELECTEDVALUE('GL_Account'[ClosingBalance]) )
VAR TableOnAndBefore =
    FILTER ( CurrentPeriodTable, SELECTEDVALUE('Date'[Date]) <= CurrentDate )
VAR PreviousDailyNetBalance =
    SUMX ( TableOnAndBefore, [Daily Net Balance] )
RETURN
    CurrentClosingBalance + PreviousDailyNetBalance

 

However this is now returning the following:

BotBot1_0-1678373939421.png

It is doing the row's ClosingBalance + Daily Net Balance, but it needs to be the previous financial periods closing balance + the sum of the daily net balances up to the rows date, e.g. 01/03/2023: 12,248,807.97 - 5,990.75 - 222,586.15 = 12,020,231.07

@BotBot1 

What are you creating? A calculated column or a measure?

@BotBot1 

One more thing  do you really think that it's not worth mentioning that the data os coming from two different tables? Do you really think that relationship are irrelevant and they do not matter? Please provide me with the complete picture to avoid wasting more time and effort. 

@tamerj1 

 

I am trying to be as helpful as possible with my limited knowledge of DAX. I am new to this, which is why I am asking the forum. Also, I have the tables referenced in the original post.

BotBot1_0-1678376540054.png

'Date'[Date] -> 'GL_Account'[glt_trdate], which is the transaction date.

'Company'[kco] -> 'GL_Account'[kco], which is 2 companies codes, 22 and 30, referencing the 2 companies in the dataset.

'GL_BusinessUnit'[id_glelement] -> 'GL_Account'[id_glelement], which have bank codes for the different divisions of the 2 companies.

'GL_Period'[id_glperiod] -> 'GL_Account'[id_glperiod], the FinancialPeriod in yyyy-mm-dd format.

 

This would be a calculated column.

 

But please, I am trying to be helpful. If I am not being clear, just ask away as I do not know what is required. Thank you.

@BotBot1 
This should be a calculated column

DailyClosingBalance =
VAR CurrentDate = 'GL_Account'[glt_trdate]
VAR CurrentPeriod = 'GL_Account'[id_glperiod]
VAR TableOnAndBefore =
    FILTER ( 'GL_Period', 'GL_Period'[glt_trdate] <= CurrentDate )
VAR CurrentPeriodTable =
    FILTER ( TableOnAndBefore, 'GL_Account'[id_glperiod] = CurrentPeriod )
VAR PreviousDailyNetBalance =
    SUMX ( CurrentPeriodTable, 'GL_Account'[Daily Net Balance] )
VAR PreviousPeriodTable =
    TOPN (
        1,
        FILTER ( TableOnAndBefore, 'GL_Account'[id_glperiod] < CurrentPeriod ),
        'GL_Account'[id_glperiod]
    )
VAR PreviousClosingBalance =
    MAXX ( PreviousPeriodTable, 'GL_Account'[ClosingBalance] )
RETURN
    PreviousClosingBalance + PreviousDailyNetBalance

@tamerj1,

 

Thank you for your help on this. My sincere apologies, but I did not know that you cannot create calculated columns from a live connection. Would it be possible to adjust this as a measure. Sorry again. 

@BotBot1

No worries. 
I'll get back to you tomorrow morning. Meanwhile it would be great if you have a sample file. Have a good night

@BotBot1 
is [Daily Net Balance] a column or a measure? If measure please advise the dax

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.

Top Solution Authors