OLAP SSAS MDX如何获取先前选择/可见日期的度量值

时间:2018-09-18 08:54:19

标签: ssas mdx olap

如何仅在日期维度的选定/可见成员中获取先前的日期成员?

我尝试了PREVMEMBER和LAG函数,但它们返回了以前的日历日期(昨天)。

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]
)

我的结果(昨天返回):

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]

如何仅在选定/显示的成员中获得PREV_SUM?

2 个答案:

答案 0 :(得分:0)

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

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

(抱歉,我不在PC上,因此无法测试)

答案 1 :(得分:0)

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]