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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
lennox25
Post Patron
Post Patron

Conditional Column, IF statement or something else?

Hello, 

I'm stuck. I have the above data in a table below.

lennox25_1-1718360167688.png

I need to look at Result 2 against Result 1. If Result  is better (UP), Worse (DOWN) or the same (SAME) then I need a conditional column of IF statement or something else to show in the 'Result' Column.

lennox25_2-1718360205506.png

 

Results - Good being the best, then Satisfactory, Poor and Very Poor.

 

Thank you! 🙂

2 ACCEPTED SOLUTIONS
AlienSx
Super User
Super User

let
    Source = your_table,
    score = [Very Poor = 1, Poor = 2, Satisfactory = 3, Good = 4],
    res = {"DOWN", "SAME", "UP"},
    result = Table.AddColumn(
        Source, 
        "Result", 
        (x) => res{Value.Compare(Record.Field(score, x[Result 2]), Record.Field(score, x[Result 1])) + 1})
in
    result

View solution in original post

dufoq3
Community Champion
Community Champion

Hi @lennox25, another solution here:

 

you can edit this step and add/remove conditions - don't forget to keep correct order from best (lowest value) to worst.

 

dufoq3_0-1718365381331.png

 

Result

dufoq3_1-1718365402791.png

 

let
    DataTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs/PT1HSUQpOLMksTktMLskvqlSK1YlGFdBRCsjPL8ImjqEPrBC3erB1IPGw1KJKBXTFUMcg5JAEITpjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Result 1" = _t, #"Result 2" = _t]),
    ConditionTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs/PT1HSUTJUitWJVgpOLMksTktMLskvqgQKGoEFA/Lzi4AcYzAnLLWoUgEqYqIUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Condition = _t, #"Order Best to Worst" = _t]),
    ConditionTableChangedType = Table.TransformColumnTypes(ConditionTable,{{"Order Best to Worst", Int64.Type}}),
    MergedQueriesRes1 = Table.NestedJoin(DataTable, {"Result 1"}, ConditionTableChangedType, {"Condition"}, "Res1Value", JoinKind.LeftOuter),
    ExpandedRes1Value = Table.ExpandTableColumn(MergedQueriesRes1, "Res1Value", {"Order Best to Worst"}, {"Res1Value"}),
    MergedQueriesRes2 = Table.NestedJoin(ExpandedRes1Value, {"Result 2"}, ConditionTableChangedType, {"Condition"}, "Res2Value", JoinKind.LeftOuter),
    ExpandedRes2Value = Table.ExpandTableColumn(MergedQueriesRes2, "Res2Value", {"Order Best to Worst"}, {"Res2Value"}),
    Ad_Result = Table.AddColumn(ExpandedRes2Value, "Result", each if [Res1Value] < [Res2Value] then "UP" else if [Res1Value] = [Res2Value] then "SAME" else "DOWN", type text),
    RemovedColumns = Table.RemoveColumns(Ad_Result,{"Res1Value", "Res2Value"})
in
    RemovedColumns

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

8 REPLIES 8
dufoq3
Community Champion
Community Champion

Hi @lennox25, another solution here:

 

you can edit this step and add/remove conditions - don't forget to keep correct order from best (lowest value) to worst.

 

dufoq3_0-1718365381331.png

 

Result

dufoq3_1-1718365402791.png

 

let
    DataTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs/PT1HSUQpOLMksTktMLskvqlSK1YlGFdBRCsjPL8ImjqEPrBC3erB1IPGw1KJKBXTFUMcg5JAEITpjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Result 1" = _t, #"Result 2" = _t]),
    ConditionTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs/PT1HSUTJUitWJVgpOLMksTktMLskvqgQKGoEFA/Lzi4AcYzAnLLWoUgEqYqIUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Condition = _t, #"Order Best to Worst" = _t]),
    ConditionTableChangedType = Table.TransformColumnTypes(ConditionTable,{{"Order Best to Worst", Int64.Type}}),
    MergedQueriesRes1 = Table.NestedJoin(DataTable, {"Result 1"}, ConditionTableChangedType, {"Condition"}, "Res1Value", JoinKind.LeftOuter),
    ExpandedRes1Value = Table.ExpandTableColumn(MergedQueriesRes1, "Res1Value", {"Order Best to Worst"}, {"Res1Value"}),
    MergedQueriesRes2 = Table.NestedJoin(ExpandedRes1Value, {"Result 2"}, ConditionTableChangedType, {"Condition"}, "Res2Value", JoinKind.LeftOuter),
    ExpandedRes2Value = Table.ExpandTableColumn(MergedQueriesRes2, "Res2Value", {"Order Best to Worst"}, {"Res2Value"}),
    Ad_Result = Table.AddColumn(ExpandedRes2Value, "Result", each if [Res1Value] < [Res2Value] then "UP" else if [Res1Value] = [Res2Value] then "SAME" else "DOWN", type text),
    RemovedColumns = Table.RemoveColumns(Ad_Result,{"Res1Value", "Res2Value"})
in
    RemovedColumns

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hi @dufoq3 -  Im unsure how to do this? I have created a 'Condition Table' but have now idea how to work this with my orignal table where there is already many applied steps?

 

dufoq3
Community Champion
Community Champion

Hi, have you read note below my post? Keep in mind that in this case Source is DataTable (in my query).


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hi @dufoq3 yes, thanks, ok got that part now I have a seperate query resulting in exactly the same as you first answered just have no clue how to incorporate that into my working table

 

dufoq3
Community Champion
Community Champion

Edit 1st step of MyQuery this way:

 

dufoq3_1-1718612581781.png

 

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

AlienSx
Super User
Super User

let
    Source = your_table,
    score = [Very Poor = 1, Poor = 2, Satisfactory = 3, Good = 4],
    res = {"DOWN", "SAME", "UP"},
    result = Table.AddColumn(
        Source, 
        "Result", 
        (x) => res{Value.Compare(Record.Field(score, x[Result 2]), Record.Field(score, x[Result 1])) + 1})
in
    result

Hi @AlienSx , Thank you. It seems simple but how to I incoporate this into my table which already has many applied steps? TIA

@lennox25 create blank query, copy and paste my code, replace your_table in my code with the name of your query - the one with many steps (it's name is on the left pane in PQ Editor)

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors