如何按日期将数据汇总到表过滤中

时间:2019-08-17 00:20:03

标签: powerbi dax powerbi-desktop

我需要汇总dax表并按从上一个EffectiveDate起5个月的日期范围对其进行过滤,该日期为2019年7月27日

所以我的dax表达式:

TestTable1 = 
VAR LastEffDate = LASTDATE(fact_Premium[EffectiveDate])  // --  7/27/2019
RETURN 
SUMMARIZE(
      FILTER(dim_Date, DATEDIFF(DATEADD(STARTOFMONTH(LastEffDate), -5,MONTH), ENDOFMONTH(LastEffDate), MONTH)),
      dim_Date[Year Month],
       "Premium", [Ttl WP]
       ) 

但是由于某种原因,它为我带来了数据集中所有年份的数据:

enter image description here

我也尝试过:

TestTable1 = 
    VAR LastEffDate = LASTDATE(fact_Premium[EffectiveDate])  // --  7/27/2019
RETURN 
    SUMMARIZE (
        FILTER (
             dim_Date, 
             DATESBETWEEN(dim_Date[Date],
                 DATE(2019,5,1),               
                 DATE(2019,6,1)
             )
        ),
        dim_Date[Year Month],
        "Premium", [Ttl WP]
    ) 

但这给我一个错误:

A table of multiple values was supplied where a single value was expected.

我在这里想念东西吗?

2 个答案:

答案 0 :(得分:2)

您的过滤器表达式应使用DATESBETWEEN而不是datediff:

TestTable1 = 
    VAR LastEffDate = LASTDATE(fact_Premium[EffectiveDate])  // --  7/27/2019
RETURN 
    SUMMARIZE (
        FILTER (
             dim_Date, 
             DATESBETWEEN(
                 //expression for start date, 
                 //expression for end date
             )
        ),
        dim_Date[Year Month],
        "Premium", [Ttl WP]
    ) 

答案 1 :(得分:0)

我认为您仍然缺少汇总保费的信息,

  

SUM(Ttl WP)

TestTable1 =
VAR LastEffDate =
    LASTDATE ( fact_Premium[EffectiveDate] ) // --  7/27/2019
RETURN
    SUMMARIZE (
        FILTER (
            dim_Date,
            DATESBETWEEN ( dim_Date[Date], DATE ( 2019, 5, 1 ), DATE ( 2019, 6, 1 ) )
        ),
        dim_Date[Year Month],
        "Premium", SUM ( [Ttl WP] ) ---- Need to aggregate here
    )