Filter SUMMARIZECOLUMNS

后端 未结 1 1053
长情又很酷
长情又很酷 2020-12-18 09:14

How to construct filter tables for SUMMARIZECOLUMNS function?

The SUMMARIZECOLUMNS has the following pattern:

SUMMARIZECOLUMNS( 
    Co         


        
相关标签:
1条回答
  • 2020-12-18 09:48

    You can also construct them the way PowerBI does, using VAR:

    VAR  __MyFilterTable = FILTER( T, T[col] = "red" ) 
    
    RETURN
    SUMMARIZECOLUMNS (
        T[col],
        __MyFilterTable
    )
    

    Which is more efficient will depend on the complexity your filtering, so there is no "one size fits all" rule necessarily. For a simple table level filter, just FILTER will suffice. I caution you that Line 1, where you're filtering the entire table T, is a bad idea. It's much more performant to only filter a single column. When you filter the entire table, DAX materializes the entire table in memory, while the following just materializes the one value of T[col]:

    VAR  __MyFilterTable = FILTER( ALL(T[col]), T[col] = "red" ) // This is better.
    
    RETURN
    SUMMARIZECOLUMNS (
        T[col],
        __MyFilterTable
    )
    

    You can do even better than that, conceptually. You can basically tell DAX, "I know this is a value, so don't even look in the table for it. Just make me a table and treat it as though I filtered it. Like this:

    VAR  __MyFilterTable = TREATAS ({"red"}, T[col] )
    
    RETURN
    SUMMARIZECOLUMNS (
        T[col],
        __MyFilterTable
    )
    

    Again, this is the pattern that PowerBI uses when performing its filters.

    BTW, Creating the filter tables a the top vs. creating them inline with SUMMARIZECOLUMNS() won't make any difference for speed. Avoid using CALCULATETABLE() as you've done here generally.

    You can also do this as well, though you aren't likely to see a speed increase generally:

    CALCULATETABLE(
        SUMMARIZECOLUMNS (
            T[col]
        ),
        KEEPFILTERS(T[col] = "red")
    )
    
    0 讨论(0)
提交回复
热议问题