How to Calculate the Difference Between Days in Power Query

Calculating the difference between two dates in Power Query can be done using a Power Query formula that references two date columns. You can use the same technique to calculate the difference between a date column and today’s date. We’ll explain how.

Learn how to calculate the difference between dates in Power Query

Working with date columns in Power Query can be somewhat confusing if you’re used to working with regular Excel formulas as Power Query uses its own formula language called M. The syntax is completely different, and it’s even different from Power BI and Power Pivot DAX.

Let’s jump in!

Getting Started with Date Calculations in Power Query

Before getting started, you will have to have data loaded into Power Query. In our example we’ll use a simple dataset that contains multiple date columns for sales, invoice, and invoice due date along with an order number. This is a pretty common setup that comes up when calculating receivables aging or other accounting use cases.

Our dataset is as follows.

Screenshot of a Power Query dataset that contains multiple columns of dates

While we already have our columns defined with a Date Data Type, it’s important to make sure that the columns you’re working with are defined as dates.

If you try calculating the day difference between text or numeric columns, your formulas will result in an error.

Ensure That Columns are Correct Data Type

Data types are noted at the top of each column by a small icon. Numbers are numeric columns, calendars are dates, text are text etc.

If you need to change a column data type, right click on the column header and select Change Type.

Changing the data type of a column in Power Query to Date or Date/Time

After making the change, you will see a new Applied Step on the right side of Power Query that tracks the change.

Create a New Custom Column

Power Query M formulas can either be entered into the formula bar above your column headers, but this technique is rarely used other than by pro Power Query developers. The more common method is to create a new column with your formula in it.

To create a new column based on a Power Query formula, go to the Add Column section of the Power Query ribbon and select Custom Column.

Creating a new custom column in Power Query to enter in a Power Query Formula

This will create a new column, but equally important it will bring up the Power Query formula dialog box.

The Power Query Formula for Date Difference

The Power Query formula to calculate the difference between two dates is Duration.Days. To use it, you have to create a Custom Column which allows you to enter formulas using the Power Query M language. Formulas are a different syntax from Excel and Power BI DAX.

When working with two pre-existing date columns the syntax is:

Duration.Days([EndDate] - [StartDate])

In practice, the Power Query formula will look like the following screenshot, where we calculate the day difference between two date columns, the Invoice Date and the Sales Date.

The Duration.Days formula in use with specific column names to calculate the difference

When you click OK a new column will be created in the dataset that can later be used for binning, analyzing or presenting in various visualizations.

Calculating the Difference Between a Date and Today

If you want to calculate the difference between two dates when one of the days is today, you will need to use a formula to pull in today’s date. Create a Custom Column and use the Power Query M formula DateTime.LocalNow() to create a column showing today’s date.

Alternatively, you could embed the formula in with a longer formula, but most of the times we prefer to separate the calculations into separate steps for easier understanding if someone else has to manage the data model.

The following screenshot is using the DateTime.LocalNow formula to create a column of today’s date and time.

Using Power Query to calculate today's date to determine the difference between a date and todays date

After creating a new column, don’t forget to convert the data type to a Date or Date Time column by right clicking on the column header and changing the data type to avoid errors in subsequent calculations.

Once the new column with today’s date is entered in, you can apply the standard Duration.Days formula to calculate the number of days between a date and today.

Conclusion

Power Query is an incredibly useful data transformation tool from Microsoft. Power Query is available in Excel, Power BI, and Microsoft Fabric as a way to prep data for further analysis and presentation. The ability to calculate the difference between date columns or to calculate the difference between a date column and today’s date is quick and easy.

Use the Duration.Days formula to calculate the difference between two columns, but first make sure that each column is in the appropriate format and can be defined as a date or date time data type. Sometimes you need to transform the column to get it into the right format for Power Query to read it appropriately.

Scroll to Top