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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
GokilaRaviraj
Helper II
Helper II

Calculate closing balance using opening balance and net cashflow

Hi ,

I have given below sample input tables

This is NetCashFlow table which is a calculated table

CompanyMonthValue
AOct-2410
ANov-2410
ADec-2410
AJan-2510
AFeb-2510
AMar-2510
AApr-2510
AMay-2510
AJun-2510
AJul-2510
AAug-2510
ASep-2510
AOct-2510
ANov-2510
ADec-2510
BOct-2410
BNov-2410
BDec-2410
BJan-2510
BFeb-2510
BMar-2510
BApr-2510
BMay-2510
BJun-2510
BJul-2510
BAug-2510
BSep-2510
BOct-2510
BNov-2510
BDec-2510

This is Opening balance table

CompanyMonthValue
AOct-2410
ANov-240
ADec-240
AJan-250
AFeb-250
AMar-250
AApr-250
AMay-250
AJun-250
AJul-250
AAug-250
ASep-250
AOct-250
ANov-250
ADec-250
BOct-2410
BNov-240
BDec-240
BJan-250
BFeb-250
BMar-250
BApr-250
BMay-250
BJun-250
BJul-250
BAug-250
BSep-250
BOct-250
BNov-250
BDec-250

In this table months are dynamically getting populated using my calendar table, wherein it has months from Current month + remaining months of the current year + next year all the months.

 

In my original table, I have multiple company names in NetcashFlow and openingbalance tables.

Closing balance report should include companies from both the tables, for current month, it has value in net cashflow and opening balance tables, we can sum it up directly. CB = OB + NetCashFlow.

 

from next month onwards, Nov CB= OCT CB + Nov Netcashflow

Dec CB= Nov CB + Dec Netcashflow , This logic continues until the last month.

Sample output should contain company, Monthyear, closing balance

Could anyone suggest a DAX code for this ? Thanks in Advance.

1 ACCEPTED SOLUTION

Hi @GokilaRaviraj

 

Your current output seems confusing to me 🤔. Specifically, I don’t understand how Company B’s opening balance of 10 in October 2024 results in a cumulative (closing) balance of 170 for the same period, given that the Net Cash Flow for the month is only 10. Logically, the closing balance should be 20 for Company B, based on the company-level dimension.

To address this, I recommend creating two dimension tables—one for the calendar and another for the company—and establishing relationships between these dimensions and your opening balance and Net Cash Flow fact tables, as outlined below:

 

DataNinja777_0-1730008799490.png

 

I would then create a cumulative measure for the closing balance using both the NetCashFlow and Opening Balance, as shown below:

 

Closing Balance = 
VAR CurrentDate = MAX('Calendar'[Date])
RETURN
CALCULATE(
    SUM(NetCashFlow[Value])+sum('Opening balance'[Value]),
    FILTER(
        ALL('Calendar'), 
        'Calendar'[Date] <= CurrentDate
    )
)

 

The resulting output looks like below which differs from your expected output:

DataNinja777_1-1730008898624.png

 

 

From an accounting perspective, the cumulative cash balance is calculated over time. However, your output appears to accumulate across the row index, which may not reflect standard accounting practices.

 

I have attached an example pbix file. 

 

Best regards,

View solution in original post

9 REPLIES 9
GokilaRaviraj
Helper II
Helper II

Hi Guys @lbendlin @v-mengmli-msft 

 

I have found a DAX code for this. But there is some issue here. This code works perfectly to calculate closing balance for current month. It is trying to fetch current month closing balance to calculate Nov , dec and all other month's. It is failing to add CurrentClosingBalance with NextMonthNetCashFlow. 

There is some issue in Return statement. It is checking if month is oct, then returning current month closing balance else it is returning

CurrentClosingBalance + NextMonthNetCashFlow this. This calculation 

has to happen iteratively till the end of all other months

I am providing the code which i have written. PLease suggest me a code if you find a solution for this issue, It would be a great help. 

ClosingBalanceMeasure =
VAR CurrentCompany = SELECTEDVALUE(Company[Company], "No Company Selected")  -- Get the current company
VAR CurrentMonth =
    VAR TodayDate = TODAY()                                                                                
    VAR CurrentMonthText = FORMAT(TodayDate, "MMM-YY") -- Get the current month in text format
    RETURN
        CALCULATE(
            MAX('Calendar'[MonthYear]),
            FILTER(
                'Calendar',
                'Calendar'[MonthYear] = CurrentMonthText
            )
        )
-- Convert the current month to a date format
VAR CurrentMonthAsDate = DATEVALUE("01-" & LEFT(CurrentMonth, 3) & "-20" & RIGHT(CurrentMonth, 2))

-- Calculate the opening balance for the current month
VAR OpeningBalance =
    CALCULATE(
        SUM(OpeningCurrentBalance[Value]),
        FILTER(
            OpeningCurrentBalance,
            OpeningCurrentBalance[Company] = CurrentCompany
        )
    )

-- Calculate the current month's net cash flow
VAR CurrentNetCashFlow =
    CALCULATE(
        SUM(NetCashFlow[Value]),
        FILTER(
            NetCashFlow,
            NetCashFlow[Company] = CurrentCompany &&
            NetCashFlow[Month] = CurrentMonth
        )
    )

-- Calculate the current month's closing balance
VAR CurrentClosingBalance = OpeningBalance + CurrentNetCashFlow

-- Handle previous month's closing balance logic
VAR PreviousMonthAsDate = EOMONTH(CurrentMonthAsDate, -1)  -- Get the last day of the previous month
VAR PreviousMonthText = FORMAT(PreviousMonthAsDate, "MMM-YY")  -- Convert to text format (e.g., "Sep-24")

-- Calculate previous month's closing balance if it exists
VAR PreviousClosingBalance =
    CALCULATE(
        SUM(ClosingEstimateBalance[Value]),
        FILTER(
            ClosingEstimateBalance,
            ClosingEstimateBalance[Company] = CurrentCompany &&
            ClosingEstimateBalance[Month] = PreviousMonthText
        )
    )

-- Calculate next month's net cash flow
VAR NextMonthAsDate = EOMONTH(CurrentMonthAsDate, 0) + 1  -- Get the first day of the next month
VAR NextMonthText = FORMAT(NextMonthAsDate, "MMM-YY")  -- Convert to text format (e.g., "Nov-24")
VAR NextMonthNetCashFlow =
    CALCULATE(
        SUM(NetCashFlow[Value]),
        FILTER(
            NetCashFlow,
            NetCashFlow[Company] = CurrentCompany &&
            NetCashFlow[Month] = NextMonthText
        )
    )

-- Calculate the final closing balance
RETURN
    IF(
        ISBLANK(PreviousClosingBalance),  -- If there is no previous closing balance (first month case)
        CurrentClosingBalance,  -- Use the current closing balance
        CurrentClosingBalance + NextMonthNetCashFlow  -- For other months, calculate using current closing balance + next month's net cash flow
    )
 
It is repeating the same value calculated for current closing balance to all other months. 
Thank you in Advance.

 

Hi Guys,

Is anyone found any solution for this issue ? I am trying out many solutions but nothing works out. 

 

v-mengmli-msft
Community Support
Community Support

Hi @GokilaRaviraj ,

 

Based on the data you provided, I think you need to create custom columns. Custom columns are used to determine the values used for accumulation. Here are my tests and pbix files for your reference.

 

  • Your date format can not be recognized, I converted it to "YYYYMM" format in YearMonth column. This format makes it easy to compare time.
  • The NC column is used to get the NetCashFlow of the relevant date.
  • The CompanyID column is used to check Company order.
  • The NC_2 column is used to check if date is current date.
  • The CB_1 column is  the cumulative closing balance value obtained by the company.
  • The CB_2 column is the final cumulative result.

vmengmlimsft_0-1729758336300.png

 

 

 

 

Best regards,

Mengmeng Li

Hi @v-mengmli-msft 

Thanks for your solution.

In my original input table, I have many company names. 

How do I write a code for Comapny ID ? 

CompanyID = SWITCH('Opening balance'[Company],"A",1,"B",2
You have given this for A, B companies. 
Is there any solution for this company id ? If I could change that, This will work I believe.

Hi @GokilaRaviraj ,

 

The reason for sorting companies is that you expect the CB value for October of company B in the result to be 170 instead of 180. I treat it as monthly cumulative value determined by both the date and the company, only companies (A) whose date is the current month and whose order is 1 use this rule: CB = OB + NetCashFlow.

 

If I understand correctly, you should have a table for company sorting, if not, you need to create a company sorting table, you can use DISTINCT to get all the companies and then create a row sequence using the SWITCH function.

 

 

 

Best regards,

Mengmeng Li

lbendlin
Super User
Super User


Please show the expected outcome based on the sample data you provided.

Hi @lbendlin 

 

Here is the expected ouput

Closing Balance  
CompanyMonthValue
AOct-2420
ANov-2430
ADec-2440
AJan-2550
AFeb-2560
AMar-2570
AApr-2580
AMay-2590
AJun-25100
AJul-25110
AAug-25120
ASep-25130
AOct-25140
ANov-25150
ADec-25160
BOct-24170
BNov-24180
BDec-24190
BJan-25200
BFeb-25210
BMar-25220
BApr-25230
BMay-25240
BJun-25250
BJul-25260
BAug-25270
BSep-25280
BOct-25290
BNov-25300
BDec-25310

Hi @GokilaRaviraj

 

Your current output seems confusing to me 🤔. Specifically, I don’t understand how Company B’s opening balance of 10 in October 2024 results in a cumulative (closing) balance of 170 for the same period, given that the Net Cash Flow for the month is only 10. Logically, the closing balance should be 20 for Company B, based on the company-level dimension.

To address this, I recommend creating two dimension tables—one for the calendar and another for the company—and establishing relationships between these dimensions and your opening balance and Net Cash Flow fact tables, as outlined below:

 

DataNinja777_0-1730008799490.png

 

I would then create a cumulative measure for the closing balance using both the NetCashFlow and Opening Balance, as shown below:

 

Closing Balance = 
VAR CurrentDate = MAX('Calendar'[Date])
RETURN
CALCULATE(
    SUM(NetCashFlow[Value])+sum('Opening balance'[Value]),
    FILTER(
        ALL('Calendar'), 
        'Calendar'[Date] <= CurrentDate
    )
)

 

The resulting output looks like below which differs from your expected output:

DataNinja777_1-1730008898624.png

 

 

From an accounting perspective, the cumulative cash balance is calculated over time. However, your output appears to accumulate across the row index, which may not reflect standard accounting practices.

 

I have attached an example pbix file. 

 

Best regards,

Hi @DataNinja777 

 

Thanks for your solution. It worked really cool !! 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.