OLAP SSAS MDX 如何获取前一个 selected/visible 日期的度量值

OLAP SSAS MDX How to get measure value of previous selected/visible date

如何只获取日期维度selected/visible个成员中的前一个日期成员?

我试过 PREVMEMBER 和 LAG 函数,但它们 return 之前的日历日期(昨天)。

OLAP 立方体中的数据:

DATE       | SUM
-----------------
2018-09-01 | 500
2018-09-02 | 150
2018-09-03 | 300
2018-09-04 | 777
2018-09-05 | 900
2018-09-06 | 1200
2018-09-07 | 1500

在我的查询中,我在过滤器中选择了不同的日期,我需要获取之前可见日期的总和:

DATE       |  SUM | PREV_SUM
-------------------------------
2018-09-02 |  150 | NULL
2018-09-04 |  777 | 150 (from 2018-09-02)
2018-09-07 | 1500 | 777 (from 2018-09-04)

我的 MDX 查询:

WITH
    MEMBER PREV_MEMBER AS
        MEMBERTOSTR([dim_date].[Day Id].CURRENTMEMBER.PREVMEMBER)
    MEMBER PREV_MEMBER_LAG AS
        MEMBERTOSTR([dim_date].[Day Id].CURRENTMEMBER.lag(1))
    MEMBER PREV_SUM AS
        SUM(
            STRTOMEMBER(PREV_MEMBER),
            [Measures].[SUM]
        )
SELECT
    NON EMPTY {
        [Measures].[SUM],
        PREV_SUM,
        PREV_MEMBER,
        PREV_MEMBER_LAG
    } ON COLUMNS,
    NON EMPTY {(
        [dim_date].[Day Id].ALLMEMBERS
    )} ON ROWS
FROM (
    SELECT ({
        [dim_date].[Day Id].&[20180902],
        [dim_date].[Day Id].&[20180904],
        [dim_date].[Day Id].&[20180907]
    }) ON COLUMNS
    FROM [cub_main]
)

我的结果(returns 昨天):

DATE     | SUM  |  PREV_SUM | PREV_MEMBER                     | PREV_MEMBER_LAG
--------------------------------------------------------------------------------------------
20180902 | 150  |       500 | [dim_date].[Day Id].&[20180901] | [dim_date].[Day Id].&[20180901]
20180904 | 777  |       300 | [dim_date].[Day Id].&[20180903] | [dim_date].[Day Id].&[20180903]
20180907 | 1500 |      1200 | [dim_date].[Day Id].&[20180906] | [dim_date].[Day Id].&[20180906]

如何只在 selected/displayed 个成员中获得 PREV_SUM?

尝试在 WITH 子句中创建自定义集。该集合将由日期组成。

然后使用 GENERATE 函数迭代集合。我认为滞后应该只使用集合中的日期。

(很抱歉我不在电脑旁所以无法测试)

WITH
    // Create custom set
    SET SSS AS
        {
            [dim_date].[Day Id].&[20180902],
            [dim_date].[Day Id].&[20180904],
            [dim_date].[Day Id].&[20180907]
        }

    // Find current date member rank in custom set
    // Decrement index of current member by 2
    // Use ITEM function
    MEMBER PREV_MEMBER AS
        SETTOSTR(SSS.ITEM(RANK([dim_date].[Day Id].CURRENTMEMBER, SSS)-2))
    MEMBER PREV_SUM AS
        SUM(
            STRTOSET(PREV_MEMBER),
            [Measures].[SUM]
        )
SELECT
    NON EMPTY {
        [Measures].[SUM],
        PREV_MEMBER,
        PREV_SUM
    } ON COLUMNS,
    NON EMPTY {(
        // Use custom set in rows as a date filter
        SSS
    )} ON ROWS
FROM [cub_main]