Power BI Functions — (List.Contains, List.ContainsAny, List.ContainsAll) | by Peter Hui | Towards Data Science

...usefully tools for your data journey.
This article will introduce you to the Contains functions with lists. It’s a great function to use in Power Query. There are little nuances for each but I hope this article will help you get started.
Lists in Power Query are written with {} brackets. They can be {1,2,3} or even {1, “a”,123}. If you like to experiment, you can go to Power Query >Create a Blank Query > Advanced Editor > Replace the source information with > Source = {1,”hello”,123}.
It’s important to remember this — columns in Power query can be wrapped in {[Column A]} to return a list so you can use list functions. You can also refer to Table[Column A] to return a list as well.
There are differences in both though — creating a new column using { [ID] } in the data set below returns a single value per “cell”. While using #”Previous Step”[ID] returns an entire list nested in a “cell”.
Before you get too deep into it, check out how to build a Power BI data model in this article. Models in Power BI are a little different than Access and SQL databases.
If you want to know how to apply functions in Power BI — here is a good article.
Now back to it, why are these list functions helpful at all? It’s helpful for situations when you want to put a list through various columns in your data set to check for conditions.
Sure, you can probably use a conditional column ribbon on Power Query, but it will be very tedious to list out all the particular items you wish to check.
You can also probably do various joins with supporting tables but it will also be tedious to clean up after. You can write if statements, but what if your list is quite big?
Let’s go through the functions together.
List.ContainsAny — Here is the breakdown of the function.
Find out if the list {1, 2, 3, 4, 5} contains 3 or 4.
List.ContainsAny({1, 2, 3, 4, 5}, {3, 4})
Returns TRUE — since any 3 or 4 are in the list from {1,2,3,4,5}
List.ContainsAny({"A", "B", "C", "D", "E"}, {"F", "G"})
Returns FALSE — “F” or “G” are not in the list of {“A”, “B”, “C”, “D”, “E”}
List.Contains — here is the breakdown of the function.
Find if the list {1, 2, 3, 4, 5} contains 3. Here the function will be
List.Contains({1, 2, 3, 4, 5}, 3)
Returns TRUE
However, you may need to be careful about the item you watch to check against. There isn’t a {} for the item you want to check. The last value being compared against is expects a value and not a list so the below returns false.
List.Contains({1, 2, 3, 4, 5}, {3})
Returns FALSE.
Now instead of a single value — List.ContainsAll will return True if it is able to match ALL the items on the list against the list you want to compare against.
List.ContainsAll({1, 2, 3, 4, 5}, {4, 5})
Returns TRUE— both 4 and 5 are on the list.
Since this function requires a list to match and because of the row by row lay out in Power Query, you will need to nest this list first for this to work. (More on this below)
Let’s walk through an example.
I have a table here in power query and I will use the add columns feature to add in additional columns to use our contain functions.
Let’s add the first column to use List.Contains using the add columns button.
The result will be this column here.
Let’s create our first column. I want to check if the Store_Number 1 is anywhere in the column.
Here the column indicates that there is a store number 1 in that column of store number.
I also want to check for if there is a store number 1 and also, any reviews contains the word “Great”.
Final column, I want to check if all the Review criteria I have — {“Great”,”Good”,”Meh”} — are represented in the column.
This particular one — List.ContainsAll is a little bit different than the others.
If you are doing a custom column just like the others, it wouldn’t work because of the row context in Power Query.
Intuitively if you tried doing —
List.ContainsAll({[Reviews]},{“Great”,”Good”,”Meh”})
Will actually return FALSE, even though the items you have listed here are all represented in the column.
The issue here is the row — if you just refer to the column, Power Query takes the “cell” of the column and checks it against the list of {“Great”,”Good”,”Meh”} and returns FALSE. That particular “cell” does not contain all the items of that list so it return FALSE.
When we had used List.ContainsAny, List.Contains, they returned the right value because even if it is doing a check by each value it will still get us what we want. The difference here is we want the entire column to be nested in a “cell” before comparing to the list.
This is why we need to write.
List.ContainsAll( #PreviousStep[Reviews],{“Great”, ”Good”, “Meh”} )
Here it is referring the entire column as a list — to check against another list.
The result will return TRUE
As a bonus, these two functions are very helpful as well — List.AnyTrue and List.AllTrue.
They are very much like the others as well and you can add in other conditions instead of just checking for membership.
The great part about these functions — you can string them together with the and, or operators and create checks on your data without having to revert to joins and multiple nested statements.
TL:DR? Here is a summary for all of them —
List.Contains — Remember to exclude the {} for the search item.
List.ContainsAll — Remember to use the previous step [ column name] for the search list.
List.ContainsAny — Easiest to use. I’ve always had to use this particular one.
List.AnyTrue, List.AllTrue — You can add in conditions to them. Whole condition statement needs to be wrapped in {}
Have fun trying some of these functions in Power Query! Stay safe!