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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
ragygad
Frequent Visitor

Compare values with if statement

I have a table with multiple fields and my main concern is 2 fields, one has ID and the other has grade (numerical field). 

I am trying to check first id ID to ID match in the same field and if they match, check grade field and see if its value higher than its previous value bases on the date order (if its higher for a higher date then 1 , if its lower for a higher date then its 0) then put a 1 else put a 0
 
ragygad_0-1689372923138.png

 

10 REPLIES 10
ragygad
Frequent Visitor

is there a way to do it in DAX

To achieve it in DAX, try this code NewColumn =
IF(
Table1[ID] = EARLIER(Table1[ID]),
IF(
Table1[Grade] > EARLIER(Table1[Grade]),
1,
0
),
BLANK()
)

I tried that but you cant have earlier in a function with if statement , it only works with filter function 

 

Oh I see, can you try this 
NewColumn =
IF(
Table1[ID] = CALCULATE(MAX(Table1[ID]), FILTER(ALL(Table1), EARLIER(Table1[ID]) = Table1[ID])),
IF(
Table1[Grade] > CALCULATE(MAX(Table1[Grade]), FILTER(ALL(Table1), EARLIER(Table1[ID]) = Table1[ID])),
1,
0
),
BLANK()
)

it doesnt work. 
so this is a dummy table of what i was trying to explain

 

ragygad_0-1689366870127.png

 

Seeing the table helps, try this calculated column
HigherGradeFlag =
VAR CurrentID = Table1[ID]
VAR CurrentGrade = Table1[Grade]
VAR PreviousGrade =
CALCULATE (
MAX ( Table1[Grade] ),
FILTER ( Table1, Table1[ID] = CurrentID && Table1[Grade] < CurrentGrade )
)
RETURN
IF ( CurrentGrade > PreviousGrade, 1, 0 )

its not working , please check the attachement 

ragygad_0-1689369095500.png

the first 2 grade value should have been 0 since they are not higher than any other values in the same field

Sorry for the missight, try this 
HigherGradeFlag =
VAR CurrentID = Table1[ID]
VAR CurrentGrade = Table1[Grade]
VAR PreviousGrade =
CALCULATE (
MAX ( Table1[Grade] ),
FILTER ( Table1, Table1[ID] = CurrentID && Table1[Grade] < CurrentGrade )
)
VAR IsFirstOccurrence =
CALCULATE (
COUNTROWS ( Table1 ),
FILTER ( Table1, Table1[ID] = CurrentID && Table1[Grade] < CurrentGrade )
) = 0
RETURN
IF ( IsFirstOccurrence || CurrentGrade > PreviousGrade, 1, 0 )

its erroring 

AbhinavJoshi
Responsive Resident
Responsive Resident

Hello, you could use a if else in Power Query M, here's a sample code
 = Table.AddColumn(#"Previous Step", "OutputColumnName", each if ( ( [#"ID1"] = [#"ID2"] ) and ( [#"GradeValue"] > YourValue ) ) then 1 else 0)

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.