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
ajeeth20
New Member

Im having troubles with my switch in DAX

Hi Guys,
Im a beginner with DAX and what I dit wrong. This is my error message: The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
 
And this is my code:
 
CountFalse = SWITCH(
IF(
FILTER(Table1,[CheckColumn2] = "NO"), COUNT(Table1[Column2]), BLANK()
),
IF(
FILTER(Table1,[CheckColumn9] = "NO"), COUNT(Table1[Column9]), BLANK()
),
IF(
FILTER(Table1,[CheckColumn15] = "NO"), COUNT(Table1[Column15]), BLANK()
),
IF(
FILTER(Table1,[CheckColumn18] = "NO"), COUNT(Table1[Column18]), BLANK()
)
)
 
I want this code to count all the NO's in Table1. Multiple Columns have a NO so Im using a switch to count them all. 
Does someone know how I can make this work or do this in a easier way?
 
Thanks!
2 ACCEPTED SOLUTIONS
bcdobbs
Super User
Super User

Try something like

VAR Col2Count = 
	CALCULATE (
		COUNTROWS( Table1 ),
		Table1[Column2] = "NO"
		)
		
VAR Col9Count = 
	CALCULATE (
		COUNTROWS( Table1 ),
		Table1[Column9] = "NO"
		)
		
VAR Col15Count = 
	CALCULATE (
		COUNTROWS( Table1 ),
		Table1[Column15] = "NO"
		)
		
VAR Col18Count = 
	CALCULATE (
		COUNTROWS( Table1 ),
		Table1[Column18] = "NO"
		)
		
RETURN Col2Count + Col9Count + Col15Count + Col18Count


Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

View solution in original post

AlexisOlson
Super User
Super User

The reason for the error is that FILTER returns a table and IF expects either True or False as its first argument, not a table.

 

@bcdobbs has the best solution but you could do it with an iterator too:

SUMX (
    Table1,
	IF ( Table1[CheckColumn2]  = "NO, 1 ) +
	IF ( Table1[CheckColumn9]  = "NO, 1 ) +
	IF ( Table1[CheckColumn15] = "NO, 1 ) +
	IF ( Table1[CheckColumn18] = "NO, 1 )
)

 

View solution in original post

4 REPLIES 4
AlexisOlson
Super User
Super User

The reason for the error is that FILTER returns a table and IF expects either True or False as its first argument, not a table.

 

@bcdobbs has the best solution but you could do it with an iterator too:

SUMX (
    Table1,
	IF ( Table1[CheckColumn2]  = "NO, 1 ) +
	IF ( Table1[CheckColumn9]  = "NO, 1 ) +
	IF ( Table1[CheckColumn15] = "NO, 1 ) +
	IF ( Table1[CheckColumn18] = "NO, 1 )
)

 

Worked like a charm! Thank you!

kp54357
Helper I
Helper I

Can you try nested If Else instead of Switch ?

bcdobbs
Super User
Super User

Try something like

VAR Col2Count = 
	CALCULATE (
		COUNTROWS( Table1 ),
		Table1[Column2] = "NO"
		)
		
VAR Col9Count = 
	CALCULATE (
		COUNTROWS( Table1 ),
		Table1[Column9] = "NO"
		)
		
VAR Col15Count = 
	CALCULATE (
		COUNTROWS( Table1 ),
		Table1[Column15] = "NO"
		)
		
VAR Col18Count = 
	CALCULATE (
		COUNTROWS( Table1 ),
		Table1[Column18] = "NO"
		)
		
RETURN Col2Count + Col9Count + Col15Count + Col18Count


Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

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.

Top Solution Authors