如何在 Power BI 中创建多列的累计和?
How to create a cumulative sum over multiple columns in Power BI?
我在 Power BI 桌面中有一个 table,我正试图在其中创建累计和。这是 SQL 创建这个 table:
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)
添加累计总和列后,table 应如下所示:
DOS_Month CRD_Month Credit_Received_Date DaysElapsed Last_Mo_Collections Cumulative
1,707 201708 8/2/2017 1 ,470 ,470
1,707 201708 8/3/2017 1 ,821 ,291
1,707 201708 8/4/2017 1 ,361 ,652
1,707 201708 8/7/2017 1 ,040 ,692
1,707 201708 8/3/2017 2 ,397 ,089
1,707 201708 8/4/2017 2 ,101 ,190
1,707 201708 8/7/2017 2 ,256 ,446
1,707 Complete Month ,041,764 ,076,210
如何使用 DAX 或快速测量来执行此操作?另外,我不能使用时间函数来对这些求和,因为 DOS_Month 和 CRD_Month 不是时间数据类型。
此公式应为您提供所需的累计总和列。
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])
)
)
)
)
EARLIER
函数是关键(在我看来这个名字太可怕了)。从文档中,函数...
Returns the current value of the specified column in an outer evaluation pass of the mentioned column.
我在 Power BI 桌面中有一个 table,我正试图在其中创建累计和。这是 SQL 创建这个 table:
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)
添加累计总和列后,table 应如下所示:
DOS_Month CRD_Month Credit_Received_Date DaysElapsed Last_Mo_Collections Cumulative
1,707 201708 8/2/2017 1 ,470 ,470
1,707 201708 8/3/2017 1 ,821 ,291
1,707 201708 8/4/2017 1 ,361 ,652
1,707 201708 8/7/2017 1 ,040 ,692
1,707 201708 8/3/2017 2 ,397 ,089
1,707 201708 8/4/2017 2 ,101 ,190
1,707 201708 8/7/2017 2 ,256 ,446
1,707 Complete Month ,041,764 ,076,210
如何使用 DAX 或快速测量来执行此操作?另外,我不能使用时间函数来对这些求和,因为 DOS_Month 和 CRD_Month 不是时间数据类型。
此公式应为您提供所需的累计总和列。
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])
)
)
)
)
EARLIER
函数是关键(在我看来这个名字太可怕了)。从文档中,函数...
Returns the current value of the specified column in an outer evaluation pass of the mentioned column.