Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Prepping for a Fabric certification exam? Join us for a live prep session with exam experts to learn how to pass the exam. Register now.

Reply
newpbiuser01
Helper V
Helper V

Using Filter with a Calculate Function Based on a Condition

Hello,

 

Would anyone know if I can apply an if statement to whether or not the calculate function applies a filter? As an example, I'm trying to say, 

test =
VAR _Category =   SELECTEDVALUE ( 'Table1'[VALUE] )
RETURN
    CALCULATE (  SUM ( 'Table2'[TOTAL SAVINGS] ),
        IF (  _Category <> "All",
            FILTER ( 'Table2', [CATEGORY] = _Category ),
            ALLSELECTED ( 'Table2'[CATEGORY] )
        )
    )

However, I get an error saying "A Function "FILTER" has been used in a True/False experssion that is used as a table filter expression. That is now allowed".

Is there a way to only use filter with a calculate function when a condition is met?

1 ACCEPTED SOLUTION
mickey64
Super User
Super User

How about this formula below?

 

test =
    VAR _Category = SELECTEDVALUE ( 'Table1'[VALUE] )
    RETURN
    IF ( _Category <> "All",
        SUMX ( FILTER('Table2', 'Table2'[CATEGORY] = _Category), 'Table2'[TOTAL SAVINGS] ), 

        CALCULATE ( SUM ( 'Table2'[TOTAL SAVINGS] ), ALLSELECTED ( 'Table2'[CATEGORY] ))
    )

View solution in original post

1 REPLY 1
mickey64
Super User
Super User

How about this formula below?

 

test =
    VAR _Category = SELECTEDVALUE ( 'Table1'[VALUE] )
    RETURN
    IF ( _Category <> "All",
        SUMX ( FILTER('Table2', 'Table2'[CATEGORY] = _Category), 'Table2'[TOTAL SAVINGS] ), 

        CALCULATE ( SUM ( 'Table2'[TOTAL SAVINGS] ), ALLSELECTED ( 'Table2'[CATEGORY] ))
    )

Helpful resources

Announcements
May PBI 25 Carousel

Power BI Monthly Update - May 2025

Check out the May 2025 Power BI update to learn about new features.

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

May 2025 Monthly Update

Fabric Community Update - May 2025

Find out what's new and trending in the Fabric community.