Power Query Number.IsOdd Function: Step-by-Step Guide
In this article
The Number.IsOdd function in Power Query is a simple yet powerful tool used to determine whether a given numeric value is an odd number. It returns a logical value—true if the number is odd, and false if it is even or zero. This function is essential for data filtering, conditional column creation, and business logic implementation within Microsoft Excel’s Get & Transform Data feature or Power BI’s Power Query Editor.
What You'll Need
- Microsoft Excel (2016 or later) or Power BI Desktop: These applications include the Power Query Editor required to run M language functions.
- Data Source: A table containing at least one column with numeric values. The data can be integers or decimals, though the function primarily evaluates the integer portion for parity checks in most standard use cases.
- Basic Familiarity: Understanding how to open the Power Query Editor via the Data tab in Excel or Transform Data in Power BI.
Step-by-Step
- Load Your Data into Power Query
Open your Excel workbook or Power BI report. Select your data range or table. Go to the Data tab (in Excel) or click Transform Data (in Power BI). In Excel, click From Table/Range. This action opens the Power Query Editor window, where all transformations occur.
- Verify Data Types
Before applying any function, ensure the column you intend to check is formatted as a number. Look at the icon next to the column header. If it shows a generic text icon (ABC), right-click the column header, select Change Type, and choose Whole Number or Decimal Number. Using
Number.IsOddon text values will result in an error unless converted first. - Add a Custom Column
With the correct column selected, go to the Add Column tab in the ribbon. Click Custom Column. A dialog box titled "Add Custom Column" will appear. This is where you will write the M code.
- Enter the Formula
In the "Custom column formula" field, type the following syntax:
Number.IsOdd([YourColumnName])Replace
[YourColumnName]with the actual name of your numeric column, including the square brackets. For example, if your column is named "ID", the formula becomesNumber.IsOdd([ID]).Give your new column a descriptive name, such as "IsOddFlag" or "ParityCheck", in the "New column name" field. Click OK.
- Review the Results
A new column appears in your preview grid containing
trueorfalsevalues. Verify that odd numbers returntrueand even numbers (including zero) returnfalse. If you see errors, proceed to the troubleshooting section below. - Apply the Transformation
Once satisfied with the output, click Close & Load in the top-left corner of the Power Query Editor. This saves the query and loads the transformed data back into your Excel worksheet or Power BI model.
Common Mistakes
- Ignoring Data Types: The most frequent error occurs when users attempt to run
Number.IsOddon columns stored as text. Even if the text looks like a number (e.g., "123"), the function fails because it expects a numeric primitive. Always convert types explicitly. - Case Sensitivity: M language is case-sensitive. Writing
number.isoddorNUMBER.ISODDwill cause a syntax error. Ensure you use exact camelCase formatting:Number.IsOdd. - Missing Square Brackets: When referencing a column in a custom column formula, you must wrap the column name in square brackets. Omitting them, such as writing
Number.IsOdd(ID), results in an "Unknown identifier" error. - Handling Nulls: If your data contains blank cells (nulls),
Number.IsOdd(null)may behave unexpectedly depending on context, often returning null rather than true/false. Be aware that nulls are not considered odd or even by default.
Tips
- Combine with Conditional Columns: Instead of just flagging odd numbers, use this function inside a Conditional Column to categorize data. For example, create a rule: "If
Number.IsOdd([Value])is true, then 'Group A', else 'Group B'." - Filtering Odd Records: After creating the boolean column, you can filter the view to show only rows where the value is
true. This is useful for auditing datasets where every other record needs review. - Performance Considerations: While
Number.IsOddis lightweight, avoid chaining multiple complex mathematical operations in a single custom column if performance degrades on large datasets. Break down logic into separate steps if necessary. - Use with Decimals: Note that
Number.IsOddworks on decimal numbers as well. It checks the integer part of the number. For instance,Number.IsOdd(3.5)returnstruebecause 3 is odd. If you need strictly integer-based logic, consider rounding or truncating first usingNumber.RoundDown.
Troubleshooting
If the function does not work as expected, follow these steps:
- Check for Errors in Preview: If the new column displays "Error" instead of true/false, click on the cell. A detailed message will appear in the formula bar or status bar. Common messages include "Expression.Error: We cannot apply operator IsOdd to type Text." This confirms a data type mismatch.
- Validate Column Names: Ensure the column name in your formula exactly matches the header in the table, including spaces and special characters. If the column name has spaces, it must still be enclosed in square brackets, e.g.,
[My Column Name]. - Inspect Source Data: Sometimes hidden characters or formatting issues prevent proper conversion. Try selecting the source column, going to Transform > Trim or Clean before changing the type to Number.
- Restart Power Query: Occasionally, the editor cache causes glitches. Close the Power Query Editor without saving, reopen it, and reapply the step to ensure clean execution.
Frequently Asked Questions
Does Number.IsOdd work with negative numbers?
Yes. The function correctly identifies negative odd numbers. For example, Number.IsOdd(-3) returns true, while Number.IsOdd(-4) returns false. Parity rules apply consistently across positive and negative integers.
How do I handle null values in my dataset?
By default, passing a null value to Number.IsOdd typically results in a null output rather than true or false. If you want nulls to be treated as even (false), you can wrap the function in an if statement: if [Column] = null then false else Number.IsOdd([Column]).
Can I use this function in DAX instead of Power Query?
No, Number.IsOdd is specific to the M language used in Power Query. In DAX (used for measures and calculated columns in Power BI or Excel Data Model), you would use the MOD function. For example: IF(MOD([Column], 2) <> 0, TRUE(), FALSE()).
Why is my decimal number returning unexpected results?
Number.IsOdd evaluates the parity of the integer component of the number. For a decimal like 2.7, it checks if 2 is odd (it is not), so it returns false. If you need to check the entire value's properties differently, consider using Number.Round or Number.Truncate before applying the odd check.
Is there a Number.IsEven function?
There is no built-in Number.IsEven function in Power Query. To check for even numbers, simply negate the result of Number.IsOdd. For example: not Number.IsOdd([Column]) will return true for even numbers and false for odd numbers.