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
akwang
Helper II
Helper II

DAX IF Criteria

Hi All,

 

So i have a some data with student exam information. Student1 has written 5 exams as follows Math-pass, English-Pass, Geo-pass, His-pass, alg-fail. Student2 has written same exams as follows Math-pass, English-fail, Geo-fail, His-pass, alg-fail.

 

Now i need to write some DAX that would give overall if student pass or fail based on the following criteria:

1. The must pass Math

2. The must pass Eng

3.The must pass one other subject

 

So the result would be: Student 1=pass, Student 2=fail.

 

Any ideas how to approch such a task. thanks

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Ok try this combination of measures:

[PassMath] = CALCULATE(
	COUNTROWS('YourTable'),
	ALLEXCEPT('YourTable', 'YourTable'[Candidate #], 'YourTable'[Subject]),
	'YourTable'[Subject] = "MATH",
	'YourTable'[Pass Exam] = "pass"
) > 0
[PassEnglish] = CALCULATE(
	COUNTROWS('YourTable'),
	ALLEXCEPT('YourTable', 'YourTable'[Candidate #], 'YourTable'[Subject]),
	'YourTable'[Subject] = "ENGA",
	'YourTable'[Pass Exam] = "pass"
) > 0
[Pass3Others] = CALCULATE(
	COUNTROWS('YourTable'),
	ALLEXCEPT('YourTable', 'YourTable'[Candidate #], 'YourTable'[Subject]),
	not 'YourTable'[Subject] = "ENGA",
	not 'YourTable'[Subject] = "MATH",
	'YourTable'[Pass Exam] = "pass"
) > 2
[PassOverAll] = AND(
	AND(
		[PassMath],
		[PassEnglish]
	),
	[Pass3Others]
)

View solution in original post

20 REPLIES 20
Anonymous
Not applicable

I would handle this as part of your data import.  I would attempt to pivot the data such that you have a single row for each student, and the classes as their own column with a Pass/Fail as a true/false field.

From here you can then include a Pass/Fail field that contains logic for the entire row.  This field would be something like (In the add column GUI box that opens up)

= if [mathResult] and [EngResult] and ([GeoResult] or [HisResult] or [AlgResult])

Hi Guys thanks for the replies,

I think i might have doen a bad job explaining my problem. So herei go again with an attached image of my table.

Criterias:

1. must pass Math

2.must pass EngA

3.must pass any other 3 subjects

 

Screenshot_2.jpg

 

If they wrote anything less than 5 subjects (which would include Math & EngA) then that would count as fail too.

Anonymous
Not applicable

Ok try this combination of measures:

[PassMath] = CALCULATE(
	COUNTROWS('YourTable'),
	ALLEXCEPT('YourTable', 'YourTable'[Candidate #], 'YourTable'[Subject]),
	'YourTable'[Subject] = "MATH",
	'YourTable'[Pass Exam] = "pass"
) > 0
[PassEnglish] = CALCULATE(
	COUNTROWS('YourTable'),
	ALLEXCEPT('YourTable', 'YourTable'[Candidate #], 'YourTable'[Subject]),
	'YourTable'[Subject] = "ENGA",
	'YourTable'[Pass Exam] = "pass"
) > 0
[Pass3Others] = CALCULATE(
	COUNTROWS('YourTable'),
	ALLEXCEPT('YourTable', 'YourTable'[Candidate #], 'YourTable'[Subject]),
	not 'YourTable'[Subject] = "ENGA",
	not 'YourTable'[Subject] = "MATH",
	'YourTable'[Pass Exam] = "pass"
) > 2
[PassOverAll] = AND(
	AND(
		[PassMath],
		[PassEnglish]
	),
	[Pass3Others]
)

So i finally got the measures to work , however when i checked the results i got 10 persons meeting the 3 criterias out of 72 persons. When i divide and change to % i get 1.39% instead of 13.9%. So i had to multiply by 10 to get correct answer. Any ideas why?Screenshot_3.jpg

Anonymous
Not applicable

I'd guess that the measure you used to get to the 1.39% has an error in it.  What was the formula you used?

hi @Anonymous,

See my measures below. I think it is the PassOverall Measure that has the problem since when on a card it says TRUE instead of giving me the count which is 10.

 

PassOverAll = (AND(AND([PassMath],[PassEng]),[Pass3 Others]))

 

PassMath = CALCULATE(
    COUNTROWS('90080-Laventure Results'),
    ALLEXCEPT('90080-Laventure Results', '90080-Laventure Results'[Candidate #], '90080-Laventure Results'[Subject]),
    '90080-Laventure Results'[Subject] = "MATH",
    '90080-Laventure Results'[Pass Exam] = "pass"
)>0

 

PassEng = CALCULATE(
    COUNTROWS('90080-Laventure Results'),
    ALLEXCEPT('90080-Laventure Results', '90080-Laventure Results'[Candidate #], '90080-Laventure Results'[Subject]),
    '90080-Laventure Results'[Subject] = "ENGA",
    '90080-Laventure Results'[Pass Exam] = "pass"
) >0

 

Matriculation = DIVIDE([PassOverAll],[Total Candidates])

@Anonymous& @ValentinBIA Still no luck getting the count of 10 for this. Any ideas?

Anonymous
Not applicable

@akwang  The Passoverall was designed just to be True/False.  You will need to use it in conjunction with another measure if you want to get that count.  I'd suggest using the PassOverall as a page or visual filter then having a count measure like:

CountPasses = 
VAR passCount = COUNTROWS('90080-Laventure Results')
VAR allCount = CALCULATE(
	COUNTROWS('90080-Laventure Results'),
    ALLEXCEPT('90080-Laventure Results', '90080-Laventure Results'[Candidate #], '90080-Laventure Results'[Subject])
)
RETURN
DIVIDE(passCount, allCount)

 

 

hi @Anonymous, when idid that i got 1 instead of 10 passes. Just to clarify, i want to count how much TRUE in the Passcount. Also rather than create the previous 4 measures for passmath,passeng,pass3 other & passcount, would it make sense to create calculated column and then sum the TRUE?

 

Screenshot_1.jpgScreenshot_2.jpg    Screenshot_3.jpg

Anonymous
Not applicable

Making them columns is a bad idea for performance and modeling reasons.  The measures are the way to go.

 

The specific measure you want for counting ultimately depends on how you are expecting to use the measure.  I'm trying to anticipate how you are using it and my hope is that you would understand the approach and tweak the measure yourself to adjust it for your specific purposes.

 

Here is a new measure and a specific use case for it:

 

CountPasses = 
VAR passCount = COUNTROWS('90080-Laventure Results')
VAR allCount = CALCULATE(
	COUNTROWS('90080-Laventure Results'),
    ALL('90080-Laventure Results')
)
RETURN
DIVIDE(passCount, allCount)

Put that measure into the CountPasses card and apply a visual level filter of PassOverAll = True.

Hi @Anonymous,

So when i try to apply the visual filter of passoverall i am not getting the option to adjust the filter on the card. I created a mesure as below: Count = IF([PassOverAll]=TRUE(),COUNT('90080-Laventure Candidate'[Candidate #]),0) which gives the correct value on the table visual but i cant seem to be able to apply same on card visual. I am still a newbie when it comes to DAX so bear with me.

Screenshot_2.png

Hi, ireceivedthis error on the pass3others measure: The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.

Anonymous
Not applicable

Thats strange, its working for me.  Just check no comma's are missing.  I know i forgot them the first time i posted the answer, then i had to edit it afterwards.

 

Capture.PNG

 

Still not working.

What i am trying to do is to calculate out of the total students what percentage meets the criteria.

Anonymous
Not applicable

Sadly the organisation's security policy for where i work won't allow me to check out your pbix file.

Darn it 😞  How about ifyou send me your pbix that you got it working on, maybe i can compare and see my error.

Anonymous
Not applicable

Its a fairly simplistic file but hopefully it helps.  Please let me know if this link works for you:

 

EDIT: Removed link

yes it worked but i think you selected the wrong file. This one is to monitor pallets

Anonymous
Not applicable

Ahh rats you are right.  I was working on another challenge for someone else and I've got my wires crossed.  I didn't save your demo project at the end of the day yesterday.  Sorry.

ValentinBIA
Resolver I
Resolver I

Hi @akwang,

 

I am not sure about the DAX writing of it, but the logic would be the following:

 

If Math = fail then "fail"

   else if English = fail then "fail"

   else if Geo = pass then "pass"

   else if His = pass then "pass"

   else if Alg = pass then "pass"

   else "fail"

 

For the DAX formula, not too sure about it but:

IF([Math]="fail","fail",

  IF([English]="fail","fail",

  IF([Geo]="pass","pass",

  IF([His]="pass","pass",

  IF([Alg]="pass","pass","fail")))))

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.