Forum Discussion

jk8979356's avatar
jk8979356
Regular Visitor
5 years ago
Solved

What is the code for a multiple condition, nested IF statement?

I need to create a multiple condition nested multiple IF statement to identify matches and then add text in a new conditional column. Their are two columns: [Column.Family] [Column.Item] and the logi...
  • v-yingjl's avatar
    5 years ago

    Hi jk8979356 ,

    You can add a custom column in power query:

    let
        Source = ...,
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Family", type text}, {"Item", type text}}),
        #"Added Custom" = 
            Table.AddColumn(
                #"Changed Type", "Custom",
                each if ([Family] = "Blue" or [Family] = "Black" or [Family] = "White" or [Family] = "Red") and Text.Start([Item],10) = "Men Jeans" 
                    then "Men" 
                else if 
                        ([Family] = "Blue" or [Family] = "Black" or [Family] = "White" or [Family] = "Red") and Text.Start([Item],13) = "Womens Shirts" 
                    then "Womens" 
                else if 
                        ([Family] = "Blue" or [Family] = "Black" or [Family] = "White" or [Family] = "Red") and Text.Start([Item],16) = "Youth Boys Shoes" 
                    then "Youth Boys" 
                else if 
                        ([Family] = "Small" or [Family] = "Medium" or [Family] = "Large" or [Family] = "XL") and Text.Start([Item],17) = "Football Uniforms" 
                    then "Football"
                else [Item])
    in
        #"Added Custom"

    Attached a sample file in the below, hopes to help you.

    In addition, here are some articles about nested if statements in power query that you can refer:

    1. IF Functions in Power Query Including Nested IFS 
    2. Logical Operators and Nested IFs in Power BI / Power Query 
    3. Power Query if Statements 

     

    Best Regards,
    Yingjie Li

    If this post helps then please consider Accept it as the solution to help the other members find it more quickly.