Power Query Number.BitwiseShiftRight: Step-by-Step
In this article
The Number.BitwiseShiftRight function in Power Query shifts the bits of a number to the right by a specified count, effectively dividing the number by powers of two. It's used in data transformation when you need to extract bit fields, decode flags, or perform low-level integer arithmetic. To use it, you need Power Query (available in Excel 365, Excel Power Pivot, or Power BI) and a basic understanding of binary numbers. The function takes two arguments: the number to shift and the number of positions to shift.
What You'll Need
- Power Query Editor open (in Excel: Data > Get Data > Launch Power Query Editor; in Power BI: Home > Transform Data).
- A column or value containing non-negative integers (the function works on 64-bit signed integers; negative numbers may produce unexpected results).
- Basic familiarity with the Power Query formula language (M).
Step-by-Step
- Open your query in Power Query Editor. Load your data into Power Query first. If you're in Excel, select your data range, go to the Data tab, and click "From Table/Range" (or "From Sheet"). In Power BI, click "Transform Data" from the Home tab.
- Add a custom column. Go to the "Add Column" tab and click "Custom Column." This opens a dialog where you can write M code.
- Write the formula. In the "Custom column formula" box, enter:
Number.BitwiseShiftRight([YourNumberColumn], 1)Replace[YourNumberColumn]with the actual column name (e.g.,[Value]). The second argument (1) is the number of bit positions to shift right. For example, shifting 8 (binary 1000) right by 1 gives 4 (binary 0100). - Name your new column. In the "New column name" field, type something descriptive like "ShiftedRight1" or "DecodedFlag." Then click OK.
- Check the result. The new column appears with the shifted values. Verify a few manually: if your original number is 10 (binary 1010), shifting right by 1 should give 5 (binary 0101).
- Use a variable shift count. If the shift amount is in another column, reference that column instead of a hard-coded number. For example:
Number.BitwiseShiftRight([Value], [ShiftCount]). Ensure the shift count column contains non-negative integers. - Combine with other bitwise functions. To extract a specific bit, shift right and then use
Number.BitwiseAndwith 1. For instance, to get the 3rd bit (counting from 0) of a number:Number.BitwiseAnd(Number.BitwiseShiftRight([Value], 3), 1). This returns 1 if that bit is set, 0 otherwise. - Handle negative numbers (if needed). The function treats numbers as 64-bit signed integers. Shifting right on a negative number performs an arithmetic shift (preserves the sign bit), which may not match your expectation. If you only need unsigned behavior, filter out negative numbers or use
Number.Modto convert to a positive range first. - Close and load. Once satisfied, click "Close & Apply" (Excel) or "Close & Apply" in Power BI to load the transformed data back into your worksheet or data model.
Common Mistakes
- Using a negative shift count. The second argument must be non-negative. A negative count will cause an error. Use
Number.BitwiseShiftLeftfor left shifts. - Shifting by more than 63. For 64-bit integers, shifting by 64 or more results in 0 (for positive numbers) or -1 (for negative numbers). This is rarely intended; double-check your shift count.
- Forgetting that this is integer division. Shifting right by 1 is equivalent to dividing by 2 and truncating the remainder (floor division for positive numbers). For example, 7 shifted right by 1 is 3, not 3.5. If you need decimal division, use
[Value] / 2instead. - Applying to non-integer columns. The function expects integers. If your column has decimals, you'll get an error. Convert to
Int64.Typefirst usingTable.TransformColumnTypes. - Confusing bit order. When extracting bits, remember that the rightmost bit is position 0. Shifting right by N moves bit N to position 0.
Tips
- Use for flag decoding. If you have an integer that encodes multiple boolean flags (e.g., 1=read, 2=write, 4=execute), you can extract each flag with
Number.BitwiseAnd(Number.BitwiseShiftRight([Flags], 0), 1)for the first bit, and so on. - Combine with
Number.BitwiseAndfor masking. To isolate a range of bits, shift right first, then apply a mask. For example, to get the upper 4 bits of a byte:Number.BitwiseAnd(Number.BitwiseShiftRight([Byte], 4), 15)(15 is binary 1111). - Test with small numbers. Before applying to a large dataset, test with a few known values in a scratch query to confirm the shift logic.
- Use a helper column for readability. If you need multiple shifts, create separate custom columns for each shift amount rather than nesting complex formulas.
- Consider using
Number.IntegerDividefor simple division by powers of two. If you only need to divide by 2, 4, 8, etc.,Number.IntegerDivide([Value], 2)is clearer. Bitwise shift is faster for large datasets but less readable.
Troubleshooting
Error: "Expression.Error: The name 'Number.BitwiseShiftRight' wasn't recognized." This means you're using an older version of Power Query that doesn't have this function. Check your Excel or Power BI version. The function is available in Excel 2016 or later and Power BI Desktop. If you're on an older version, use Number.IntegerDivide instead.
Error: "Argument 2 should be a number." The shift count is not numeric. Ensure the column referenced for the shift count is of type Int64.Type or Number.Type. Convert it if necessary.
Result is not what you expected. Verify your binary understanding. Write out the binary representation of a few numbers and manually shift them. Remember that shifting right truncates bits on the right. Also, check if the number is negative; negative numbers have an infinite string of 1s on the left, so shifting right can produce large negative numbers.
Function returns null. If the input column contains null values, the function will return null. Use if [Column] = null then null else Number.BitwiseShiftRight(...) to handle them explicitly.
Performance issues on large tables. Bitwise operations are fast, but if you're doing many shifts on millions of rows, consider adding a step to filter out rows you don't need before the shift, or use a calculated column in DAX (if in Power BI) instead of a Power Query step.
Frequently Asked Questions
Can I shift right by a decimal number?
No, the shift count must be a non-negative integer. If you need to divide by a non-power-of-two, use Number.IntegerDivide or regular division.
What is the difference between Number.BitwiseShiftRight and Number.IntegerDivide?
For positive integers, shifting right by N is equivalent to Number.IntegerDivide by 2^N. However, bitwise shift is faster and works directly on binary representation. For negative numbers, they differ: IntegerDivide rounds toward zero, while bitwise shift rounds toward negative infinity.
Does this function work with 64-bit numbers?
Yes, Power Query uses 64-bit signed integers. The maximum positive value is 2^63-1. Shifting right by 63 on a positive number will give 0 or 1 depending on the sign bit.
How do I extract a specific bit from a number?
Use . For example, to get the 4th bit (position 4), use . This returns 1 if the bit is set, 0 otherwise.
Why does shifting a negative number give a different result than expected?
Power Query performs an arithmetic shift on negative numbers, which preserves the sign bit. This means shifting right on a negative number effectively divides by 2 but rounds toward negative infinity. For example, -3 shifted right by 1 is -2 (not -1). If you need logical shift (fill with zeros), convert to a positive number first.
Can I use this function in DAX?
No, DAX does not have bitwise shift functions. You would need to use Power Query to pre-compute the shifted values, or use DAX workarounds like INT([Value] / 2) for simple cases.