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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

Calculate Day Diff between 2 dates on row level

Hi there,

 

I hope you can help me!

 

I have an issue: I used the table below as an example

 

I am trying to calculate the day difference based on " Change Date " for each Product based on " Entry NO ". Normally if I had 2 date columns that would be easy, but in this case, I have only 1 date column with all change dates.

 

It's easy to solve this in excel, but how can I solve this in Power BI? it has to be dynamic.

 

Product Entry      NoPhase      Change Date

Bike1Start8/11/2020
Bike2Develop8/12/2020
Bike3Finish8/14/2020
Car1Start8/12/2020
Car2Develop8/15/2020
Motor1Start8/17/2020
Motor2Develop8/20/2020
Motor3Finish8/24/2020
Step1Start8/11/2020
Step2Develop8/20/2020
Step3Finish8/29/2020
1 ACCEPTED SOLUTION
pranit828
Community Champion
Community Champion

HI @Anonymous 

 

Please check the below code. This gives 1 day as per your need.

 

CHANGE DATE = DATEDIFF([PHASE],MAXX(FILTER(TABLE,[PRODUCT] = EARLIER(PRODUCT) && [ENTRY] < EARLIER([ENTRY])),[PHASE] ),DAY)

 





PBI_SuperUser_Rank@1x.png


Hope it resolves your issue? 
Did I answer your question? Mark my post as a solution!

Appreciate your Kudos, Press the thumbs up button!!
Linkedin Profile

View solution in original post

7 REPLIES 7
pranit828
Community Champion
Community Champion

HI @Anonymous 

 

Please check the below code. This gives 1 day as per your need.

 

CHANGE DATE = DATEDIFF([PHASE],MAXX(FILTER(TABLE,[PRODUCT] = EARLIER(PRODUCT) && [ENTRY] < EARLIER([ENTRY])),[PHASE] ),DAY)

 





PBI_SuperUser_Rank@1x.png


Hope it resolves your issue? 
Did I answer your question? Mark my post as a solution!

Appreciate your Kudos, Press the thumbs up button!!
Linkedin Profile
amitchandak
Super User
Super User

@Anonymous , Not very clear

Try like

datediff(maxx(filter(table,[Product] = earlier(Product ) && [Entry] < earlier([Entry])),[Change Date] ),[Change Date],day)

Anonymous
Not applicable

What is expect is the difference in days for each product based on entry no and change date.

 

Example,

 

Bike has 3 entry no, the difference in days = 

Change date entry no 2 - Change date entry no 3 = (12-8-2020) - (11-8-2020) = 1 day.

 

This should be dynamic where Power BI takes into consideration the Product, Entry No before calculating the diff date in days.

parry2k
Super User
Super User

@Anonymous what you are expecting #2 - #1, #3 - #2 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

What is expect is the difference in days for each product based on entry no and change date.

 

Example,

 

Bike has 3 entry no, the difference in days = 

Change date entry no 2 - Change date entry no 3 = (12-8-2020) - (11-8-2020) = 1 day.

 

This should be dynamic where Power BI takes into consideration the Product, Entry No before calculating the diff date in day.

@Anonymous try this measure

 

New Measure = 
VAR __entry = MAX ( Table[Entry] ) - 1
VAR __prevDate = CALCULATE ( MAX ( Table[ChangeDate] ), Table[Entry] = __entry )
RETURN
DATEDIFF ( __prevDate, MAX ( Table[ChangeDate] ), DAY )

 

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

@parry2k  Thank you.

 

the function might be correct but we have different Scenario's:

 

1. Entry No different but Change date is the same

2. Entry No not ascending (1 3 4 6 instead of 1 2 3) which means Entry No -1 won't work properly.

3. Entry No not ascending and date is the same

4. First Entry No row date diff should always be 0 because there is no previous Entry No.

5. Entry No might start at 3 instead of 1.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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