Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Saving Filters as Measures

Hello Power BI Community,

 

I frequently find myself saving filters as VARs and then applying the variable using the CALCULATE function.


See below:

 

myExpression =
VAR testFilter = FILTER(table, table[column])
RETURN CALCUALTE(SUM(table[column]),testFilter)

 

And this works really well! However, I end up re-writing the same VARs for filters in other measures. I was wondering if there was a way to save these VARs and reuse them (possibly as a measure?).

 

Appreciate any and all help!

  • Hi Anonymous ,

    As FILTER function returns a table, so you cannot save it as a measure or column, but you can save it as a new table to solve your issue, like this:

    1.Create a new table.

    2.Create relationship.

    3.Directly use the new table as a filter in your DAX.

    Best Regards,
    Community Support Team _ kalyj

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

5 Replies

  • Hi Anonymous ,

    As FILTER function returns a table, so you cannot save it as a measure or column, but you can save it as a new table to solve your issue, like this:

    1.Create a new table.

    2.Create relationship.

    3.Directly use the new table as a filter in your DAX.

    Best Regards,
    Community Support Team _ kalyj

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hello Anonymous 

    You don't need to use the filter. You can directly use the column within CALCULATE() without using FILTER().
    MyExpression = 
    CALCUALTE( SUM( table[column] ), table[column1] = "Condition" )

    • Anonymous's avatar
      Anonymous
      Not applicable

      I could, but this doesn't really solve the issue of re-writing the conditional statement over and over again.

      • Anonymous's avatar
        Anonymous
        Not applicable

        It would be nice if I could change this in one place (like you can with a measure). Something like:

         

        myFilter = FITLER(table,table[column]="condition")

        To ultimately use the filter in multiple measures like this:
        measure1 = CALCULATE(SUM(table[column]),myFilter))
        measure2 = CALCULATE(SUM(table[column1]),myFilter))

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hello Anonymous 

    You can't create a myFilter measure as it'll through an error. You may try the calculation group in this case.