Power Query Table.Contains: Step-by-Step Guide
In this article
Table.Contains is a Power Query M function that tells you whether a specific record (a row) exists in a table. It returns true or false, and you use it inside a custom column, a filter, or another expression when you need to check for a matching row rather than just a matching value in one column. To use it, you need Power Query in Excel or Power BI, a table to search, and a record that describes the row you are looking for.
What You'll Need
- Excel (Microsoft 365 or Excel 2019+) or Power BI Desktop with Power Query available.
- A table loaded into Power Query. This can be an Excel table, a CSV, a database query, or any other source you have already connected to.
- A clear idea of which columns and values make a row unique, because Table.Contains compares entire records, not single cells.
Step-by-Step
- Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI, use Home > Transform data to open the Power Query Editor. Menu labels can differ slightly by version, device or region.
- Decide what a matching row looks like. Table.Contains checks whether a record exists in a table. A record is a set of field-name and value pairs, so you must specify every column you want to match. If you only care about one column, you can build a record with just that field, but the function still compares the whole record against each row.
- Open the Advanced Editor or add a custom column. For a quick test, go to Home > Advanced Editor and add a step that calls Table.Contains. For a reusable check per row, go to Add Column > Custom Column and write the expression there.
- Write the basic expression. The syntax is
Table.Contains(table as table, row as record, optional equationCriteria as any) as logical. A simple example:Table.Contains(MyTable, [CustomerID = 123, Status = "Active"]). This returns true if any row in MyTable has CustomerID 123 and Status "Active". - Use the optional equationCriteria when needed. By default, Table.Contains compares values using standard equality. If you need case-insensitive matching or a custom comparer, pass an equationCriteria argument, such as
Comparer.OrdinalIgnoreCaseor a custom comparer function. For most text checks,Comparer.OrdinalIgnoreCaseis the practical choice. - Apply the result. If you used Advanced Editor, the step returns a single true/false value you can use elsewhere. If you used a custom column, each row gets its own true/false result, which you can then filter or use in further logic. For example, filter the new column to keep only true rows.
- Check your column names and types. Record field names must match the table's column names exactly, including spaces and capitalization. If a column is named "Customer ID" with a space, write
[#"Customer ID" = 123]. Mismatched names cause an error or an always-false result. - Close and load. When the query returns the result you expect, go to Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to send the data back to your workbook or model.
Common Mistakes
- Passing a single value instead of a record. Table.Contains expects a record like
[Column = value], not a bare value like123. A bare value will raise an error. - Forgetting that all specified fields must match. If you include two fields in the record, a row must match both. Leaving out a field means that field is not checked, which can produce unexpected true results.
- Using the wrong column name. Field names are case-sensitive in records. "status" and "Status" are different. Use the exact name shown in the table header.
- Expecting a row number or position. Table.Contains only returns true or false. If you need the matching row, use Table.SelectRows or Table.PositionOf instead.
- Ignoring data types. A number 123 and a text "123" are not equal by default. Make sure the record value type matches the column type.
Tips
- For a single-column check, consider List.Contains instead of Table.Contains. It is simpler and faster when you only need to know whether a value exists in one column.
- Use
Comparer.OrdinalIgnoreCaseas equationCriteria when matching text that may vary in capitalization. - Test your expression on a small table first. A wrong record shape can be hard to spot in a large dataset.
- If you need to check many rows against a reference table, Table.Contains inside a custom column works, but it can be slow on large tables. For better performance, consider merging queries instead.
- Remember that Table.Contains is case-sensitive by default for text. If case does not matter, always add the comparer.
Troubleshooting
If Table.Contains returns false when you expect true, check the record field names against the actual column headers. A trailing space or different capitalization is enough to break the match. Next, confirm the value types: a text value will not match a number column. If you get an error like "We cannot convert a value of type Text to type Record", you passed a single value instead of a record; wrap it in square brackets with a field name. If the function is not recognized, make sure you are in the Power Query Editor and not in a regular Excel formula bar. For case-insensitive matching that still fails, verify the comparer argument is placed correctly as the third parameter. If performance is poor on large tables, reduce the columns in the record to only the key fields, or switch to a merge operation.
Frequently Asked Questions
What is the difference between Table.Contains and List.Contains?
Table.Contains checks for a whole record (a row with specified field values) in a table. List.Contains checks for a single value in a list, which is usually one column. Use List.Contains when you only need to test one column.
Can Table.Contains match on multiple columns?
Yes. Include multiple fields in the record, for example [CustomerID = 123, Status = "Active"]. The function returns true only if a row matches all specified fields.
Is Table.Contains case-sensitive?
By default, text comparison is case-sensitive. Pass Comparer.OrdinalIgnoreCase as the third argument to make it case-insensitive.
Why does Table.Contains return false for a value I can see in the table?
Common causes are mismatched column names, different data types (text vs number), or extra spaces in the value. Check the exact field name and type in the table.
Can I use Table.Contains to return the matching row instead of true/false?
No. Table.Contains only returns a logical value. Use Table.SelectRows to filter to matching rows, or Table.PositionOf to find the row position.
Does Table.Contains work in both Excel and Power BI?
Yes, it is part of the Power Query M language and works the same in Excel and Power BI Desktop, though the surrounding interface labels may differ slightly.