Written By Rick de Groot

Rick is the founder of BI Gorilla. He believes learning is one of life's greatest pleasures and shares his knowledge to help you improve your skills.

Are you trying to use the IN operator in Power Query but don’t know how? While the IN operator isn’t built into Power Query, you can still achieve the same functionality using other methods. This article will show you how to use the IN operator in Power Query.

The Need for the IN Operator

When working with Power Query, especially when creating conditional if statements, you often need to check if a value matches one of many possible options. Typically, this is done using multiple OR operators.

if [Month] = "January" or [Month] = "February" or [Month] = "March" 
   then "Q1"
   else "Remainder"

While this approach works, it can become cumbersome and hard to maintain as the number of options increases. Imagine if you had to check for 11 or more months. The code would be lengthy and prone to errors.

In many programming languages, the IN operator simplifies this process. The IN operator allows you to check if a value exists within a list, essentially serving as a shorthand for multiple OR statements. This can be especially helpful when you want to categorize data based on a predefined set of values. Unfortunately, Power Query doesn’t have a built-in IN operator, but we can replicate its functionality using other methods.

The IN Operator in Power Query

Example Scenario: Categorizing Products in Power Query

Let’s consider a practical example to understand the need for the IN operator better. Suppose you own a company that sells clothes and you currently offer a discount on Jeans, Skirts, and Tights. You want to categorize these products as “Sale” items and all other products as “Regular” items.

Without the IN operator, you would write the conditional statement like this:

if [Product] = "Jeans" or [Product] = "Skirts" or [Product] = "Tights" 
   then "Sale"
   else "Regular"
Elaborate conditional if statement in Power Query

This statement is straightforward, but it becomes increasingly complex and difficult to manage as the list of products grows. Each new product you want to include in the “Sale” category requires an additional OR condition, making the code lengthy and repetitive.

To make your code more concise and maintainable, you can use the List.Contains function in Power Query to replicate the IN operator. Here’s how you can achieve the same result without the repetitive OR statements:

if List.Contains( { "Jeans", "Skirts", "Tights" }, [Product] ) 
   then "Sale"
   else "Regular"
In operator in Power Query

Benefits of Using List.Contains

Using List.Contains instead of multiple OR operators offers several advantages:

  • Conciseness: Your code is shorter and easier to read.
  • Maintenance: Updating the list of products is straightforward. You only need to change the values within the List.Contains function.
  • Clarity: The purpose of your code is clearer. It explicitly shows that you are checking if a value exists within a predefined list.

Tip

The above example uses only three products, but you can expand this list with as many values as you need. You can even reference a column that contains the values to check against, making your code dynamic to changes in your data.

Combining Multiple Conditions with List.Contains

You can also combine conditions to check for multiple OR conditions at once. For instance, if you want to categorize products that are on sale only if they match both a specific product type and color, you can use a construct like this:

if List.Contains( { "Jeans", "Skirts", "Tights" }, [Product] )
     and List.Contains( { "Blue", "Red", "Yellow" }, [Color] )
   then "Sale"
   else "Regular"
Multiple In Operators in Power Query

In this example:

  • List.Contains( { "Jeans", "Skirts", "Tights" }, [Product] ) checks if the product is one of the specified types.
  • List.Contains( { "Blue", "Red", "Yellow" }, [Color] ) checks if the color is one of the specified colors.
  • The combined condition and ensures that both criteria must be met for the product to be categorized as “Sale”.

Excluding Specific Items with the Not Operator

To mark all products except the ones in your list as “Sale,” you can reverse the condition using the not operator. This is useful when you want to exclude certain products from the sale.

if not List.Contains( { "Jeans", "Skirts", "Tights" }, [Product] ) 
   then "Sale"
   else "Regular"

Here’s how it works:

  • List.Contains( { "Jeans", "Skirts", "Tights" }, [Product] ) checks if the product is in the specified list.
  • not inverts the check, so the statement categorizes as “Sale” any product not in the list.

The IN Operator with Multiple Columns

In Power Query, you can replicate the IN operator for multiple columns. This is useful when you need to check if a combination of values exists in a table.

Using List.Contains for Multiple Columns

To check for combinations of values using lists, you can apply the following approach:

List.Contains(
  { {"Jeans", "Blue"},
    {"Skirt", "Yellow"} },
    { [Product], [Color] }  // Turns Columns into list
)

In this code:

  • The second argument contains a list with the two required column values.
  • This list is compared with a list of inner lists specified in List.Contains‘ first argument.
  • This allows you to compare multiple columns at once, checking if the combination of [Product] and [Color] matches any of the combinations in the list.

Using Table.Contains for Multiple Columns

You can also achieve this using tables. In that case you would use the Table.Contains function as follows:

Table.Contains(
    #table(                       // creates table
       {"Product", "Color"},      // with column names
       { {"Jeans", "Blue"},       // values 1st row
         {"Skirt", "Yellow"} } ), // values 2nd row
    [[Product], [Color]]          // checks if values exist in table
  )

In this approach:

  • The #table function creates a table with specified column names and values.
  • Table.Contains then checks if the combination of [Product] and [Color] exists in the table.

Using Table.FromRecords for Multiple Columns

This is very similar to using the below construct that stores the values within a record and transforms them into a table using Table.FromRecords:

Table.Contains(
     Table.FromRecords(
         { [Product = "Jeans", Color = "Blue"],
           [Product = "Skirt", Color = "Yellow"] },
         type table [Product = Text.Type, Color = Text.Type]
     ),
  [[Product], [Color]]
 )

Here’s how it works:

  • Table.FromRecords creates a table from a list of records.
  • Each record represents a combination of values for [Product] and [Color].
  • Table.Contains checks if the combination of [Product] and [Color] exists in this table.

These were but three examples. There are several other ways to construct tables that result in similar outcomes. If this intrigued you, makes sure to read the deep dive on constructing table values in Power Query.

Conclusion

By understanding how to replicate the IN operator in Power Query, you can significantly improve your code. Whether you’re working with single columns, multiple columns, or complex combinations of conditions, using functions like List.Contains and Table.Contains can make your code more efficient, readable, and maintainable.

This not only helps you handle more complex conditions but also makes sure your Power Query scripts are clean and easy to manage.

Happy querying!

Share this post:
  1. I’m getting an error that says I’m passing 4 arguments for a function that expects 2 or 3, and that kind of defeats the purpose of having this function. Has anyone else had this happen?

    Reply
  2. Hi Rick,
    brilliant explanation. Thank you. I have a question that puzzles me. If I try to make a list from a selection of values in a column and I call this in a prior step, say, ClothesList, when I then insert this list into the code below like this:

    if List.Contains( ClothesList, [Product] ) 
       then "Sale"
       else "Regular"

    I get an error to the tune that it cannot find a list argument in a list. The reasons for this approach is that I would like to use a List.Select type of step to make my list dynamic and not hard coded. Any light you may shed on this point, is very useful. Thank you again for putting together this treasure trove of M wisdom

    Reply
  3. Thanks a lot, this was explained very clearly and I managed to do what I was trying to achieve for the last few hours in just a few minutes!

    Reply
  4. Hi Rick,
    1-During the introduction in your statement you mentioned that the List.Contains function “…[Product] column equals one of the values in the list” (emphasizing the equals). shouldn’t that be “…contains…” or “…is in…”?
    2-how does actually the List.Contains work: it checks whether the 1st argument is in the 2nd argument or checks whether the 2nd argument is in the 1st argument? because as I read on ms.com documentation, it reads the oppposite of what you explained in the video. by the way, does the order matter?

    Reply
    • Erivaldo – Thanks for asking, your questions are very relevant. Let’s delve in.

      1. The List.Contains function takes a list as first argument. In the example of the post I wrote:

      if List.Contains( { "Jeans", "Skirts", "Tights" }, [Product] ) 
         then "Sale"
         else "Regular"

      The formula refers to the column called [Product]. This formula is executed for each line in Power Query. That means that the Product column (2nd argument) in my example contains a single value for each execution.

      Each line the formula checks whether the value in the product column is available in the manual list provided in the 1st argument. The List.Contains function performs the comparison and checks whether the value is equal.

      That means:

      = List.Contains( { "Jeans", "Skirts", "Tights" }, "Jeans" ) // Returns true
      = List.Contains( { "Jeans", "Skirts", "Tights" }, "Jea" )   // Returns false 

      So by default the function does not check for a substring, but the term ‘Contains’ within the function name can confuse here.

      2. So how does List.Contains work? It checks whether the value in argument 2 is available in the list of argument. That’s also in line with what Microsoft writes :
      “… Indicates whether the list contains the value. “

      Reply
    • Agba – this function folds as many times as you like. I have written extensive case-when statements using the function at least 15 times in one statement.

      I did notice that folding breaks once you have the List.Contains function check for null values, next to for example text values. So keep an eye out for that one.

      = List.Contains( {"A", "B" },       [Column1] ) // Formula folds
      = List.Contains( {"A", "B", null }, [Column1] ) // Does not fold
      
      // Formula folds
      = List.Contains( {"A", "B" }, [Column1] ) or [Column1] = null
      Reply

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.