Power Query Number.BitwiseNot: How to Use It
In this article
Number.BitwiseNot inverts every bit of a number and returns the result, so a value of 0 becomes -1, 1 becomes -2, and 5 becomes -6. It is a Power Query M function you call inside a custom column, a calculated step or an expression, and it is most useful when you are working with bit flags, masks or low-level identifiers that arrive as integers. Nothing needs installing beyond Power BI Desktop or Excel's Power Query Editor, but you should understand that the function works on whole numbers only and that negative results are normal, not an error.
What You'll Need
- A Power Query environment: Power BI Desktop, Excel (Data > Get Data), or another host that runs M queries.
- A query with at least one column containing whole numbers (Int64 type is safest).
- Basic familiarity with the Power Query Editor and the formula bar.
- Awareness that menu labels differ slightly by version, host and region, so a button may be worded a little differently on your machine.
Step-by-Step
- Load your data into Power Query. In Excel, go to Data > Get Data > From Table/Range or another source. In Power BI Desktop, use Home > Get Data. Confirm the query appears in the Queries pane on the left.
- Check the data type of the column you want to transform. Click the type icon in the column header and set it to Whole Number. Number.BitwiseNot expects an integer; if the column is typed as Decimal Number or Text, the step will fail or coerce values in ways you did not intend.
- Add a custom column. On the Add Column tab, click Custom Column. In the dialog, give the new column a name such as InvertedFlags.
- Enter the formula. In the formula box, type
Number.BitwiseNot([YourColumn]), replacing YourColumn with the exact name of your integer column, including square brackets. The function takes a single required argument and returns a number. - Click OK. Power Query evaluates the expression row by row and adds the new column. You should see negative values where the original was positive, and vice versa.
- Verify a few results by hand. 0 should give -1, 1 should give -2, 2 should give -3, and 3 should give -4. If your numbers match that pattern, the function is working correctly.
- Set the new column's type. Click the type icon on the new column and choose Whole Number so downstream steps treat it as an integer rather than Any.
- Rename and reorder as needed. Double-click the column header to rename it, or right-click and choose Rename. Drag it into position if you want it next to the source column.
- Apply and close. Use Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to push the result back to your model or worksheet.
If you prefer to write the step directly in the formula bar, you can also add a step manually: Table.AddColumn(PreviousStep, "InvertedFlags", each Number.BitwiseNot([YourColumn]), Int64.Type). This does exactly the same thing as the dialog and is handy when you are editing a query you already understand.
Common Mistakes
- Passing a decimal. Number.BitwiseNot is defined for integers. Feeding it 3.5 does not produce a meaningful bit pattern; round or truncate first with Number.Round or Number.IntegerDivide.
- Forgetting the square brackets. Writing
Number.BitwiseNot(YourColumn)treats the name as an identifier, not a column reference, and produces an error. It must be[YourColumn]. - Expecting a positive result. Bitwise NOT in M uses two's complement, so the result of inverting a non-negative number is negative. That is correct behaviour, not a bug.
- Using it on text. A column typed as Text will not convert reliably. Change the type first, or use Number.FromText in the expression.
- Assuming it flips only the bits you care about. The function inverts the whole value, including leading bits, which is why small numbers become large negatives. If you only want to flip specific flags, combine it with Number.BitwiseAnd using a mask.
Tips
- Use a mask to limit the inversion:
Number.BitwiseAnd(Number.BitwiseNot([Flags]), 255)keeps only the low eight bits, which is often what you want when decoding a byte. - Pair it with Number.BitwiseAnd, Number.BitwiseOr and Number.BitwiseXor to read and set individual flags in a packed integer.
- Add a comment step or rename the custom column clearly, because a column of negative numbers with no label is confusing to anyone else opening the query.
- Test on a small sample query first. Duplicate the query (right-click > Duplicate) and experiment there so your production query stays clean.
- Remember that M is case-sensitive for function names: it is Number.BitwiseNot, not number.bitwisenot.
Troubleshooting
The step shows an error for every row. Open the Query Settings pane, click the failing step and read the message in the formula bar area. The most common cause is a type mismatch; set the source column to Whole Number and refresh the preview.
The custom column dialog rejects the formula. Check for a missing closing parenthesis or a column name that does not match exactly. Column names with spaces need the brackets but no extra quotes: Number.BitwiseNot([Flag Value]).
Results look wrong compared with another tool. Some languages treat bitwise NOT as an unsigned operation on a fixed width. M works on signed 64-bit integers, so the same input can produce a different-looking number. Mask the result if you need a fixed width.
The column disappears after loading. Confirm the step is before Close & Load and that the column was not removed by a later Remove Columns step. Reorder the steps if necessary.
Nothing changes when you refresh. Check that the query is actually refreshing and that you have not disabled loading for the query. In Excel, right-click the query and confirm Load To is set as intended.
If none of that helps, rebuild the step manually in the formula bar using Table.AddColumn, which often surfaces the real error more clearly than the dialog.
Frequently Asked Questions
What does Number.BitwiseNot actually do?
It flips every bit of the supplied integer and returns the result. In practice, a non-negative input becomes negative and a negative input becomes non-negative.
Why does 0 return -1?
Because M uses two's complement for signed integers, all bits set equals -1. That is the standard behaviour of bitwise NOT in most languages.
Can I use it on decimal numbers?
No, not meaningfully. Convert to a whole number first with Number.Round or Number.IntegerDivide, then apply the function.
Does it work in Excel Power Query as well as Power BI?
Yes. Both hosts run the same M engine, so the function and syntax are identical. Only the surrounding menus differ.
How do I invert only some bits?
Combine Number.BitwiseNot with Number.BitwiseAnd and a mask, for example masking with 255 to keep the low byte only.
Is the function case-sensitive?
Yes. M is case-sensitive, so Number.BitwiseNot must be written with that exact capitalisation.