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
Ecan20
Frequent Visitor

DAX to compute the difference between date for specific items

Dear experts,
I have a table like the example below with product ID, StartDate, EndDate. I would like to calculate the difference between the last EndDate and the first StartDate per each iem ID, like shown in the DateDiff column. Is that possible with a measure? Appreciate if you can offer some help. 

 

TIA!

 

IDStartDateEndDateDateDiff
121-12-11 22:3021-12-11 23:470.04167
121-12-11 23:4721-12-11 23:300.04167
221-12-11 23:5722-12-11 0:000.01250
222-12-11 0:0022-12-11 0:140.01250
222-12-11 0:1422-12-11 0:150.01250
322-12-11 0:2722-12-11 0:400.02569
322-12-11 0:4022-12-11 0:550.02569
322-12-11 0:5522-12-11 1:040.02569
422-12-11 1:0622-12-11 1:570.04514
422-12-11 1:5722-12-11 2:030.04514
422-12-11 2:0322-12-11 2:110.04514
1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

Please try this column expression and convert the column to type decimal.

 

NewColumn =
VAR thisID = IDs[ID]
VAR minDT =
    MINX ( FILTER ( IDs, IDs[ID] = thisID ), IDs[StartDate] )
VAR maxDT =
    MAXX ( FILTER ( IDs, IDs[ID] = thisID ), IDs[EndDate] )
RETURN
    maxDT - minDT

 

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


View solution in original post

3 REPLIES 3
mahoneypat
Employee
Employee

Please try this column expression and convert the column to type decimal.

 

NewColumn =
VAR thisID = IDs[ID]
VAR minDT =
    MINX ( FILTER ( IDs, IDs[ID] = thisID ), IDs[StartDate] )
VAR maxDT =
    MAXX ( FILTER ( IDs, IDs[ID] = thisID ), IDs[EndDate] )
RETURN
    maxDT - minDT

 

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


Anonymous
Not applicable

@Ecan20 

 

Are you sure you want a measure and not a calculated column? What should a measure return if there are many different ID's visible in the current context? Your picture suggests that you want a calculated column...

@Anonymous you are right, a calculated column will work better for my case. I am not really familiar though, could you help me through the steps please? Thank you

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.

Top Solution Authors