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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Meghanshu
Frequent Visitor

DAX Query for opening balance

Hi All, I have an inventory dataset having daily transactions and would like to calculate opening and closing balances.
Snapshot of inventory data is given below.
 
I have calculated the closing balance as per the below formula and i have separate "Date table" which has a relation with Date field from the invetory data
Closing  = CALCULATE(SUM(Table[Inventory Balance]),
FILTER(ALL('Date'[Date]), 'Date'[Date] <= MAX('Date'[Date])))
 
   Calculated using FromulaNeed calculation for Opening
DateItemInventory BalanceClosingOpening
Wednesday, January 1, 2020Laptop1  
Thursday, January 2, 2020Laptop3  
Friday, January 3, 2020Laptop-2  
Saturday, January 4, 2020Laptop35 
Saturday, February 1, 2020Laptop3 5
Sunday, February 2, 2020Laptop-2  
Monday, February 3, 2020Laptop4  
Tuesday, February 4, 2020Laptop-28 (Sum from January) 
Sunday, March 1, 2020Laptop5 8
Monday, March 2, 2020Laptop-1  
Tuesday, March 3, 2020Laptop-1  
Wednesday, March 4, 2020Laptop2  
Thursday, March 5, 2020Laptop-112 
 

 

Thanks in Advance !

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Meghanshu , the opening should be 1 less?

 

Opening= CALCULATE(SUM(Table[Inventory Balance]),
FILTER(ALL('Date'[Date]), 'Date'[Date] < MAX('Date'[Date])))

View solution in original post

6 REPLIES 6
amitchandak
Super User
Super User

@Meghanshu , the opening should be 1 less?

 

Opening= CALCULATE(SUM(Table[Inventory Balance]),
FILTER(ALL('Date'[Date]), 'Date'[Date] < MAX('Date'[Date])))

Hi @amitchandak  

Query works perfectly fine when I change max value to min value it give me first opening transactional value of the month.

 

 

Thanks !!

Opening of any month is equal to closing of my previous month.

Hi @Meghanshu,

 

You can try the following calculated columns

Closing Balance = 
    var curDate = 'Table'[Date]
    var _item = 'Table'[Item]
    var invMonth =  CALCULATE(max('Date'[year-month]), FILTER('Date', 'Date'[Date] = curDate))
    var maxInvDate = CALCULATE(MAX('Date'[Date]), FILTER(ALL('Date'), 'Date'[year-month] =invMonth), FILTER('Table', 'Table'[Inventory Balance]<> 0 && 'Table'[Item] = _item))
return 
    if('Table'[Date] <> maxInvDate, BLANK(), CALCULATE(sum('Table'[Inventory Balance]), FILTER(ALL('Table'), 'Table'[Date]<=maxInvDate && 'Table'[Item] = _item)))

 

Opening Balance = 
    var curDate = 'Table'[Date]
    var _item = 'Table'[Item]
    var invMonth =  CALCULATE(max('Date'[year-month]), FILTER('Date', 'Date'[Date] = curDate))
    var maxInvDate = CALCULATE(min('Date'[Date]), FILTER(ALL('Date'), 'Date'[year-month] =invMonth), FILTER('Table', 'Table'[Inventory Balance]<> 0 && 'Table'[Item] = _item))
return 
    if('Table'[Date] <> maxInvDate, BLANK(), CALCULATE(sum('Table'[Inventory Balance]), FILTER(ALL('Table'), 'Table'[Date]<maxInvDate && 'Table'[Item] = _item)))

 

richbenmintz_0-1596210265770.png

 

One thing to note is that you will probably want to create measures that supress the total as the values of these calc columns is not additive

 

Hope this Helps,

 

Richard 


Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

 



I hope this helps,
Richard

Did I answer your question? Mark my post as a solution! Kudos Appreciated!

Proud to be a Super User!


Hi @Meghanshu,

Question 1:

Something like below should work

 

 

_opening balance = 
    SWITCH(TRUE(), 
        ISBLANK(SUM('Table'[Inventory Balance])), BLANK(),
        HASONEVALUE('Date'[year-month]), CALCULATE(MIN('Table'[Opening Balance]), FILTER(ALL('Date'), 'Date'[year-month] = MIN('Date'[year-month]))),
        HASONEVALUE('Date'[Date]), MIN([Opening Balance]), 
    BLANK())

 

 

richbenmintz_0-1596215189245.png

Question 2:

Depends on How you want to deal this balances, the formula currently will generate a balance per item

 

Thanks,


Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

 

 



I hope this helps,
Richard

Did I answer your question? Mark my post as a solution! Kudos Appreciated!

Proud to be a Super User!


Hi @richbenmintz ,

In the example i have mentioned a single category (Laptop) what if i have multiple category in Items.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.