cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
pam259
Frequent Visitor

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

@pam259 , 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



Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
Dashboard of My Blogs !! YouTube Channel !! Connect on Linkedin

Proud to be a Super User!

Pragati11
Super User
Super User

HI @pam259 ,

 

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

 

Thanks,

Pragati


My Blog: Data Vibe
Connect: LinkedIn
Connect: Twitter






If this helps, Appreciate a KUDOS!
Did I answer your question? Mark my post as a solution!


Proud to be a Super User!




Greg_Deckler
Super User
Super User

@pam259 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!!!
Check out my External Tool for Power BI Desktop!
Microsoft Hates Greg's Quick Measures
Check out my latest book!

Sujit_Thakur
Solution Sage
Solution Sage

Dear @pam259 

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

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

@pam259 

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

 

Keep learning my friend 

 

Dear @pam259 

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

 

 

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

@pam259 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






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
PBI_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power BI October Update 2021.jpg

Power BI Release

Click here to read more about the October 2021 Release!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Teds Dev Camp Oct. 2021 768x460.jpg

Power BI Dev Camp - October 28th, 2021

Mark your calendars and join us for our next Power BI Dev Camp!