创建每周6个月的滚动平均值过滤条件

时间:2018-12-10 03:28:02

标签: powerbi dax

我正在尝试创建一个报告,该报告应该提供每周数据,还应提供一个列,用于滚动显示6个月到上个月和去年同期。

我可以使用以下公式计算滚动平均值:

sentry_sdk.init(
    dsn="sentry dsn",
    integrations=[DjangoIntegration()],
    send_default_pii=True
)

数据显示很好,但是一旦我在[Year Week]上放置了切片器,它就会开始提供每周数据,而不是滚动平均值。

我认为我需要使用ALL过滤器,但是我的努力还没有得到回报。感谢任何帮助。

报告结构如下:

  

类别

     

Current_Week_Data

     

上一年同周数据

     

差异%

     

滚动6个月(今年-前6年6个月/上一年6个月)

1 个答案:

答案 0 :(得分:1)

我认为这可以解决问题。如果我们在12月(如现在)中,它将对今年和去年11月至11月的'Data'[X]求和,然后计算分数变化。从计算角度来看,切片器不会在[Year Week]

上受到影响。
Rolling n month average = 
var n = 7 //number of months looking back
var currentDate = MAX('Calendar'[Date]) // max date in current context
var startDate = EOMONTH(EDATE(currentDate; -n);0) // end date of the 7th month back
var currentDateLY = currentDate-364 // Monday -> Monday, use 365 if date match is more important. Using 365 also removes strange values in the beginning/end of months.
var startDateLY = EOMONTH(EDATE(currentDateLY; -n); 0)

var theDataTY = // the data This Year
CALCULATE(
    SUM('Data'[X]);
    ALL('Calendar'[Year Week]);
    FILTER(
        ALL('Calendar'[Date]);
        'Calendar'[Date] > ( startDate ) && 'Calendar'[Date] <DATE(YEAR(currentDate);MONTH(currentDate);1) // the 6 month interval 
    )
)
var theDataLY = // the data Last Year
CALCULATE(
    SUM('Data'[X]);
    ALL('Calendar'[Year Week]);
    FILTER(
        ALL('Calendar'[Date]);
        'Calendar'[Date] > ( startDateLY ) && 'Calendar'[Date] <DATE(YEAR(currentDateLY);MONTH(currentDateLY);1) // the 6 month period last year
    )
)

return
DIVIDE( // calculate the fractional change
    theDataTY - theDataLY;
    theDataLY;
    0 // returns 0 if fraction is not defined. 
)

我的设置中有两个表:“日历”和“数据”。 “日历” [日期]和“数据” [日期]之间存在1 ::关系。另外,我毫不怀疑在DAX中有更好的方法可以做到这一点,这就是我本来会做到的。

希望这会有所帮助。

干杯

奥斯卡

相关问题