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
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
v-xinruzhu-msft
Community Support
Community Support

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

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.