Forum Discussion

Ecan20's avatar
Ecan20
Frequent Visitor
5 years ago
Solved

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
  • 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

     

3 Replies

  • Anonymous's avatar
    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...

    • Ecan20's avatar
      Ecan20
      Frequent Visitor

      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

  • mahoneypat's avatar
    mahoneypat
    Icon for Microsoft Employee rankMicrosoft 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