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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Kashinoda
Advocate I
Advocate I

Conditional statements within a CALCULATE filter

Hello,

I have a simple measure which intersects my fact table with several disconnected tables and works without issue:

 

Intersect = 
VAR varInter1 = INTERSECT(ALL('Fact'[County]),  VALUES ( 'Treatas1'[Values] ))
VAR varInter2 = INTERSECT(ALL('Fact'[Town]),  VALUES ( 'Treatas2'[Values] ))
RETURN
CALCULATE ( sum('Fact'[Amount]),varInter1, varInter2 ) 

 

Output:

Kashinoda_0-1645173417315.png

I would like to dynamically control which types are used via a slicer, making the INTERSECT dynamic.

Kashinoda_1-1645173727026.png

The code I attempted does not work as CALCULATE is expecting a column filter.

 

Intersect = 
VAR varSelect1 = IF (HASONEVALUE(Treatas1[Type]), VALUES(Treatas1[Type]))
VAR varInter1 = SWITCH ( TRUE (), varSelect1 = "County", INTERSECT(ALL('Fact'[County]),  VALUES ( 'Treatas1'[Values] )), 
                                 varSelect1 = "Town", INTERSECT(ALL('Fact'[Town]),  VALUES ( 'Treatas1'[Values] )),
                                 varSelect1 = "Road", INTERSECT(ALL('Fact'[Road]),  VALUES ( 'Treatas1'[Values] )))

VAR varSelect2 = IF (HASONEVALUE(Treatas2[Type]), VALUES(Treatas2[Type]))
VAR varInter2 = SWITCH ( TRUE (), varSelect2 = "County", INTERSECT(ALL('Fact'[County]),  VALUES ( 'Treatas2'[Values] )), 
                                 varSelect2 = "Town", INTERSECT(ALL('Fact'[Town]),  VALUES ( 'Treatas2'[Values] )),
                                 varSelect2 = "Road", INTERSECT(ALL('Fact'[Road]),  VALUES ( 'Treatas2'[Values] )))
RETURN
CALCULATE ( sum('Fact'[Amount]), varInter1, varInter2 ) 

 

 

I could obviously use a conditional statement before CALCULATE for this example, but the final product will be using up to 10 types with up to 4 combinations which would be an enormous bit of code.

 

3 REPLIES 3
v-yanjiang-msft
Community Support
Community Support

Hi @Kashinoda ,

I think the error is caused of the SWITCH function can't return a column or table, but only a value.

Best Regards,
Community Support Team _ kalyj

amitchandak
Super User
Super User

@Kashinoda , Try if this can work

 

Intersect =
VAR varSelect1 = IF (HASONEVALUE(Treatas1[Type]), VALUES(Treatas1[Type]))
VAR varInter1 = SWITCH ( TRUE (), varSelect1 = "County", filter('Fact', 'Fact'[County] in INTERSECT(ALL('Fact'[County]), VALUES ( 'Treatas1'[Values] ))),
varSelect1 = "Town", filter('Fact', 'Fact'[Town] in INTERSECT(ALL('Fact'[Town]), VALUES ( 'Treatas1'[Values] ))),
varSelect1 = "Road", filter('Fact', 'Fact'[Road] in INTERSECT(ALL('Fact'[Road]), VALUES ( 'Treatas1'[Values] ))))

VAR varSelect2 = IF (HASONEVALUE(Treatas2[Type]), VALUES(Treatas2[Type]))
VAR varInter2 = SWITCH ( TRUE (), varSelect2 = "County", filter('Fact', 'Fact'[County] in INTERSECT(ALL('Fact'[County]), VALUES ( 'Treatas2'[Values] ))),
varSelect2 = "Town", filter('Fact', 'Fact'[Town] in IINTERSECT(ALL('Fact'[Town]), VALUES ( 'Treatas2'[Values] ))),
varSelect2 = "Road", filter('Fact', 'Fact'[Road] in INTERSECT(ALL('Fact'[Road]), VALUES ( 'Treatas2'[Values] ))))
RETURN
CALCULATE ( sum('Fact'[Amount]), varInter1, varInter2 )

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks for your reply, unfortunately this errors with "The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value".

I uploaded the PBIX here: https://drive.google.com/file/d/1j9watMGIEvBB82nMzP0h3bxJolUD6twl

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.