Smart Systems for Home

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

Power Query Number.BitwiseShiftLeft: How to Use It

· 5 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.BitwiseShiftLeft shifts the bits of a number to the left by a given number of positions, effectively multiplying the number by 2 raised to that power. In Power Query M, the function is Number.BitwiseShiftLeft(number as nullable number, shift as number) as nullable number. It works on integer values; if you pass a decimal, Power Query rounds it to the nearest integer before shifting. You need Power Query (available in Excel, Power BI, and other Microsoft data tools) and a basic understanding of how to add a custom column or edit an existing query.

What You'll Need

  • Access to Power Query: Excel (Data > Get & Transform Data > Launch Power Query Editor), Power BI Desktop (Transform Data), or another tool that hosts Power Query.
  • A query with at least one numeric column, or the ability to create a blank query to test the function.
  • Familiarity with the Power Query Editor interface: the formula bar, the Add Column tab, and the Advanced Editor.
  • No special permissions or add-ins are required; the function is built into the standard M library.

Step-by-Step

  1. Open the Power Query Editor. In Excel, go to Data > Get & Transform Data > Launch Power Query Editor. In Power BI Desktop, go to Home > Transform Data. The exact menu labels can differ slightly by version and region.
  2. Load or create a query. If you already have data loaded, select the query in the left pane. To test quickly, go to Home > New Source > Blank Query, then use the formula bar to enter a number, for example = 5, and press Enter.
  3. Add a custom column for the shifted value. With your query selected, go to the Add Column tab and click Custom Column. In the dialog, give the new column a name such as Shifted.
  4. Enter the formula. In the Custom Column formula box, type a call to the function. For example, to shift the value in a column named Number left by 3 bits, enter: Replace with your actual column name and 3 with the number of positions you want to shift. Click OK.
  5. Check the result. The new column should appear with the shifted values. For example, shifting 5 left by 3 gives 40 (5 × 2³ = 40). If you see errors, proceed to the troubleshooting section.
  6. Handle nulls and non-integers if needed. The function returns null if the input is null. If your column contains decimals, they are rounded to the nearest integer before shifting. To avoid surprises, you can wrap the input in Number.Round or filter out non-integers beforehand.
  7. Apply and close. When you are satisfied, go to Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to load the results back into your workbook or model.

Common Mistakes

  • Using a negative shift value. Number.BitwiseShiftLeft expects a non-negative shift. A negative shift may produce unexpected results or an error; use Number.BitwiseShiftRight for right shifts instead.
  • Forgetting that decimals are rounded. Passing 5.7 will shift 6, not 5.7. If you need to preserve fractional parts, handle them separately.
  • Assuming the result is always exact for large numbers. Power Query uses integer arithmetic, but very large shifts can overflow the underlying integer type. Test with your expected range.
  • Referencing a column that does not exist or using the wrong case. Column names are case-sensitive in M. Check the exact name in the formula bar or the query steps.
  • Mixing up bitwise and arithmetic multiplication. While shifting left by n is equivalent to multiplying by 2^n for integers, the function only works on integers; for general multiplication, use the * operator.

Tips

  • Use Number.BitwiseShiftLeft when you need to pack flags or bitmasks into a single integer, or when decoding binary protocols.
  • To shift by a variable amount, reference another column for the shift count, for example Number.BitwiseShiftLeft([Value], [ShiftAmount]).
  • Combine with Number.BitwiseOr and Number.BitwiseAnd to set, clear, or test individual bits.
  • If you are working with binary data, convert to integer first using Binary.ToList and related functions, then apply the shift.
  • Document your custom column formula in a comment or a separate step so others understand the bit manipulation.

Troubleshooting

If the function returns an error, check the following in order:

  • Syntax errors: Ensure the function name is spelled exactly Number.BitwiseShiftLeft and that arguments are separated by a comma. M is case-sensitive.
  • Data type mismatch: The first argument must be a number or null. If your column is text, convert it to a number first using the Transform tab or Number.From.
  • Null values: If the input is null, the result is null. If you want a default, use ?? (coalesce) or if [Column] = null then 0 else Number.BitwiseShiftLeft([Column], 3).
  • Shift value out of range: Extremely large shift values may cause overflow. Keep shifts within the range of your data type (typically 32-bit or 64-bit integers).
  • Query folding: Bitwise functions may not fold to the data source, which can affect performance on large datasets. Consider doing the shift in the source system if possible.
  • Version differences: The function is available in most recent versions of Power Query, but if you are on an older version, check the Microsoft documentation for availability.

If none of these resolve the issue, try creating a minimal test query with a single number to isolate the problem.

Frequently Asked Questions

What does Number.BitwiseShiftLeft actually do?

It shifts the binary representation of an integer to the left by a specified number of bits, filling the rightmost bits with zeros. This is equivalent to multiplying the number by 2 raised to the shift count, for non-negative integers.

Can I use a negative shift value?

No, the function expects a non-negative shift. For right shifts, use Number.BitwiseShiftRight.

Does it work with decimal numbers?

Yes, but decimals are rounded to the nearest integer before the shift is applied. If you need to preserve fractional parts, handle them separately.

What happens if the input is null?

The function returns null. You can handle nulls with conditional logic or the coalesce operator.

Is Number.BitwiseShiftLeft available in all Power Query versions?

It is part of the standard M library and is available in current versions of Excel, Power BI, and other Power Query hosts. If you are on an older version, check the official documentation.

Can I use this function in a calculated column in Power BI?

Yes, you can use it in the Power Query Editor to create a custom column. It is not available in DAX; for DAX, use arithmetic multiplication instead.