Power Query Problems

How to Fix an IF Function Returning Null in Power Query

Nimra MalikNimra Malik Nov 11, 2026 869 views

Question details

The user needs to calculate the duration between project tasks but receives null values when the completion date is blank or certain milestone dates are missing.

How to Fix an IF Function Returning Null in Power Query
Product
Excel
Device & OS
not provided
Scenario
Calculating the number of days between project tasks where job sites might be unfinished, requiring a fallback to the current date or previous milestones.
Observed behavior
The Power Query duration calculation returns null instead of a valid number of days when encountering blank date fields.
Before you start

Ensure all your date columns are properly formatted as 'Date' or 'Date/Time' data types in the Power Query Editor before performing duration calculations.

Solution 1Recommended

Use a Cutoff Date Column and Null-Coalescing Operator

This method creates a fallback date for unfinished tasks using the current date, and gracefully handles missing previous milestone dates using the '??' operator.

The null-coalescing operator (??) in Power Query evaluates the left operand. If it is not null, it returns it; otherwise, it evaluates and returns the right operand. This is perfect for stepping back through milestone dates.

1
Add a Cutoff Date Column

Go to the 'Add Column' tab and select 'Custom Column'. Name the column 'CutOffDate' and enter the formula: = if [Completed] = null then DateTime.LocalNow() else [Completed]

2
Format the New Column

Select the newly created 'CutOffDate' column, click the Data Type icon in the column header, and change it to 'Date' or 'Date/Time'.

3
Calculate the Duration

Create another Custom Column named 'Duration'. Use the null-coalescing operator (??) to fall back to previous milestones if the latest one is missing. For example: = [CutOffDate] - ([#"24-Hour Run Test"] ?? [Rotor Install] ?? [Prework Walkdown])

Use a Cutoff Date Column and Null-Coalescing Operator
Tip: Using DateTime.LocalNow() ensures that your query always calculates the duration up to today's date for ongoing tasks every time you refresh the data.
Free Microsoft Office alternative

Try WPS Office for Efficient Data Analysis

While Power Query's M-language is specific to Microsoft Excel, WPS Office offers a free, lightweight, and highly compatible alternative for everyday spreadsheet tasks and data analysis.

Free and lightweight suite for quick installationSeamless compatibility with Microsoft Excel (.xlsx, .xls) filesBuilt-in advanced formulas and pivot tables for robust data analysisFamiliar user interface requiring zero learning curve
microsoft office alternative - wps office

Frequently Asked Questions

What does the '??' operator do in Power Query?

It is the null-coalescing operator. It returns the value on its left if it is not null; otherwise, it returns the value on its right. It is extremely useful for providing fallback values when dealing with missing data.

How do I get today's date dynamically in Power Query?

You can use the DateTime.LocalNow() function in M code to get the current system date and time. If you only need the date without the time, wrap it in DateTime.Date(DateTime.LocalNow()).

Why does subtracting two dates in Power Query return an error or null?

This usually happens if one of the fields contains a null value, or if the columns have not been explicitly formatted as 'Date' or 'Date/Time' data types before performing the mathematical operation.

Can WPS Spreadsheet run Microsoft Power Query M-code scripts?

WPS Spreadsheet supports advanced data analysis, pivot tables, and VBA-like macros, but the direct execution of Power Query M-code scripts is a proprietary feature specific to Microsoft Excel.