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
Eric
Advocate I
Advocate I

Transaction Level Running Total

I am trying to calculate running balances at a transaction level for inventory.  The measures are working on a daily level but I cannot get the "Qty Before" balances to calculate correctly at a transaction level.  For example, in the table below note that the "Qty Before" on 10/3/17 shows the same number for each separate transaction. The number is correct for the end of the day but it is not correct for each separate transaction.  I need the "Qty Before" balance to iterate correctly so the ending balances will roll forward correctly on a transaction by transaction basis.

 

Here is the DAX code for my measures:

 

Quantity_Before = [Quantity_EndBal] - [Quantity_Reverse]
 
Quantity_EndBal =
CALCULATE (
   SUM ( TMPROD[PDINVENTRY] ),
   FILTER ( TMPROD, TMPROD[PDNUMBER] = MAX ( TMHIST[PDHSTPRDNO] ) )
   )  [Working Correctly]
 
Quantity_Reverse =
CALCULATE (
   SUM ( 'TMHIST'[Movement] ),
   ALLSELECTED ( ),
   ALL ( TMHIST[Date] )
   )
   - CALCULATE (
      SUM ( 'TMHIST'[Movement] ),
      ALLSELECTED ( ),
      FILTER (
         ALL ( TMHIST[Date] ),
         TMHIST[Date] < MAX ( TMHIST[Date] )
      )
)
[Working correctly on a daily basis but not on a transaction basis] 

 

This should be a simple formula but I can't get it to work after numerous different attempts. When I try substituting Ref No in place of Date in the FILTER(ALL( function Power BI runs out of memory and errors out.  For reference, the fact table has roughly 2 million records in it.

 

 

 

Inv Bal Example.JPG

6 REPLIES 6
Icey
Community Support
Community Support

Hi @Eric ,

I reproduced your question, but did not get any error. Is the result below what you want?

Transaction Level Running Total.png

For your problem - ‘Power BI runs out of memory and errors out’, you can do something like below to improve performance.

  1. For your computer, close other non-essential programs.
  2. For your data model, you can refer to this article to optimize your model.
  3. For DAX, there are more different records in [Ref No] and ‘Filter’ need more time and memory to complete. You can use DAX studio to measure DAX performance and refer to this article to optimizing DAX

 

Best Regards,

Icey Zhang

 

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

Thanks for the reply, Icey.  Let me be a bit more specific about the error I'm trying to fix.  See the table below for more info:

 

Date            Product           Ref No          Qty Before        Movement       Qty After

10/02/17       03885             7065503           1455                    +4                 1459

10/03/17       03885             7071406           1459                     -2                 1454 

10/03/17       03885             7071433           1459                     -2                 1454 

10/03/17       03885             7071908           1459                     -1                 1454  

 

My main problem is with the calculation of the Qty Before (in red above).  Here is the desired outcome:

 

Date            Product           Ref No          Qty Before        Movement       Qty After

10/02/17       03885             7065503           1455                    +4                 1459

10/03/17       03885             7071406           1459                     -2                 1457 

10/03/17       03885             7071433           1457                     -2                 1455 

10/03/17       03885             7071908           1455                     -1                 1454  

 

And here is he formula for the Qty Before measure:  

 

Quantity_Reverse =
CALCULATE (
   SUM ( 'TMHIST'[Movement] ),
   ALLSELECTED ( ),
   ALL ( TMHIST[Date] )
   )
   - CALCULATE (
      SUM ( 'TMHIST'[Movement] ),
      ALLSELECTED ( ),
      FILTER (
         ALL ( TMHIST[Date] ),
         TMHIST[Date] < MAX ( TMHIST[Date] )
      )
)
 
It should be a simple fix but I just can see what needs to change in my formula to make the iteration work properly.
 
Any help will be appreciated.
 
Thanks,
 
Eric
 
Icey
Community Support
Community Support

Hi @Eric ,

I reproduce your question, and I think the results are correct. Can you share your PBIX to me if possible? Or a similar one. Then I can help you better.

Transaction Level Running Total.png

Quantity_Reverse 2 =
CALCULATE ( SUM ( 'TMHIST'[Movement] ), ALLSELECTED (), ALL ( TMHIST[Ref No] ) )
    - CALCULATE (
        SUM ( 'TMHIST'[Movement] ),
        ALLSELECTED (),
        FILTER ( ALL ( TMHIST[Ref No] ), TMHIST[Ref No] < MAX ( TMHIST[Ref No] ) )
    )

Best Regards,

Icey

 

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

 

Thanks for you help.  In the end, I was able to solve the problem by coming at it from a different direction and didn't have to use this formula after all.

Greg_Deckler
Community Champion
Community Champion

See if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Greg, thanks for the response.  I haven’t tried the CALCULATETABLE function, I will give that some thought.  But, anytime we try to use SUMX(FILTER( we run out of memory even with a table that only has 2 million records.

 

Thanks,

 

Eric

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.

Top Solution Authors