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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

If statement with string variable not working

Hello,

 

I have data from Power Bi dataset, I need to create a measure using if then else for a string variable"regions". I basically need to create a measure(since I cannot create a new column) which would combine some regions and rename some regions. Both if and switch statements are unable to identify by col("Cannot find name"). I think I need to summaries it.

 

Just for testing, I used count for this field, powerbi identifies it but I need something to work on text fields. 

 

9 REPLIES 9
amitchandak
Super User
Super User

@Anonymous , you need to use min , max or firstnonblank or lastnonblank or dimension value

example masure

color =
switch ( true(),
FIRSTNONBLANK(Table[commodity],"NA") ="commodity1" && sum(Table[Value]) >500,"lightgreen",
FIRSTNONBLANK(Table[commodity],"NA") ="commodity2" && sum(Table[Value]) >1000,"lightgreen",
/// Add more conditions
"red"
)

 

measure =
switch ( true(),
max(Table[region]) ="R1" , [Mea1],
max(Table[region]) ="R2", [Mea2],
/// Add more conditions
[Mea3]
)

 

also refer

https://www.daxpatterns.com/dynamic-segmentation/
https://radacad.com/grouping-and-binning-step-towards-better-data-visualization

Pragati11
Super User
Super User

HI @Anonymous ,

 

Not able to understand your query. Please share sample data along with expected output to understand your question here.

 

Thanks,

Pragati

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

Greg_Deckler
Super User
Super User

@Anonymous If I understand correctly, use MAX or MIN as your aggregation. Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...
Sujit_Thakur
Solution Sage
Solution Sage

Dear @Anonymous 

Can you share some dummy data (3-4 rows ) and expected output

,You can use excel to show this , and just paste that things from excel in post section.

I will write and send you measure 👍

 

Regards

Thakur Sujit

Anonymous
Not applicable

Have field, "productname" values as ABC, AB,AA, etc. Need to convert values in a new measure like ABC=AB1, AB=AB2. Trying to use switch or IF, but field "productname" is not being identified

@Anonymous 

For row context and filter context understanding = https://youtu.be/xV-KwuSDFMQ

 

Keep learning my friend 

 

Dear @Anonymous 

You are having such issues because you didn't have included Aggregation function before your field . 

I would suggest you , if you want to change values like ABC=AB1, AB=AB2.

 

Use a calculated column, it will be more easy and helpful .

Use this 

 

Column = Switch (

True(),

[Product name] = "ABC" , "AB1",

[Product name] = "AB" ,"AB2",

"Else condition value "

)

 

Or if you are adhered using measure let me tell you my friend , for using any field in measure you must use aggregation function before it to tell power bi that from which rows you want calculation to happen.

 

But as column works  most of the time in row context hence no aggregation function will be required in calculated columns.

 

I hope you understand .

 

If any doubt let me know .

 

I do expect kudos to keep me motivated !!

 

If this post helped you to solve your problem then please accept it as solution.

 

Regards 

Thakur Sujit

 

 

Anonymous
Not applicable

Thankyou so much.  I have data from powerbi datasets so I cannot create any new columns. What aggregation should I use for this string variable?

parry2k
Super User
Super User

@Anonymous very confusing what you are trying to do here. You need to provide some sample data and expected output. Read this post to get your answer quickly.

https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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