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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
UnearthlyFalcon
Advocate I
Advocate I

New Conditional Column from Two Other Columns in M

Hi all,

 

I have a table like below. 

 

IDPet TypeNumber Category

Unique 1

Cat1
Unique 2Dog2
Unique 3Cat3
Unique 4Dog3
Unique 5Cat2
Unique 6Dog1

 

I would like to create a new column where any dog that is in number category 3 means they were adopted but cats just remain as cats, as do pets in the other nuber categories. Sorry, this is not a great example logically. A table below is what I would like the end result to be:

 

IDPet TypeNumber CategoryNew Column

Unique 1

Cat1Cat
Unique 2Dog2Dog
Unique 3Cat3Cat
Unique 4Dog3Adopted
Unique 5Cat2Cat
Unique 6Dog1Dog

 

I would like to make this new column in M rather than Dax (I think). I have seen examples with the switch function but realize that does not work in M and I'm not sure how to nest my IF statements accordingly. I tried the below but my syntax was wrong, as the ANDs are not right because it expects THEN

 

#"Added Conditional Column2" = Table.AddColumn(#"Reordered Columns", "New Column", each if Text.Contains([Pet Type], "Cat") AND each if Text.Contains([Number Category], "1") then "Cat" else if Text.Contains([Pet Type], "Dog") AND each if Text.Contains([Number Category], "1") then "Dog" else if Text.Contains([Pet Type], "Cat") AND each if Text.Contains([Number Category], "2") then "Cat" else if Text.Contains([Pet Type], "Dog") AND each if Text.Contains([Number Category], "2") then "Dog" else if Text.Contains([Pet Type], "Cat") AND each if Text.Contains([Number Category], "3") then "Cat" else if Text.Contains([Pet Type], "Dog") AND each if Text.Contains([Number Category], "3") then "Adopted"

 

Thank you in advance!

1 ACCEPTED SOLUTION
jgeddes
Super User
Super User

This should solve your issue. 

= Table.AddColumn(previousStep, "ConditionalColumn", each if [Number Category] = 3 and [Pet Type] = "Dog" then "Adopted" else [Pet Type], type text)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

7 REPLIES 7
jgeddes
Super User
Super User

This should solve your issue. 

= Table.AddColumn(previousStep, "ConditionalColumn", each if [Number Category] = 3 and [Pet Type] = "Dog" then "Adopted" else [Pet Type], type text)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





@jgeddes  thank you for the reply! It did not quite work. It did create a new column and replicated the text from the pet type column, however, the new "Adopted" title is not incorporated for some reason. All the orange in the below graph meets the criteria for "dog" and "3" but it still has the old tag of "dog".

UnearthlyFalcon_0-1770740881291.png

 

If the code provided gave you a column result similar to 

jgeddes_0-1770742649518.png

We can assume that the M code worked as intended. 
Can you confirm that you are using the new "conditional column" in the chart you shared?

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





@jgeddes Yes, I removed the old column and added the new "conditional column" to the graph. I also inserted a new table visual to verify and the "adopted" is not there, it is "dog". I'm not sure what is wrong. In my data, the pet type is another conditional column, but it is earlier in the applied steps. Is this throwing it off?

An earlier conditional step should not matter as long as the conditional column we created references the immediate previous step. 
If the "Conditional Column" is loaded into the report, and you can view the correct data in the table view, I cannot explain why you are getting the result you are. 
 I will share my pbix file to see if you can compare between and find the issue.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





I needed to change the column type to text for the original two columns, that fixed it!

I can't see the correct data in the table view either, neither in a visual or the power querry. My data is very large though and power querry can't load enough of it to preview what I need to see. I tried to filter by "3" in the category column but it doesn't even think a 3 (or a 2 for that matter) exiss because of the data quantity. 

 

Regardless, I appreciate your help! My code is exactly what you provided. I'll try to keep playing around with different things to see if I can get it to work. I'll go ahead aand mark your answer as the solution, because it should be working for this type of situation haha. Thank you!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.