基于断开的切片器的动态X轴日期范围

时间:2018-08-08 20:54:05

标签: ssas powerbi dax powerpivot

我有一个需要解决的问题。我正在根据此表使用断开的切片器

Time Frame := 
DATATABLE("ID", Integer, "Time Frame", string
, {
  {1, "3 Month"}
, {2, "6 Month"}
, {3, "9 Month"}
, {4, "12 Month"}
})

看起来像这样:

enter image description here

使用一些DAX措施:

Sum Of Paid := sum(Data[Paid])

Sum Of Paid 3 Month Period :=
CALCULATE (
    [Sum Of Paid],
    DATESINPERIOD ( Data[Date], LASTDATE ( Data[Date] ), -3, MONTH )
)

Sum Of Paid 6 Month Period :=
CALCULATE (
    [Sum Of Paid],
    DATESINPERIOD ( Data[Date], LASTDATE ( Data[Date] ), -6, MONTH )
)

Sum Of Paid 9 Month Period :=
CALCULATE (
    [Sum Of Paid],
    DATESINPERIOD ( Data[Date], LASTDATE ( Data[Date] ), -9, MONTH )
)

Sum Of Paid 12 Month Period :=
CALCULATE (
    [Sum Of Paid],
    DATESINPERIOD ( Data[Date], LASTDATE ( Data[Date] ), -12, MONTH )
)

Slicer - Paid Per Period :=
VAR selection =
    IF (
        HASONEVALUE ( 'Time Frame'[Time Frame] ),
        VALUES ( 'Time Frame'[Time Frame] ),
        "All"
    )
VAR ThreeMonth = [Sum of Paid 3 Month Period]
VAR SixMonth = [Sum of Paid 6 Month Period]
VAR NineMonth = [Sum of Paid 9 Month Period]
VAR TwelveMonth = [Sum of Paid 12 Month Period]
RETURN
    SWITCH (
        TRUE (),
        selection = "3 Month", ThreeMonth,
        selection = "6 Month", SixMonth,
        selection = "9 Month", NineMonth,
        selection = "12 Month", TwelveMonth,
        TwelveMonth
    )

我可以模拟切片器,并根据用户选择显示3、6、9和12个月的聚合。这些在卡片,choropleths和几乎我需要它们的所有东西中都很好用。我的问题是尝试创建在x轴上具有日期范围的折线图,条形图等。我一辈子都无法弄清楚如何复制DAX度量中引用的周期来动态设置x轴。

我尝试了一些似乎可行的方法,但不可行:

3 Month Period :=
CALCULATE (
    VALUES(Data[Date]),
    DATESINPERIOD ( Data[Date], LASTDATE ( Data[Date] ), -3, MONTH )
)

感谢您的帮助。

谢谢

1 个答案:

答案 0 :(得分:3)

您可能想尝试一下:

3 Month Period :=
CALCULATE (
    [Sum Of Paid],
    KEEPFILTERS (
        DATESINPERIOD (
            Data[Date],
            CALCULATE (
                MAX ( Data[Date] ),
                ALLSELECTED ()
            ),
            -3,
            MONTH
        )
    )
)