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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Combining Filtering conditional on an IF statement

I am trying to create a new measure whereby you apply a calculation to a filtered version of the table, based on an if statment of a different column.

 

A simplified version of the table is shown below:

CategoryYearValue
A201811
A201912
A202014
B20189
B

2019

9
B202019

 

What I am trying to do is have a conditional filtering, so that when Category == "A", then we filter to Years == 2018 or 2019 and sum Value, but if Category = B, then we filter to Year == 2019 or 2020, and sum Value.

 

I currently have a dax formula that looks like this:

 

Measure = CALCULATE(SUM(Sheet1[Value]), 
                    IF(Sheet1[Category] == "A", 
                    Sheet1[Year] == 2018 || Sheet1[Year] == 2019, 
                    Sheet1[Year] == 2019 || Sheet1[Year] == 2020))
However, I am getting the error "The expression contains multiple columns, but only a single column can be used in a True/False expression that is used as a table filter expression."
 
Could anyone help with how best to tackle this problem?
1 ACCEPTED SOLUTION

Hi @Anonymous 

It is not clear what you need.

If the measure is to add up everything with those rules: 

 

Measure =
SUMX (
    Sheet1,
    SWITCH (
        TRUE (),
        Sheet1[Category] = "A" && Sheet1[Year] IN { 2018, 2019 }, Sheet1[Value],
        Sheet1[Category] = "B" && Sheet1[Year] IN { 2019, 2020 }, Sheet1[Value],
        0
    )
)

 

   

View solution in original post

4 REPLIES 4
Cmcmahan
Resident Rockstar
Resident Rockstar

Your original problem is that you're trying to test whether [Category] = "A".  The problem is that [Category] is a column, not a single value.  And when creating measures, PowerBI doesn't know that you plan to use this only in circumstances where the context has been filtered to a single category.  You can use an aggregation to change the value of [Category] from a column of values to a single value.  My preferred option is SELECTEDVALUE, since it will work when you have multiple rows of data, but they all have the same value in the given column. You can also default the result, if there's no values, or if you get multiple values.  The default for this is BLANK().

 

For minimal changes to your original measure, you can use this. New updates are in blue.

 

SimilarMeasure =
CALCULATE (
    SUM ( Sheet1[Value] ),
    FILTER (
        Sheet1,
        IF (
            SELECTEDVALUE ( Sheet1[Category] ) == "A",
            Sheet1[Year] == 2018 || Sheet1[Year] == 2019,
            Sheet1[Year] == 2019 || Sheet1[Year] == 2020
        )
    )
)

 

 

For a more robust measure, I would use SWITCH, since you can add more category options without a whole series of nested IFs.

 

SwitchMeasure =
CALCULATE (
    SUM ( Table1[Value] ),
    FILTER (
        Table1,
        SWITCH (
            SELECTEDVALUE ( Table1[Category] ),
            "A", Table1[Year] == 2018 || Table1[Year] == 2019,
            "B", Table1[Year] == 2019 || Table1[Year] == 2020, //Add more categories in this format
TRUE() //This is the else expression, and will return all values if SELECTEDVALUE([Category]) is not A or B,
// e.g. in a total row of a table ) ) )

EDIT: just saw that @AlB had a very similar method.  The SUMX version will also work, and only sum each row if it meets your condition.  The downside is that it has worse performance than SUM, since it has to re-evalute each row individually instead of filtering the entire table, but is likely the cleanest way to write this measure if you have a large number of categories.

RobbeVL
Impactful Individual
Impactful Individual

Hi,

 

What is it you are exacly trying to calculate here?
Could you specify ? 

 

Robbe

Anonymous
Not applicable

The result would look like:

 

CategoryYearValueMeasure
A20181111
A20191212
A202014 
B20189 
B

2019

99
B20201919

 

Or if we didnt include year or value in the table in the table:

 

CategoryMeasure
A23
B28

 

 

Hi @Anonymous 

It is not clear what you need.

If the measure is to add up everything with those rules: 

 

Measure =
SUMX (
    Sheet1,
    SWITCH (
        TRUE (),
        Sheet1[Category] = "A" && Sheet1[Year] IN { 2018, 2019 }, Sheet1[Value],
        Sheet1[Category] = "B" && Sheet1[Year] IN { 2019, 2020 }, Sheet1[Value],
        0
    )
)

 

   

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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