如何在Power BI中的多个列上创建累积总和?

时间:2017-08-08 17:13:57

标签: powerbi dax cumulative-sum

我在Power BI桌面上有一张桌子,我试图创建累积总和。这是创建此表的SQL:

create table #sample
(
DOS_Month               varchar(6)
,CRD_Month              varchar(6)
,Credit_Received_Date   date
,DaysElapsed            varchar(20)
,Last_Mo_Collections    decimal(13,0)
)
insert #sample values('201707','201708','8/2/2017','01',11470)
insert #sample values('201707','201708','8/3/2017','01',2821)
insert #sample values('201707','201708','8/4/2017','01',1361)
insert #sample values('201707','201708','8/7/2017','01',9040)
insert #sample values('201707','201708','8/3/2017','02',2397)
insert #sample values('201707','201708','8/4/2017','02',5101)
insert #sample values('201707','201708','8/7/2017','02',2256)
insert #sample values('201707','',NULL,'Complete Month',1041764)

添加累积和列后,此表应如下所示:

DOS_Month   CRD_Month   Credit_Received_Date    DaysElapsed Last_Mo_Collections Cumulative
$201,707    201708  8/2/2017    1   $11,470 $11,470
$201,707    201708  8/3/2017    1   $2,821  $14,291
$201,707    201708  8/4/2017    1   $1,361  $15,652
$201,707    201708  8/7/2017    1   $9,040  $24,692
$201,707    201708  8/3/2017    2   $2,397  $27,089
$201,707    201708  8/4/2017    2   $5,101  $32,190
$201,707    201708  8/7/2017    2   $2,256  $34,446
$201,707            Complete Month  $1,041,764  $1,076,210

如何使用DAX或Quick Measure执行此操作?此外,由于DOS_Month和CRD_Month不是时间数据类型,因此我无法使用时间函数来对它们求和。

1 个答案:

答案 0 :(得分:1)

此公式应该为您提供所需的累积总和列。

Cumulative = CALCULATE(
    SUM(Table1[Last_Mo_Collections]), 
    FILTER(
        Table1, 
        Table1[DOS_Month] = EARLIER(Table1[DOS_Month]) &&
        (
            (
                Table1[DaysElapsed] = EARLIER(Table1[DaysElapsed]) && 
                Table1[Credit_Received_Date] <= EARLIER(Table1[Credit_Received_Date])
            ) || (
                Table1[DaysElapsed] < EARLIER(Table1[DaysElapsed])
            )
        )
    )
)

Results

EARLIER功能是关键(在我看来可怕的名字)。从文档中,函数......

  

返回上述列的外部评估过程中指定列的当前值。