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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.