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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Muazam
New Member

What would be the DAX code for the discussed scenario?

Hey there, I'm having trouble calculate correct closing balance. Need correct DAX for the psudocode that seems appropriate for the scenario depicted in the attached JPEG.

 

if No. of Transactions on Min (date) <=1 

   New Closing Balance = Closing Balance,

else

  find sum of all withdrawals on Min (date), find sum of all deposits on Min (date),

  if sum of all deposits on Min (date) >= sum of all withdrawals on Min (date)

     New Closing Balance = Max (Closing Balance) on Min (date)

  else

     New Closing Balance = Min (Closing Balance) on Min (date)

     

I hope the corresponding DAX code will help get the correct closing balance on Min (date).

 

Once we have new closing balance on Min (date), the new closing balance for subsequent dates would be as under:

   New closing Balance Today = New Closing Balance Yesterday + Sum of all deposits today - sum of all withdrawals today...Challenge_Closing_Balance.jpg 

2 REPLIES 2
Anonymous
Not applicable

Hi @Muazam 

You can refer to the following example.

You can create a new column in table

New Closing Balance = 
var _mindate=FILTER('Table',[Date]=MIN([Date]))
var _tilltoday=FILTER('Table',[Date]<=EARLIER([Date])&&[Date]<>MIN([Date]))
var _sumwith=SUMX(_mindate,[Withdrawal])
var _sumdepo=SUMX(_mindate,[Deposit])
var _if=IF(CALCULATE(DISTINCTCOUNT('Table'[Transactions]),'Table'[Date]=MIN([Date]))=1,[Closing Balance],IF(_sumdepo>=_sumwith,MAXX(_mindate,[Closing Balance]),MINX(_mindate,[Closing Balance])))
return _if+SUMX(_tilltoday,[Deposit])-SUMX(_tilltoday,[Withdrawal])

vxinruzhumsft_0-1675057478773.png

Best Regards,

Xinru Zhu

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

 

amitchandak
Super User
Super User

@Muazam , to me it very similar to inventory calculation

 

Inventory / OnHand
CALCULATE(firstnonblankvalue('Date'[Month]),sum(Table[Intial Inventory]),all('Date')) + CALCULATE(SUM(Table[Ordered]),filter(date,date[date] <=maxx(date,date[date]))) - CALCULATE(SUM(Table[Sold]),filter(date,date[date] <=maxx(date,date[date])))

 

You can use Min for start of period

 

You might not need - CALCULATE(firstnonblankvalue('Date'[Month]),sum(Table[Intial Inventory]),all('Date'))

 

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

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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