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
gauravnarchal
Post Prodigy
Post Prodigy

Date Difference

I need help with the measure to calculate the date difference between two dates in a table.

 

Once I have the days calculated, I then need to get the results as how many items were shipped between the number of days.

gauravnarchal_0-1598638451585.png

gauravnarchal_1-1598638495111.png

 

1 ACCEPTED SOLUTION
v-jayw-msft
Community Support
Community Support

Hi @gauravnarchal ,

 

Check the following measures.

Measure = 
var datediff = DATEDIFF(SELECTEDVALUE('Table'[booking date]),SELECTEDVALUE('Table'[shipped date]),DAY)
return
SWITCH(TRUE(),datediff>=1&&datediff<=7,"1-7",datediff>=8&&datediff<=10,"8-10",datediff>=11&&datediff<=13,"11-13",datediff>=13,"13&more")

Measure 2 = CALCULATE(DISTINCTCOUNT('Table'[id]),FILTER('Table',[Measure]=SELECTEDVALUE(days[days])))

Result would be shown as below.

2.PNG

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

View solution in original post

9 REPLIES 9
v-jayw-msft
Community Support
Community Support

Hi @gauravnarchal ,

 

Check the following measures.

Measure = 
var datediff = DATEDIFF(SELECTEDVALUE('Table'[booking date]),SELECTEDVALUE('Table'[shipped date]),DAY)
return
SWITCH(TRUE(),datediff>=1&&datediff<=7,"1-7",datediff>=8&&datediff<=10,"8-10",datediff>=11&&datediff<=13,"11-13",datediff>=13,"13&more")

Measure 2 = CALCULATE(DISTINCTCOUNT('Table'[id]),FILTER('Table',[Measure]=SELECTEDVALUE(days[days])))

Result would be shown as below.

2.PNG

 

Best Regards,

Jay

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
Ashish_Mathur
Super User
Super User

Hi,

Share the link from where i can download your PBI file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
vivran22
Community Champion
Community Champion

Hello @gauravnarchal 

 

In such cases, I usually prefer to use Power Query to get the difference of the two dates and then add the column for categories using conditional columns. Power Query is made for such calculations and is efficient as compared to DAX calculated columns.

 

For getting the difference of the two dates, in the Power Query:

  • select the Ship Date & Order date (in that order)
  • Go to Add Columns > Date > Subtract Days
  • This will add a column with Duration in "Days:Hours:Months:Seconds" format.
  • Select the column > Transform > Duration > Total Days
  • You will get the column for days difference

 

For adding categories, you can use the Conditional Column feature under Add Column in Power Query.

 

 

For more details, you may follow the articles below:

 

https://www.vivran.in/post/bi-simplified-webinar-date-transformations-using-power-query

https://youtu.be/r5pVbKQkbGI?t=788

 

For adding categories:

https://www.vivran.in/post/adding-categories-with-power-query

 

Cheers!
Vivek

If it helps, please mark it as a solution. Kudos would be a cherry on the top 🙂
If it doesn't, then please share a sample data along with the expected results (preferably an excel file and not an image)

Blog: vivran.in/my-blog
Connect on LinkedIn
Follow on Twitter

jairoaol
Impactful Individual
Impactful Individual

the difference in days can be calculated with a measure as explained by previous colleagues with the Datediff() function, but to be able to use day ranges to plot is better a calculated column or table.

TomMartens
Super User
Super User

Hey @gauravnarchal ,

 

You can create a calculated column by using the DAX function DATEDIFF, create a calculated column like so:

days = DATEDIFF( 'Table'[booking date] , 'Table'[shipped date] , DAY)

 

Counting the difference between the booking and shipped date can be solved following the static segmentation pattern that is described by this pattern: https://www.daxpatterns.com/static-segmentation/

 

Hopefully, this provides some idas on how to tackle your challenge.

 

Regards,

Tom

 

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
pranit828
Community Champion
Community Champion

Hi @gauravnarchal 

you can create a column

_datediff =

var _diff = DATEDIFF(Table[Booking Date], Table[Shipped Date], DAY)

Return IF([_diff]<=7,"1-7",IF([_diff]<=10,"8-10",IF([_diff]<=13,"11-13","13 and More")))

 

and create a matrix chart with _datediff and count of _datediff column.

Let em know if you need help projecting it in table.





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

@pranit828 - Instead of creating column can this be achieved with the measure?

HI @gauravnarchal 

Yes, you can use measure to create it.





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

@pranit828  - I am not getting the table when creating the measure. Is there something wrong I am doing?

 

See below screenshot.

 

gauravnarchal_0-1598716293109.png

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.