How to calculate new column value from two columns and its sum dynamic value in power bi?

1k views Asked by At

I'm trying to add two columns with static value RM500 it gives us a new column called totalSA

My Table is:

Transaction Date    Pol No.     Policy Status   Incremental SA  Referral SA
16-Sep-20          P00061353    Inforce             RM100         0
17-Sep-20          P00061353    Inforce             RM100         0
18-Sep-20          P00061353    Inforce             RM100         0
18-Sep-20          P00061353    Inforce             RM100        RM200
19-Sep-20          P00061353    Inforce             RM100         0
18-Sep-20          P00061354    Inforce             RM100         0
18-Sep-20          P00061354    Inforce             RM100        RM200
19-Sep-20          P00061354    Inforce             RM100         0

Expected Table is:

Transaction Date    Pol No.     Policy Status   Incremental SA  Referral SA  Total SA
16-Sep-20          P00061353    Inforce             RM100         0           RM600
17-Sep-20          P00061353    Inforce             RM100         0           RM700
18-Sep-20          P00061353    Inforce             RM100         0           RM800
18-Sep-20          P00061353    Inforce             RM100        RM200        RM1100
19-Sep-20          P00061353    Inforce             RM100         0           RM1200
18-Sep-20          P00061354    Inforce             RM100         0           RM600
18-Sep-20          P00061354    Inforce             RM100        RM200        RM900
19-Sep-20          P00061354    Inforce             RM100         0           RM1000

Logic: if Policy no is the same then we need to increment.

(Initial value/previous toal)+Incremental SA + Referral SA   Total SA
      RM500                  +        RM100  +0              RM600 (New Policy)
      RM600                  +        RM100  +0              RM700
      RM700                  +        RM100  +0              RM800
      RM800                  +        RM100  +RM200          RM1100
      RM1100                 +        RM100  +0              RM1200
      RM500                  +        RM100  +0              RM600 (New Policy)
      RM600                  +        RM100  +RM200          RM900
      RM900                  +        RM100  +0              RM1000

I have tried the below code doesn't work.

Total_SA = 
VAR insSA = CONVERT(SUBSTITUTE([Incremental SA], "RM", ""),INTEGER)
VAR refSA = CONVERT(SUBSTITUTE([Referral SA],"RM",""),INTEGER)


VAR PreviousRow =
    TOPN (
        1,
        FILTER (
            'Table',
            'Table'[Index] < EARLIER('Table'[Index]) && 
             'Table'[Pol No.] = EARLIER ( 'Table'[Pol No.])
        ),
        [Index], DESC
    )
 
VAR PreviousValue =  MAXX( PreviousRow,CONVERT(SUBSTITUTE('Table'[Referral SA], "RM", ""),INTEGER)  )  

RETURN  PreviousValue +    CONVERT(SUBSTITUTE('Table'[Incremental SA], "RM", ""),INTEGER) + 500
2

There are 2 answers

5
mkRabbani On BEST ANSWER

As you said you have an Index column in the table, let your data looks like below-

enter image description here

Now, create this following Measure in the table-

total_sa = 

var initial_amount = 500
var current_index = MIN('Table'[Index])

var cumulative_inc_sa = 
CALCULATE(
    SUM('Table'[Incremental SA]),
    FILTER(
        ALL('Table'),
        'Table'[Index] <= current_index
    )
) + 0

var cumulative_ref_sa = 
CALCULATE(
    SUM('Table'[Referral SA]),
    FILTER(
        ALL('Table'),
        'Table'[Index] <= current_index
    )
) + 0

RETURN initial_amount + cumulative_inc_sa + cumulative_ref_sa

Here is your final output-

enter image description here

2
Joao Leal On

From your example and assuming that all transactions are sorted by index, that looks like a simple running total:

Total_SA =
VAR currentIndex = SELECTEDVALUE('Table'[Index])
RETURN
 CONCATENATE("RM",
    CONVERT(
       CALCULATE(SUM(CONVERT(SUBSTITUTE('Table'[Incremental SA], "RM", ""),INTEGER)) 
               + SUM(CONVERT(SUBSTITUTE('Table'[Referral SA], "RM", ""),INTEGER)),
         ALL('Table'),
         'Table'[Index] <= currentIndex
       ) + 500, STRING))

I would recommend that you create 2 additional columns with the int value of the SAs, as it would make the code a bit more readable.