Forum Discussion

kbabu57's avatar
kbabu57
Regular Visitor
2 years ago
Solved

Cumulative Sum by 2 columns

Hi Folks and Fowmy Jihwan_Kim 

 

I have a requirement where i have to get cumulative sum by 2 columns. The below is the sample data

The thing is Final Amount is coming by Customer Number and Year 

 

YearCustomer NumberPrice AmtDiffBalance PayFinal Amount
2015123$1000-50$500$450
2016123$1000100$600-$50
2017123$1000200$200-$50
2015789$2000800$100$2700
2016789$2000700$300$3100
2017789$2000600$400$3700

 

I want to get Final Amount calculation as below:
1sr row calculation =  $1000 +(-50) - $500 = $450

2nd row calculation = $1000 + ((-50)+100) - ($500+$600) = -50

3rd row calculation = $1000 + (-50+100+200) - ($500+$600+$200) = -50

 

4th row calculation = $2000+800 - $100 = $2700

5th row calculation = $2000+(800+700) - ($100+$300) = $3100

6th row calculation = $2000 + (800+700+600) - $400 = $3700

 

 

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi kbabu57 

     

    Ashish_Mathur Good share!

     

    For your question, here is the method I provided:

     

    Here's some dummy data

     

    "Datas"

     

    You can create a measure. Group by customer number, and add up "Diff" and "Balance Pay".

    Final Amount = 
    CALCULATE (
        SUM ('Datas'[Price Amt]),
        FILTER (
            ALLEXCEPT ('Datas', 'Datas'[Customer Number]),
            'Datas'[Year] = MAX ('Datas'[Year])
        )
    )
    + CALCULATE (
        SUM ('Datas'[Diff]),
        FILTER (
            ALLEXCEPT ('Datas', 'Datas'[Customer Number]),
            'Datas'[Year] <= MAX ('Datas'[Year])
        )
    )
    - CALCULATE (
        SUM ('Datas'[Balance Pay]),
        FILTER (
            ALLEXCEPT ('Datas', 'Datas'[Customer Number]),
            'Datas'[Year] <= MAX ('Datas'[Year])
        )
    )
    

     

    Here is the result

     

    Regards,

    Nono Chen

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

     

     

     

     

     

     

7 Replies

  • kbabu57's avatar
    kbabu57
    Regular Visitor

    Hi Friends, Could you please help how to write the DAX measure for the above requirement

  • kbabu57's avatar
    kbabu57
    Regular Visitor

    Hi Ashish Ashish_Mathur 

     

    1st of all , Thank you so much for replying my thread. Sorry for delayed reply as i am trying out with any options & combinations with the above pbi file which you attached. Seems like, you created a extra table called "Date" and developed the logics to achieve the requirement. Is there any way without creating extra "date" and build the logic with the existing data ?

    I will take a step back. I have only customer numbers as below and want to achieve the requirement.Please provide your thoughts how can we get with the below data

    Customer

    Number

    Price AmtDiffBalance PayFinal Amount
    abc123$1000-50$500$450
    abc123$1000100$600-$50
    abc123$1000200$200-$50
    def567$2000800$100$2700
    def567$2000700$300$3100
    def567$2000600$400$3700


    Thanks in advance

     

      • kbabu57's avatar
        kbabu57
        Regular Visitor

        Hi Ashish Ashish_Mathur 

        Thank you. Much appreciated for your help. However, i just tried without creating extra Calendar table, with the existing data that has date column, got the required output as well 🙂


  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi kbabu57 

     

    Ashish_Mathur Good share!

     

    For your question, here is the method I provided:

     

    Here's some dummy data

     

    "Datas"

     

    You can create a measure. Group by customer number, and add up "Diff" and "Balance Pay".

    Final Amount = 
    CALCULATE (
        SUM ('Datas'[Price Amt]),
        FILTER (
            ALLEXCEPT ('Datas', 'Datas'[Customer Number]),
            'Datas'[Year] = MAX ('Datas'[Year])
        )
    )
    + CALCULATE (
        SUM ('Datas'[Diff]),
        FILTER (
            ALLEXCEPT ('Datas', 'Datas'[Customer Number]),
            'Datas'[Year] <= MAX ('Datas'[Year])
        )
    )
    - CALCULATE (
        SUM ('Datas'[Balance Pay]),
        FILTER (
            ALLEXCEPT ('Datas', 'Datas'[Customer Number]),
            'Datas'[Year] <= MAX ('Datas'[Year])
        )
    )
    

     

    Here is the result

     

    Regards,

    Nono Chen

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

     

     

     

     

     

     

    • kbabu57's avatar
      kbabu57
      Regular Visitor

      Wow.. This is even better. Thank you Nono Chen