如何在矩阵Power BI中计算运行总计

时间:2019-06-20 16:52:17

标签: powerbi dax powerbi-desktop

在矩阵中,行MM-YY来自dim_Calendar表。 列是LossMMYY表中的fact_Data

如何获取LossMMYY列中的累计总数,并可能填补空白?

enter image description here

关系:

enter image description here

应该是这样的:

enter image description here

我尝试过:

RunningTotal= CALCULATE(
                    PolicyNumberCount,
                    FILTER(
                        ALL(fact_Data[LossDate]),fact_Data[LossDate]<= MAX(dim_Date[Date].[Date])

但不幸的是,这不起作用。

由于某种原因,它仍然无法正常工作: .pbix文件:

https://www.dropbox.com/s/267m1r3rf98g86e/PRC%20Actuarial%20Report%20-%20Test.pbix?dl=0

                      )

2 个答案:

答案 0 :(得分:1)

enter image description here

您可以使用两个新表和以下公式来完成此操作:

Count of x = 
VAR eDate = SELECTEDVALUE(EffectiveCalendar[MM-YY]) 
VAR lDate = MAX('LossCalendar'[Date]) 
RETURN 
CALCULATE(COUNT(fact_Data[PolicyNumberCount])
, FILTER(fact_Data, fact_Data[EffectiveMMYY] = eDate && 'fact_Data'[LossDate].[Date] <= lDate))

新表1:

EffectiveCalendar = 
ADDCOLUMNS (
CALENDAR (MIN(fact_Data[EffectiveDate]),MAX(fact_Data[EffectiveDate])),
"DateAsInteger", FORMAT ( [Date], "YYYYMMDD" ),
"MonthAsInteger", FORMAT ( [Date], "YYYYMM" ),
"Year", YEAR ( [Date] ),
"Month Number", month([Date]),
"Year/Month", FORMAT ( [Date], "YYYY/MM" ),
"YearMonthShort", FORMAT ( [Date], "YYYY/mmm" ),
"MM-YY", FORMAT ( [Date], "MMM-YY" ),
"Month", FORMAT ( [Date], "mmm" ),
"MonthNameLong", FORMAT ( [Date], "mmmm" ),
"DayOfWeekNumber", WEEKDAY ( [Date] ),
"DayOfWeek", FORMAT ( [Date], "dddd" ),
"DayOfWeekShort", FORMAT ( [Date], "dddd" ),
"Quarter", "Q" & FORMAT ( [Date], "Q" ),
"YearQuarter", FORMAT ( [Date], "YYYY" ) & "/Q" & FORMAT ( [Date], "Q" )
)

新日历2:

LossCalendar = 
ADDCOLUMNS (
CALENDAR (MIN(fact_Data[LossDate]),MAX(fact_Data[LossDate])),
"DateAsInteger", FORMAT ( [Date], "YYYYMMDD" ),
"MonthAsInteger", FORMAT ( [Date], "YYYYMM" ),
"Year", YEAR ( [Date] ),
"Month Number", month([Date]),
"Year/Month", FORMAT ( [Date], "YYYY/MM" ),
"YearMonthShort", FORMAT ( [Date], "YYYY/mmm" ),
"MM-YY", FORMAT ( [Date], "MMM-YY" ),
"Month", FORMAT ( [Date], "mmm" ),
"MonthNameLong", FORMAT ( [Date], "mmmm" ),
"DayOfWeekNumber", WEEKDAY ( [Date] ),
"DayOfWeek", FORMAT ( [Date], "dddd" ),
"DayOfWeekShort", FORMAT ( [Date], "dddd" ),
"Quarter", "Q" & FORMAT ( [Date], "Q" ),
"YearQuarter", FORMAT ( [Date], "YYYY" ) & "/Q" & FORMAT ( [Date], "Q" )
)

答案 1 :(得分:1)

首先使用以下DAX创建另一个表:

MMYY = 
SUMMARIZECOLUMNS (
    fact_Data[LossMMYY],
    "Index Column",
    VAR YearNumber =
        RIGHT ( VALUES ( fact_Data[LossMMYY] ), 2 ) + 2000
    VAR CurrentMonthName =
        LEFT ( VALUES ( fact_Data[LossMMYY] ), 3 )
    RETURN
        YearNumber
            & SWITCH (
                CurrentMonthName,
                "JAN", "01",
                "FEB", "02",
                "MAR", "03",
                "APR", "04",
                "MAY", "05",
                "JUN", "06",
                "JUL", "07",
                "AUG", "08",
                "SEP", "09",
                "OCT", "10",
                "NOV", "11",
                "DEC", "12"
            )
)

按[索引列]排序[LossMMYY]

然后使用相同的逻辑在事实表中添加另一列:

Index Column = 
VAR YearNumber =
    RIGHT ( 'fact_Data'[LossMMYY], 2 ) + 2000
VAR CurrentMonthName =
    LEFT ( 'fact_Data'[LossMMYY], 3 )
RETURN YearNumber &
    SWITCH (
    CurrentMonthName,
    "JAN", "01",
    "FEB", "02",
    "MAR", "03",
    "APR", "04",
    "MAY", "05",
    "JUN", "06",
    "JUL", "07",
    "AUG", "08",
    "SEP", "09",
    "OCT", "10",
    "NOV", "11",
    "DEC", "12"
) 

现在您有了一个有效的索引,可以从中比较日期。您现在可以使用以下DAX创建度量:

Running Total =
VAR CurrentDate =
    MAX ( MMYY [Index Column] )
RETURN
    CALCULATE (
        SUM ( fact_Data[PolicyNumberCount] ),
        FILTER (
            ALL (fact_Data[Index Column] ),
            fact_Data[Index Column] <= CurrentDate
        )
    )

在矩阵的values参数中使用新度量,在column参数中使用新字段'MMYY'[LossMMYY]将产生以下结果: enter image description here

oleg,这是在.pbix中实现逻辑时得到的结果 enter image description here