CONTAINS
Whether a table has a row with the given values in the given columns.
Group: Information. Compatible with DAX.
Syntax
CONTAINS ( <table>, <columnName>, <value> [, <columnName>, <value>] … )
| Parameter | Type | Description |
|---|---|---|
table | Table | The rows to search: a table of the model or a table expression. |
columnName | Column repeatable | A column of the table to look in, or of a table it looks up through a relationship. |
value | Value repeatable | What the column before it must hold. |
Returns
Boolean. TRUE when one row at least holds each value in its column, FALSE otherwise.
Remarks
- Columns and values come in pairs, and every pair must match on the same row: a row with IPA and another with 80 liters do not make a row with both.
- The match is strict, as for
CONTAINSROWand theINoperator: a blank finds only a blank, never 0 or empty text. Text is matched without regard to upper and lower case. - A value of a type its column cannot be compared with, such as the text "80" for a column of whole numbers, is an error. Turn it into the column's type with
VALUEorFORMATfirst. - Over an empty table the answer is FALSE.
- A column of a related table on the one side counts as the row's own:
CONTAINS ( Sales, Products[Category], "Cider" )asks whether a row of Sales is for a cider (example 3). A table without such a relationship, or a table made byVALUESorSUMMARIZE, is an error with such a column. - For a single column,
CONTAINSROWorINreads more simply. To filter one table by the values another table holds,TREATASis usually the better tool.
Examples
One value, then two on the same row
IPA is there with 80 liters, not with 120.
EVALUATE
VAR Orders =
DATATABLE (
"Product", STRING, "Liters", INTEGER,
{ { "Pils", 120 }, { "IPA", 80 }, { "Stout", 45 } }
)
RETURN
ROW (
"IPA", CONTAINS ( Orders, [Product], "IPA" ),
"Cider", CONTAINS ( Orders, [Product], "Cider" ),
"IPA with 80", CONTAINS ( Orders, [Product], "IPA", [Liters], 80 ),
"IPA with 120", CONTAINS ( Orders, [Product], "IPA", [Liters], 120 )
)
| IPA | Cider | IPA with 80 | IPA with 120 |
|---|---|---|---|
| TRUE | FALSE | TRUE | FALSE |
Blanks and case
The Stout line has no liters: a blank finds it, a zero does not. Lower-case pils finds Pils.
EVALUATE
VAR Orders =
DATATABLE (
"Product", STRING, "Liters", INTEGER,
{ { "Pils", 120 }, { "IPA", 80 }, { "Stout", BLANK () } }
)
RETURN
ROW (
"Blank liters", CONTAINS ( Orders, [Liters], BLANK () ),
"Zero liters", CONTAINS ( Orders, [Liters], 0 ),
"Lower case", CONTAINS ( Orders, [Product], "pils" )
)
| Blank liters | Zero liters | Lower case |
|---|---|---|
| TRUE | FALSE | TRUE |
Did a shop buy a product?
The category is a column of Products, which Sales looks up.
EVALUATE
ROW (
"Pils at Frogner", CONTAINS ( Sales, Sales[Product], "Pils", Sales[Shop], "Shop Frogner" ),
"Any Stout", CONTAINS ( Sales, Sales[Product], "Stout" ),
"Any cider", CONTAINS ( Sales, Products[Category], "Cider" )
)
| Pils at Frogner | Any Stout | Any cider |
|---|---|---|
| TRUE | FALSE | TRUE |