Search code examples
filtertagsdaxcalculated-columns

Creating Tags for Max / Min dates in Power BI DAX


I have the following table calculated table - QOL2:

In DAX Power BI I need to create a calculated columns RecentDate and LatestDate as shown on a screenshot:

Based on ClientID I need to tag latest date as "Max" in LatestDate column and earliest date as "Min" in [RecentDate] column (see screenshot)

If there is only one date (no highest or earliest date) - as it is for ClientID = 2666, then I need no tags!

enter image description here

I am using the following code, but it puts "Max" tag in RecentDate and "Min" tag in LatestDate:

      LatestDate = var LatestD = CALCULATE(Min(QOL2[Date]), ALLEXCEPT(QOL2, QOL2[ClientID]))   
                    return If(QOL2[Date]=LatestD && QOL2[Date]>1,"Min")  




     RecentDate = var RecentD = CALCULATE(Max(QOL2[Date]), ALLEXCEPT(QOL2,QOL2[ClientID]))                                                                                                                                                          
                   return If(QOL2[Date]=RecentD && QOL2[Date]>1,"Max") 
 

Please help!


Solution

  • I'm not sure why you need this as Calculate column, but you can do this like that:

    RecentDate = var _client = 'Table'[ClientID]
    var _min = calculate(min('Table'[Date]), FILTER(ALL('Table'), _client = 'Table'[ClientID]))
    var _max = calculate(max('Table'[Date]), FILTER(ALL('Table'), _client = 'Table'[ClientID]))
    return
    if(_client = 'Table'[ClientID] && _min = 'Table'[Date] && _max <> _min, "MIN", BLANK())
    
    LatestDate = var _client = 'Table'[ClientID]
    var _min = calculate(min('Table'[Date]), FILTER(ALL('Table'), _client = 'Table'[ClientID]))
    var _max = calculate(max('Table'[Date]), FILTER(ALL('Table'), _client = 'Table'[ClientID]))
    return
    if(_client = 'Table'[ClientID] && _max = 'Table'[Date] && _max <> _min, "MAX", BLANK())
    

    enter image description here