Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Running balance (difference) by groups

I'm trying to calculate running balance for quantity on hand after usage.

My sample table looks like this. Any idea on what I need to do to get the expected value?

Index 1Index 2ComponentStart DateOn handQty requiredRunning Balance (expected value)
11A1/1/20241028
22A1/2/20241026
31B1/1/2024514
42B1/2/2024513
51C1/1/202415114
62C1/2/202415113
71D1/1/20241028
81E1/1/2024514
92E1/2/2024513
103E1/3/2024512
  • Why is On hand repeated?  Shouldn't that be only listed for the first date?

     

     

    Measure:

    Running Balance = 
    var c = SELECTEDVALUE('Table'[Component])
    var i = SELECTEDVALUE('Table'[Index 2])
    var b = filter(ALLSELECTED('Table'),[Component]=c && [Index 2]<=i)
    return maxx(b,[On hand])-sumx(b,[Qty required])

2 Replies

  • Why is On hand repeated?  Shouldn't that be only listed for the first date?

     

     

    Measure:

    Running Balance = 
    var c = SELECTEDVALUE('Table'[Component])
    var i = SELECTEDVALUE('Table'[Index 2])
    var b = filter(ALLSELECTED('Table'),[Component]=c && [Index 2]<=i)
    return maxx(b,[On hand])-sumx(b,[Qty required])
  • Running Balance =
    CALCULATE(
    MAX(SampleTable[On hand]) - SUMX(
    FILTER(
    SampleTable,
    SampleTable[Component] = EARLIER(SampleTable[Component]) &&
    SampleTable[Start Date] <= EARLIER(SampleTable[Start Date])
    ),
    SampleTable[Qty required]
    )
    )