Power Query Not Operator: How to Use "not" in M Code
In this article
The not operator in Power Query (the M language) is a logical operator that flips a true/false result: not true returns false, not false returns true. It works on any expression that evaluates to a logical value, so it is most useful in a custom column or a filter step such as not Text.Contains([Email], "@test.com"). In practice, you will mostly use it to reverse a condition you have already written, or to filter out rows that match something instead of keeping them. There is no single "NOT filter" button in the Power Query Editor — you either untick values in a column's filter dropdown, use the column's filter menu with a numeric or text condition plus "Does not equal", or write the expression in M yourself.
The same logic applies whether you are in Excel's Power Query Editor (Data > Get Data) or in Power BI Desktop's Power Query Editor (Home > Transform data), and menu labels can differ slightly depending on your version, region or product.
What You'll Need
- Excel for Microsoft 365, Excel 2016 or later, or Power BI Desktop, with Power Query available (in Excel: Data tab > Get Data).
- A table, CSV, database query or other source already loaded into the Power Query Editor with at least one column you want to test.
- Basic familiarity with the formula bar and the Applied Steps list, since you will edit M code directly for the custom-column method.
Step-by-Step
- Load your data into the Power Query Editor. In Excel, select your table and choose Data > From Table/Range (or Data > Get Data for an external source). In Power BI Desktop, choose Home > Transform data and pick your source. The editor opens with your columns and a list of Applied Steps on the right.
- Start with the simplest method — filtering by value. Click the filter arrow in the header of the column you want to test. Untick the values you want to exclude, then choose OK. The editor writes the filter for you and you never have to type
not. This is the right approach for one-off exclusions such as dropping a handful of product codes. - Use the column's text or number filter for "does not equal". Click the filter arrow and choose Text Filters > Does Not Equal (or Number Filters > Does Not Equal, depending on the column type). Type the value to exclude. This creates a filter step in M using the not-equal operator,
<>, which is the practical equivalent of a NOT for a single value. - Add a custom column when the condition is more complex. On the Add Column tab, choose Custom Column. Give it a name such as "Exclude Flag", and in the formula box write a logical expression, for example
not Text.Contains([Email], "@internal.com"). This returns true for every row that does not contain that text and false for the ones that do. - Filter the new column to keep only true. Click the filter arrow on your new column and untick false (or any error values). You now have the equivalent of a NOT filter driven by a full expression rather than a single value.
- Combine NOT with AND and OR carefully. In M, use
andandoras the boolean operators, not&&or||. Writenot ([Status] = "Closed" or [Status] = "Cancelled")to exclude both statuses. Note thatnotbinds to the whole bracketed expression, which is what you usually want; writenot [Status] = "Closed" and not [Status] = "Cancelled"if you prefer to negate each test separately. - Edit the M directly for full control. Select a step in Applied Steps and look at the formula bar. A filter step appears as something like
Table.SelectRows(Source, each [Status] <> "Closed"). You can typenothere, for exampleTable.SelectRows(Source, each not Text.StartsWith([Code], "X")). This keeps the logic in one clean step instead of adding a helper column. - Check the result. The column header shows the step's output; scroll through and confirm the excluded rows are gone and the rest are intact. If the count of rows looks wrong, click the previous step in Applied Steps to compare before and after.
- Apply and close. In Excel choose Home > Close & Load; in Power BI choose Home > Close & Apply. The transformation is saved into the query so it reapplies every time the data refreshes.
Common Mistakes
- Using
!=instead of<>. In M the not-equal operator is<>;!=is not valid and the editor will complain. - Expecting
notto skip nulls automatically.not nulldoes not return false in the way many people expect; comparisons against null often yield null, which a filter treats as false and quietly drops. Filter nulls explicitly first if you want to keep them. - Forgetting that text comparisons are case-sensitive by default.
Text.Containsaccepts an optional comparer, so if you need case-insensitive matching, useComparer.OrdinalIgnoreCaseas the third argument. - Negating the wrong scope.
not [A] = 1 and [B] = 2is read as(not [A] = 1) and [B] = 2, not as "not both". Bracket the whole condition when that is your intent. - Typing
notat the wrong level. It must appear inside theeach ...function inTable.SelectRows, where it has access to the row's fields. - Hard-coding values that change. If you filter out one email domain today, consider referencing a parameter or list of values so the rule is easy to maintain.
Tips
- Let the editor write most of your filters. Untick or use the Does Not Equal menu first, then edit the generated M only if you need something the menus cannot express.
- Rename steps in Applied Steps so the intent is obvious — "RemoveInternalEmails" is easier to audit than "Filtered Rows" repeated six times.
- Group your steps (right-click a step > Group) to keep a long query manageable.
- When you need to exclude a whole list of values, it is often simpler to keep a small reference table of exclusions and merge or anti-join against it than to write a very long NOT expression.
- Use
not List.Contains({"A","B","C"}, [Status])to express a multi-value exclusion in a single readable clause.
Troubleshooting
- "Expression.SyntaxError: Token Comma expected" or similar. M is case-sensitive and picky. Check for
!=, lowercasenotis correct but function names must be capitalised as written, and make sure brackets are balanced. - Every row disappears. Your condition is probably returning null or the wrong type. Click the step before the filter and inspect the column's data type; if it is "Any" or text holding numbers, change the type first.
- Nothing is filtered out. Recheck case sensitivity and whitespace — trailing spaces in text values are a common cause. Trim the column before comparing.
- The step errors after a refresh. A new value may have appeared that breaks a type assumption. Remove the step's type coercion or handle nulls explicitly.
- You cannot find a "NOT" option in the menus. There is no dedicated NOT button; use Does Not Equal, unticking values, or a custom column as described above.
Frequently Asked Questions
Does Power Query M support a NOT operator?
Yes. not is a built-in logical operator. It takes a logical value and returns the opposite, and it can be used anywhere in the formula bar, including inside each expressions in table functions.
What is the not-equal operator in Power Query?
It is <>. So [Status] <> "Closed" is the direct way to say "Status is not Closed" without using the word not.
Can I use AND and OR together with NOT?
Yes, with and and or (lowercase keywords). Bracket your groups to control the order, for example not ([A] = 1 or [B] = 2).
Why does my NOT filter remove blanks as well?
Because comparisons involving null typically return null rather than true, and filters treat null as non-matching. Add an explicit condition to handle nulls if you want to keep blank rows.
Does not work in Excel and Power BI the same way?
Yes. Both use the same M engine, so the syntax is identical; only the menus used to launch the editor differ.