Forum Discussion

UnearthlyFalcon's avatar
UnearthlyFalcon
Advocate I
6 months ago
Solved

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!

  • 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)

7 Replies

  • 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)
    • UnearthlyFalcon's avatar
      UnearthlyFalcon
      Advocate I

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

       

      • jgeddes's avatar
        jgeddes
        Super User

        If the code provided gave you a column result similar to 

        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?