Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Create an if statement in a measure that returns both text and a column field

Totally new to Power BI and just can't get my head around this !!

I want to create a Measure that uses an if statement in my Model Table. I can write the if statement ok I just want to return a combination of a field on my table and text.

 

Measure = if( ([Field1])=0,"OK","Error")
This works.
What I want is something like
Measure = if( ([Field1])=0,"OK","Error -"&([Field2]))
 
Could you point me in the right direction please?
  • Anonymous's avatar
    Anonymous
    4 years ago

     figured this out. I created a new column within the data environment and used the following syntax (which I guess was the problem all along !!.

     = if('Table1'[Field1]='Table1'[Field2],"OK","Check - "&'Table1'[Field1]&" vs "&'Table1'[Field2])


    Thanks for all your time and patience. As always, much appreciated.

6 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous 

     

    What error did you get? It is better to provide some sample data...if it is a measure, you can't call a field directly... 

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous 

       

      So it is a column, you missed () for the Text.From, but your error was not about it, the question is how you put your if statement? Did you add a custom column in Power Query Editor then put it like this?

       

      if [#"Trunks & Radials (RPE).RPE Plan"]=[#"Loads (Driver Vehicle).Planned_Total_RPE"] then "OK"
      else "Check - "&Text.From([#"Trunks & Radials (RPE).RPE Plan"])&" vs "&Text.From([#"Loads (Driver Vehicle).Planned_Total_RPE"])

       

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi there Vera

    I figured out that I should be doing this in query (which I have made progress with however I still can't get my formula to concatenate data with text.
    My formula is fine until I try to concatenate:


    if [#"Trunks & Radials (RPE).RPE Plan"]=[#"Loads (Driver Vehicle).Planned_Total_RPE"] then "OK"
    else "Check - "&Text.From[#"Trunks & Radials (RPE).RPE Plan"]&" vs "&Text.From[#"Loads (Driver Vehicle).Planned_Total_RPE"]

     

    The "OK" is present where the condition is met but there is an error where the condition is not met:

     

    (If I use text ("OK" and "Not OK") for both then it works, however I need to concatenate data to create information for the viewer.)

     

    The error message states:

    Expression.Error: We cannot apply field access to the type Function.
    Details:
    Value=[Function]
    Key=Trunks & Radials (RPE).RPE Plan

     

    Thanks for your help and patience in advance.

  • Anonymous's avatar
    Anonymous
    Not applicable

    You don't need all the parentheses. Just try this:

     

    Measure = if [Field1] = 0 then "OK" else "Error -"& [Field2]

     

    --Nate

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Nate
    I tried your suggestion with a test using the following:
    if [Field1] = "OK" then "OK" else "Error -"& ([Field2])

    Field1 is a calculation returning text "OK"

    Field2 is a numerical value

     

    This still has an orange "error" where the condition is not met with the Expression error:

    Expression.Error: We cannot apply operator & to types Text and Number.

    So then I tried to concatenate like so:

     

    if [Field1] = "OK" then "OK" else "Error -"&Number.ToText([Field2])

     

    Now I get "Error -1" but it isn't yellow and does not link to an explanation.

     

    Any ideas?

     

     

  • Anonymous's avatar
    Anonymous
    Not applicable

     figured this out. I created a new column within the data environment and used the following syntax (which I guess was the problem all along !!.

     = if('Table1'[Field1]='Table1'[Field2],"OK","Check - "&'Table1'[Field1]&" vs "&'Table1'[Field2])


    Thanks for all your time and patience. As always, much appreciated.