如何计算两列的新列值及其在power bi中的总和动态值?

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

我正在尝试添加两个静态值为 RM500 的列,它为我们提供了一个名为 totalSA 的新列

我的Table是:

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

预计Table是:

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

逻辑:如果策略号相同,则我们需要递增。

(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

我试过下面的代码不起作用。

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

根据您的示例并假设所有交易都按索引排序,这看起来像一个简单的 运行 总计:

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))

我建议您使用 SA 的 int 值创建 2 个额外的列,因为这会使代码更具可读性。

正如您所说,table 中有一个 Index 列,让您的数据如下所示-

现在,在 table-

中创建以下 Measure
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

这是你的最终输出-