Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Number.BitwiseAnd: How to Use It

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Number.BitwiseAnd is a Power Query M function that compares two numbers bit by bit and returns a new number containing only the bits that are set to 1 in both inputs. You use it when you need to test or extract flags packed into an integer, such as permission masks, feature toggles stored as a single value, or device status codes. Nothing needs to be installed beyond Power Query itself, which ships with Excel, Power BI Desktop, and Power BI Service dataflows; you just need a query to edit.

What You'll Need

  • A Power Query editor session: in Excel, use Data > Get Data > Launch Power Query Editor; in Power BI Desktop, use Home > Transform data.
  • Two whole numbers to compare. The function works on integers, so convert decimals first if necessary.
  • A basic understanding that bitwise operations work on the binary representation of a number, not its decimal digits.
  • Optional but helpful: a column of flag or status values you want to decode, so you can apply the function in a custom column.

Step-by-Step

  1. Open the Power Query Editor. In Excel, select any cell in your table, then choose Data > Get Data > Launch Power Query Editor. In Power BI Desktop, select Home > Transform data. Menu labels can differ slightly by version, device, or region.
  2. Load the data you want to work with. Use Home > New Source and pick the connector for your data, or select an existing query in the Queries pane on the left.
  3. Decide which two numbers you are comparing. The first argument is usually the value you want to test, and the second is the mask that defines which bits matter. For example, to test whether the lowest bit is set, the mask is 1; to test the second bit, the mask is 2; to test the third, the mask is 4.
  4. Add a custom column. Go to Add Column > Custom Column. Give the column a name such as "FlagCheck".
  5. Enter the formula. In the formula box, type Number.BitwiseAnd([YourColumn], 1), replacing [YourColumn] with the actual column name from the list on the right. The function name and arguments are case-sensitive in M, so keep the capital B and A in BitwiseAnd.
  6. Press OK. Power Query adds the new column. Each row now contains the result of the bitwise AND for that row.
  7. Interpret the result. If the result equals the mask, the bit (or all bits in the mask) was set. If the result is 0, none of the masked bits were set. For a single-bit mask, any non-zero result means the flag is on.
  8. Convert the result to a true/false flag if you prefer. Add another custom column with a formula such as Number.BitwiseAnd([YourColumn], 1) <> 0. This returns true when the bit is set and false when it is not, which is easier to filter on.
  9. Check the column type. Select the new column header and set the data type to Whole Number (or True/False if you used the comparison). Power Query sometimes infers the wrong type, which can cause errors later.
  10. Apply the changes. Select Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to send the results back to your workbook or model.

Common Mistakes

  • Passing decimals. Number.BitwiseAnd expects whole numbers. A value like 3.5 will raise an error. Round or truncate the input first with Number.Round or Number.IntegerDivide as appropriate.
  • Using the wrong mask. A mask of 1 tests the first bit, 2 tests the second, 4 the third, 8 the fourth, and so on. Using 3 tests both the first and second bits together, which is only correct if you want to know whether both are set.
  • Comparing the result to 1 instead of the mask. When the mask is greater than 1, checking for a result of 1 gives wrong answers. Compare against the mask itself, or check for non-zero.
  • Typing the function name in lowercase. M is case-sensitive; number.bitwiseand will not resolve.
  • Forgetting negative numbers. Negative integers are represented in two's complement, so results with negative inputs can look surprising. Test with small positive values first.
  • Leaving the column as Any type. The step may work in the editor but fail on refresh if the type is not set explicitly.

Tips

  • Use a calculator or a quick manual conversion to confirm your mask before applying it to a whole column. Writing out the binary form of a few sample values makes the logic obvious.
  • Combine Number.BitwiseAnd with Number.BitwiseOr to build or merge flags, and with Number.BitwiseShiftLeft to construct masks programmatically.
  • Name custom columns descriptively, such as "IsAdminFlag" or "HasAlarmBit", so the meaning survives when you or someone else revisits the query later.
  • Keep the original column alongside the decoded flag during development; you can remove it once you have confirmed the results are correct.
  • If you are decoding many flags from one value, create one custom column per flag rather than one complex formula. It is easier to audit and easier to change later.

Troubleshooting

The formula returns an error about the argument type. Check that both arguments are whole numbers. Select the source column, set its type to Whole Number, and confirm there are no nulls or text values. Nulls propagate through the function, so replace them first with Table.ReplaceValue or by filtering.

The column shows the same value for every row. This usually means you referenced a constant instead of the row's column. Make sure the first argument is written as [ColumnName] with square brackets, not as a bare number.

The result is always 0. The mask may not match any bit in your data, or the data may be stored as text that Power Query is coercing to zero. Check the column type and a few sample values.

The query refreshes but the numbers look wrong. Confirm the mask values against your source documentation. If the source packs flags in a different bit order than you assumed, the results will be consistently off.

The function name is not recognised. Update Power Query or the Power BI Desktop version you are using; the bitwise functions are part of the standard M library, but very old versions may not include them. Use the official update channel for your product.

If that did not work, rebuild the step from scratch in a new custom column with a single hard-coded value, such as Number.BitwiseAnd(6, 2), which should return 2. If that works, the problem is in your column reference or data type rather than the function itself.

Frequently Asked Questions

What does Number.BitwiseAnd actually return?

It returns a whole number whose binary bits are set only where both input numbers had a bit set. For example, 6 (binary 110) AND 3 (binary 011) returns 2 (binary 010).

Can I use it on decimal numbers?

No. Both arguments must be whole numbers. Round or truncate decimals before passing them in, or the step will fail.

How do I test a single flag?

Use a mask with exactly one bit set: 1 for the first bit, 2 for the second, 4 for the third, and so on. A non-zero result means the flag is on.

Does it work in Excel and Power BI the same way?

Yes. The M engine is shared, so the function behaves the same in Excel Power Query, Power BI Desktop, and Power BI dataflows. Only the surrounding menus differ.

Why does my result look negative?

Negative inputs are stored in two's complement, so the bit pattern includes high bits that produce a negative result. This is expected behaviour, not an error.

Can I combine several flags in one column?

Yes. Use a mask that has several bits set, then compare the result to that mask to confirm all of them are present, or check for non-zero to confirm at least one is present.