Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Need help reset count

Hi All,

I've tried to calculate the total infected and recovered users every month, the total recovered doesn't matter but the total infected when the same users are infected again after recovery the calculation doesn't work.

 

My table:

 

This is my DAX for it: 

CountInfected =
VAR CountIf = CALCULATE(DISTINCTCOUNT(Summary[User]),Summary[Infected]=1)
VAR CountRec = CALCULATE(DISTINCTCOUNT(Summary[User]),Summary[Recovered]=1)
RETURN
IF(CountIf=CountRec,0,CountIf-CountRec)
 
I would like Total Infected is 1 not 0
 
Many thanks if any of you can help me!
  • I'd recommend looking at whatever the last status was.

     

    Try something like this:

    CountInfected =
    VAR LastStatusDates =
        ADDCOLUMNS (
            VALUES ( Summary[User] ),
            "@LastStatusDate", CALCULATE ( MAX ( Summary[Date] ) )
        )
    RETURN
        SUMX (
            LastStatusDates,
            LOOKUPVALUE (
                Summary[Infected],
                Summary[User], Summary[User],
                Summary[Date], [@LastStatusDate]
            )
        )

     

6 Replies

  • Anonymous based on the data seems like everyone is recovered, how you expected answer to be 1

     

    Follow us on LinkedIn

     

    Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

     

    Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

  • I'd recommend looking at whatever the last status was.

     

    Try something like this:

    CountInfected =
    VAR LastStatusDates =
        ADDCOLUMNS (
            VALUES ( Summary[User] ),
            "@LastStatusDate", CALCULATE ( MAX ( Summary[Date] ) )
        )
    RETURN
        SUMX (
            LastStatusDates,
            LOOKUPVALUE (
                Summary[Infected],
                Summary[User], Summary[User],
                Summary[Date], [@LastStatusDate]
            )
        )

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Perfect, It works as required too in different ways. Great Support. Thanks AlexisOlson 

  • AlexisOlson got it. I missed that. Thanks for pointing that out.

     

    Anonymous Here is a measure.

     

    Total Infected = 
    VAR __table = TOPN ( 1, ALLSELECTED ( 'Table' ), CALCULATE ( MAX ( 'Table'[Date] ), VALUES ( 'Table'[User] ) ), DESC )
    VAR __totalInfected = 
    CALCULATE ( 
        SUM ( 'Table'[Infected] ), 
        KEEPFILTERS ( __table )
    ) -
    CALCULATE (
        SUM ( 'Table'[Recovered] ),
        KEEPFILTERS ( __table )
    )
    RETURN IF ( __totalInfected  > 0, __totalInfected )
       
    • Anonymous's avatar
      Anonymous
      Not applicable

      It works as required. Great Support. Thanks, parry2k