Forum Discussion

imranamikhan's avatar
imranamikhan
Icon for Helper V rankHelper V
6 years ago
Solved

Count IFS By Latest Date

Hello,

 

I have a number of records with duplicates. Each record has a timestamp (date & time) and a status column.

 

 

I am using the following DAX expression to count records by the most recent timestamp. How do I modify this expression to also include only “Completed” records in the count?

 

 

 

LatestRecord =

VAR MostRencentTime =

    CALCULATE (

        MAX ( TaskTable[RecordTimestamp]),

        ALLEXCEPT ( TaskTable, TaskTable[Task ID] )

    )

RETURN

    IF ( MostRencentTime = MAX ( TaskTable[RecordTimestamp] ), 1, 0 )

 

 

 

Best regards,

ImranAmi

 

  • danextian's avatar
    danextian
    6 years ago

    Hi @imranamikhan ,

    I'm not sure how you want the count to be calculated exactly, but try this:

    UniqueCount = 
    VAR _max =
        CALCULATE (
            MAX ( TaskTable[RecordTimestamp] ),
            ALLEXCEPT ( TaskTable, TaskTable[Task ID] )
        )
    RETURN
        CALCULATE (
            COUNTROWS ( TaskTable ),
            FILTER (
                ALLEXCEPT ( TaskTable, TaskTable[Task ID] ),
                TaskTable[Status] = "Completed"
                    && TaskTable[RecordTimestamp] = _max
            )
        )
    

  • vivran22's avatar
    vivran22
    6 years ago

    @imranamikhan

    Try this:

    Solution =
    VAR MaxDate =
        MAX ( TaskTable[RecordTimestamp] )
    VAR _STATUS =
        FILTER (
            TaskTable,
            TaskTable[Status] = "Completed"
                && TaskTable[RecordTimestamp] = MaxDate
        )
    VAR _Count =
        COUNTROWS ( _STATUS )
    RETURN
        _Count

    Bless you!
    Vivek

    If it helps, please mark it as a solution
    Congratulations would be a cherry on top 🙂

    https://www.vivran.in/

    Connect connects on LinkedIn

12 Replies

  • vivran22's avatar
    vivran22
    Icon for Community Champion rankCommunity Champion

    Hello imranamikhan 

     

    You may try:

     

    LatestRecord =
    
    VAR MostRencentTime =
    
        CALCULATE (
    
            MAX ( TaskTable[RecordTimestamp]),
    
            ALLEXCEPT ( TaskTable, TaskTable[Task ID] ),
            KEEPFILTERS(TaskTable[Status] = "Completed"
    
        )
    
    RETURN
    
        IF ( MostRencentTime = MAX ( TaskTable[RecordTimestamp] ), 1, 0 

     

    Cheers!
    Vivek

    If it helps, please mark it as a solution
    Kudos would be a cherry on the top 🙂

    https://www.vivran.in/

    Connect on LinkedIn

     

  • v-frfei-msft's avatar
    v-frfei-msft
    Icon for Community Support rankCommunity Support

    Hi imranamikhan ,

     

    Have a try please.

    LatestRecord =
    VAR MostRencentTime =
        CALCULATE (
            MAX ( TaskTable[RecordTimestamp] ),
            FILTER (
                ALLEXCEPT ( TaskTable, TaskTable[Task ID] ),
                TaskTable[Status] = "Completed"
            )
        )
    RETURN
        IF ( MostRencentTime = MAX ( TaskTable[RecordTimestamp] ), 1, 0 )
    

     

     

    • imranamikhan's avatar
      imranamikhan
      Icon for Helper V rankHelper V

      Thanks v-frfei-msft and vivran22.

       

      I have tried both options but both return a value of 1, whereas what I am looking for is a count of records with a "Completed" status by the most recent time stamp. If I could translate the DAX into an Excel formula, it would look like this:

      =COUNTIFS(
      Table_TaskList[RecordTimestamp],
      MAX(Table_TaskList[RecordTimestamp]),
      Table_TaskList[ProcessStatus],"Completed"
      )

       

      • danextian's avatar
        danextian
        Icon for Super User rankSuper User

        Hi imranamikhan ,

         

        Have you tried using the formula of v-frfei-msft  and vivran22  in a calculated column then use the result for your count? If you are going to use a measure, the formula should be something like below:

         

        Count =
        CALCULATE (
                MAX ( TaskTable[RecordTimestamp] ),
                FILTER (
                    ALLEXCEPT ( TaskTable, TaskTable[Task ID] ),
                    TaskTable[Status] = "Completed"
                )
            )

         

  • v-frfei-msft's avatar
    v-frfei-msft
    Icon for Community Support rankCommunity Support

    Hi imranamikhan ,

     

    Does that meet your requirement?

    LatestRecord = 
    VAR MostRencentTime =
        CALCULATE (
            MAX ( TaskTable[RecordTimestamp] ),
            ALLEXCEPT ( TaskTable, TaskTable[Task ID] )
        )
    VAR a =
        IF (
            MostRencentTime = MAX ( TaskTable[RecordTimestamp] )
                && MAX ( TaskTable[Statue] ) = "Completed",
            1,
            0
        )
    RETURN
        a
    
    LatestRecord—1 = 
    SUMX(TaskTable,[LatestRecord])

     

     

     

      • danextian's avatar
        danextian
        Icon for Super User rankSuper User

        Hi @imranamikhan ,

        I'm not sure how you want the count to be calculated exactly, but try this:

        UniqueCount = 
        VAR _max =
            CALCULATE (
                MAX ( TaskTable[RecordTimestamp] ),
                ALLEXCEPT ( TaskTable, TaskTable[Task ID] )
            )
        RETURN
            CALCULATE (
                COUNTROWS ( TaskTable ),
                FILTER (
                    ALLEXCEPT ( TaskTable, TaskTable[Task ID] ),
                    TaskTable[Status] = "Completed"
                        && TaskTable[RecordTimestamp] = _max
                )
            )