Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
aymantm
Frequent Visitor

Subtract dates in the same column and return zero in case of the identical date values

Hello all,

I was looking for a way to create a calculated column that auto calculate the duration between dates in a date column called orderdate, with a filter of another column let's call it Product.

I composed the following expression:

ByAymanProductFlowDays =
var currentproduct=Sales[ProductName]
var previousdate=MAXX(FILTER(Sales,Sales[ProductName]=currentproduct&&Sales[OrderDate]<EARLIER(Sales[OrderDate])),Sales[OrderDate])
var result=IF(ISBLANK(previousdate),0,Sales[OrderDate]-previousdate)
return result
 
When using this expression on adventureworks data I got the results as shown in the following image
 
byaymanproductflowdays.jpg

 

In the small red circle there are two consecutive values of 1. My goal is to subtract the dates in the red column
so because the relevant dates are identical the yellow highlited value should be zero.
How to fix the DAX expression or rewrite it to meet my goals?
Than you all
Ayman
4 REPLIES 4
mahoneypat
Employee
Employee

Your previousdate variable has only a < EARLIER.  Change that to <=.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypatI tried this before. it returns all the column into zeros.

lbendlin
Super User
Super User

Do you have an index column?  How do you sort the rows?

I sorted the table manually.

No index column. I prefer if the solution is not based on extra columns

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.