DAX 函数使用变量提高效率

DAX Function Using Variables for Efficiency Purposes

这是我的 dax。我不断收到以下错误消息。

Keepfilters function can only be used as a top level filter argument of calculate and calculate table or with a table argument of a function performing a table scan.

有人有什么建议吗?这是我的代码:

GB 1-2 Login Counts =  
var kf_ds_glc_not_empty = KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT]) <> ""
var kf_ds_glc_eq_12 = KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT]) = "1-2"

RETURN

switch(max(GBvsProg[Filter Order]),

    1,
    CALCULATE(COUNT(D_STUDENTS[GO_LOGIN_CAT]),
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] <> ""), --This is what was here originally.
        kf_ds_glc_not_empty ,
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] = "1-2"), --This is what was here originally.
        kf_ds_glc_eq_12,
        KEEPFILTERS(D_STUDENTS[PROGRESS_GO_PASSRATECAT] = "Failing 100%") --This is what was here originally.
        ),

    2,
    CALCULATE(COUNT(D_STUDENTS[GO_LOGIN_CAT]),
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] <> ""), --This is what was here originally.
        kf_ds_glc_not_empty ,
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] = "1-2"), --This is what was here originally.
        kf_ds_glc_eq_12,
        KEEPFILTERS(D_STUDENTS[PASSING_GO_PASSRATECAT] = "Failing 100%") --This is what was here originally.
        ),

    3,
    CALCULATE(COUNT(D_STUDENTS[GO_LOGIN_CAT]),
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] <> ""), --This is what was here originally.
        kf_ds_glc_not_empty ,
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] = "1-2"), --This is what was here originally.
        kf_ds_glc_eq_12,
        KEEPFILTERS(D_STUDENTS[PROGRESS_GO_PASSRATECAT] = "Passing 0-49%") --This is what was here originally.
        ),

    4,
    CALCULATE(COUNT(D_STUDENTS[GO_LOGIN_CAT]),
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] <> ""), --This is what was here originally.
        kf_ds_glc_not_empty ,
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] = "1-2"), --This is what was here originally.
        kf_ds_glc_eq_12,
        KEEPFILTERS(D_STUDENTS[PASSING_GO_PASSRATECAT] = "Passing 0-49%") --This is what was here originally.
        ),

    5,
    CALCULATE(COUNT(D_STUDENTS[GO_LOGIN_CAT]),
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] <> ""), --This is what was here originally.
        kf_ds_glc_not_empty ,
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] = "1-2"), --This is what was here originally.
        kf_ds_glc_eq_12,
        KEEPFILTERS(D_STUDENTS[PROGRESS_GO_PASSRATECAT] = "Passing 50-74%") --This is what was here originally.
        ),

    6,
    CALCULATE(COUNT(D_STUDENTS[GO_LOGIN_CAT]),
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] <> ""), --This is what was here originally.
        kf_ds_glc_not_empty ,
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] = "1-2"), --This is what was here originally.
        kf_ds_glc_eq_12,
        KEEPFILTERS(D_STUDENTS[PASSING_GO_PASSRATECAT] = "Passing 50-99%") --This is what was here originally.
        ),

    7,
    CALCULATE(COUNT(D_STUDENTS[GO_LOGIN_CAT]),
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] <> ""), --This is what was here originally.
        kf_ds_glc_not_empty ,
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] = "1-2"), --This is what was here originally.
        kf_ds_glc_eq_12,
        KEEPFILTERS(D_STUDENTS[PROGRESS_GO_PASSRATECAT] = "Passing 100%") --This is what was here originally.
        ),

    8,
    CALCULATE(COUNT(D_STUDENTS[GO_LOGIN_CAT]),
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] <> ""), --This is what was here originally.
        kf_ds_glc_not_empty ,
        -- KEEPFILTERS(D_STUDENTS[GO_LOGIN_CAT] = "1-2"), --This is what was here originally.
        kf_ds_glc_eq_12,
        KEEPFILTERS(D_STUDENTS[PASSING_GO_PASSRATECAT] = "Passing 100%") --This is what was here originally.
        )
    ) + 0





            
            
            
            

KEEPFILTER 只能在 CALCULATE 或 CALCULATETABLE 函数表达式或过滤器中使用。 这意味着如果你想使用 var 你应该这样做(在变量中使用 FILTER 或 CALCULATETABLE):

var kf_ds_glc_not_empty = CALCULATETABLE(D_STUDENTS, D_STUDENTS[GO_LOGIN_CAT] <> "" )

并在计算中:

   CALCULATE(COUNT(D_STUDENTS[GO_LOGIN_CAT]),
        KEEPFILTERS(kf_ds_glc_not_empty) ,
       KEEPFILTERS( kf_ds_glc_eq_12),
        KEEPFILTERS(D_STUDENTS[PROGRESS_GO_PASSRATECAT] = "Failing 100%") --This is what was here originally.
        ),