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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Anonymous
Not applicable

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?
1 ACCEPTED SOLUTION
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.

View solution in original post

6 REPLIES 6
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.
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?

 

 

watkinnc
Super User
Super User

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

 

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

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
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.

Vera_33
Resident Rockstar
Resident Rockstar

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... 

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"])

 

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors