Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Getting previous date by category, not min date

Hello all, this Measure is working as long as there are only 2 dates Min and Max, but I need to get the date before Max regardless if it's Min or not. 1 table, 1 date column and 1 category column. I have a date table but I am not using it here. Thanks for any help on this, 

 

 

PreviosNameDays = 
var _Last =  MAX(Table[Created])
var _MinDate = CALCULATE (
    MIN( Table[Created]),
        ALLEXCEPT(  'Table', 'Table'[Name] ))
var _previousBKT = CALCULATE(MAX('Table'[Name]),Table[Created])
var _currentBKT = CALCULATE(MIN(Table[Name]),Table[created])
return
IF(_previousBKT =_currentBKT && _MinDate <> _Last,VALUE(UTCNOW()-_MinDate),
IF(_previousBKT =_currentBKT && _MinDate = _Last,VALUE(UTCNOW()-_MinDate),DATEDIFF(_MinDate,_Last,DAY)))

 

 

  • Thanks for clearer explanation, below you can try the new measure for the different of the days compare to previous ID.

    Measure =
    var forid = SELECTEDVALUE('Table'[ID])
    var selectdate = SELECTEDVALUE('Table'[Date])
    var previousdate = CALCULATE(MAX('Table'[Date]),FILTER(ALL('Table'),'Table'[ID]=forid-1))
    var result = DATEDIFF(previousdate,selectdate,DAY)
    return
    IF(previousdate=BLANK(),BLANK(),result)

    Another measure is the status: 
    Status =
    var forid = SELECTEDVALUE('Table'[ID])
    var forcategory = SELECTEDVALUE('Table'[Category])
    var result = CALCULATE(MAX('Table'[Category]),FILTER(ALL('Table'),'Table'[ID]=forid-1))
    return
    IF(result=BLANK(), BLANK(), IF(forcategory<>result, "Change", "No Change"))
     

     


     

    Hope this can help you.

     



  • Hi Anonymous 

     

    If the problem has been solved you can mark the reply for the standard answer to help the other members find it more quickly. If not, please point it out.


    Looking forward to your feedback.


    Best Regards,
    Henry

11 Replies

  • Hi, you may try to use MAXX/MINX instead of MAX/MIN. and if you want to by category, you can add

    var category1 = selectedvalue('category')

     

    return

    CALCULATE(IF(_previousBKT = _currentBKT && _MinDate = _Last,VALUE(UTCNOW()- _MinDate), DATEDIFF(_MinDate,_Last,DAY)), 'category' = 'category1')

     

    Hope this can help you 😁

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello, thanks for the reply. my goal is to return the date before max not min, but I can't use the earlier function in a measure (or can you?), below is my data simplified, in the table I need to compare ID 1 and 4 and get the datediff in days for aName

      1aName7/30/22
      2aName7/15/22
      3aName7/1/22
      4aName8/15/22

       thanks for anu help on this

      • Chew_WenJie's avatar
        Chew_WenJie
        Icon for Resolver III rankResolver III

        Hello, earlier function are normally used in the calculated column instead of measure. You may refer to the power bi that i had attached below, the measure i done is datediff based on the ID of the name. 

         

        Dummy data:


        Sample measure result: 

        Measure:

         

        Measure =
        var aname = SELECTEDVALUE('Table'[Name])
        var findminID = CALCULATE(MIN('Table'[ID]),'Table'[Name]=aname)
        var findmaxID = CALCULATE(MAX('Table'[ID]),'Table'[Name]=aname)
        var find1stdate = CALCULATE(MAX('Table'[Date]),'Table'[ID]=findminID)
        var findlastdate = CALCULATE(MAX('Table'[Date]),'Table'[ID]=findmaxID)

        var datedifferent = DATEDIFF(find1stdate,findlastdate,DAY)

        return
        datedifferent

         

        .

        Hope this can help you 

         

  • v-henryk-mstf's avatar
    v-henryk-mstf
    Icon for Community Support rankCommunity Support

    Hi Anonymous ,

     

    You can try formula like below:

    Col = RANKX('Table','Table'[Date],,DESC,Dense)
    M_ =
    VAR min_ =
        MIN ( 'Table'[Date] )
    VAR max_ =
        CALCULATE ( MAX ( 'Table'[Date] ), 'Table'[Col] = 3 )
    RETURN
        DATEDIFF ( min_, max_, DAY )

    If the problem is still not resolved, please provide detailed error information and let me know immediately. Looking forward to your reply.


    Best Regards,
    Henry


    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello thank you for the reply, the last post in this thread has my sample data and and an explanation of what I am trying to do. I can see where RANKX can be useful but I cuold not get the desired result, I am trying to count the number of days in each category, please see my sample data posted earlier today.