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

DAX Switch True

DAX experts, 

Here is how my data is set up:

ID:       Reason 1       Reason 2      Reason 3       Reason 4

1                    Correct          Incorrect      Incorrect       Correct

2                    Incorrect       Correct         Correct         Incorrect

3                   Correct           Incorrect      Correct         Correct

 

What I am trying to do is get a count of how many times each reason contains incorrect.

So Reason1 = 1; Reason2 = 2; Reason3 = 1

 

Right now I am doing a Switch True:

TEST = SWITCH(TRUE(),
    'Check'[Reason1] = "INCORRECT","Reason1",
    'Check'[Reason2] = "INCORRECT","Reason2",
    'Check'[Reason3] = "INCORRECT","Reason3",

This is wrong because it is saying only when reason 1 is incorrect then count the number of times reason 2 is incorrect. I basically need this to be calculated independent of what reason 1 was marked.

1 ACCEPTED SOLUTION
Rahal
Frequent Visitor

So, with the structure of your data you can only get something like this :

Test3.png

Otherwise you need to transform your data, for that u need to use SQL,

here i'm ganna start by creating my table Check :

Test4.png

Then i can tranform it also with Sql :

Test5.png

 

So finaly i can get what i need on Power BI :

 

Test6.png

View solution in original post

10 REPLIES 10
Rahal
Frequent Visitor

So, with the structure of your data you can only get something like this :

Test3.png

Otherwise you need to transform your data, for that u need to use SQL,

here i'm ganna start by creating my table Check :

Test4.png

Then i can tranform it also with Sql :

Test5.png

 

So finaly i can get what i need on Power BI :

 

Test6.png

Anonymous
Not applicable

I appreciate all your help on this!

Rahal
Frequent Visitor

Hi, if i understand what you want to do, you will need to create 4 measure for each reason and name them what you want :

Ofc replace ; with ,

Reason (1) = 
VAR R1=CALCULATE(count(Check[Reason 1]);Check[Reason 1]="Incorrect")
RETURN
if(ISBLANK(R1)=TRUE();0;R1)

Test2.png

Anonymous
Not applicable

That is super helpful thank you! 

 

When I go to make it into a clustered bar chart the axis is blank. How would I create a field to put in the axis so it labels all the bars as what the reason is?

 

powerbi2.PNG

Rahal
Frequent Visitor

Hi,

 

thats a mesure, and yeah the ; on your case is a comma ,

 

 

Anonymous
Not applicable

@Rahal 

That is working now thank you for all your help! 

 

The only thing I am struggling with now is the Return statement

 

How do I get the return statement to use the reason as a column header and the R1 value as the value for that reason.

 

powerbi.PNG

Rahal
Frequent Visitor

Hi, here's what u can do :

Test =

VAR R1=CALCULATE(count(Check[Reason 1]);Check[Reason 1]="Incorrect")
VAR R2=CALCULATE(count(Check[Reason 2]);Check[Reason 2]="Incorrect")
VAR R3=CALCULATE(count(Check[Reason 3]);Check[Reason 3]="Incorrect")
VAR R4=CALCULATE(count(Check[Reason 4]);Check[Reason 4]="Incorrect")

RETURN

"Reason1 = " & R1 & " ; Reason2 = " & R2 & " ; Reason3 = " & R3 & " ; Reason4 = " & R4
 

Test.png

Anonymous
Not applicable

Is that considered a new table, column or measure?

 

Additionally the ; that you have on lines 3-6 I cant not get to work. Should that be a comma?

kentyler
Solution Sage
Solution Sage

try changing your data to this format:

ID:       Reason        Response     

1                   1         Incorrect     

1                 2       Correct         

1                  3         Incorrect      

1                    4          Incorrect      

2                    1       Correct         

2                  2         Incorrect      

2                  3          Incorrect    

2                4      Correct         

 

it will make your calculation much easier

 





Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


Anonymous
Not applicable

That's a good idea. The only thing is I really have around 20 reasons and an ID can appear multiple times if it was incorrect for any reason the previous time it was submitted. 

 

So this table would then grow exponentially and be extremely large if I am understanding it correctly.

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