Forum Discussion

SO's avatar
SO
Helper III
6 years ago

Calculated Measure for multiple filters

I am trying to total the number of days when a person is absent for both the am and the pm (on the same day) over a period of time based on multiple filters using a number of slicers. 

 

In essence 

Person#       Date              Period         Abs Type

 1                 9/1/2019         am                sick

 1                 9/1/2019         pm                sick

 2                 9/1/2019         am                sick

 1                 9/2/2019         am                sick

 1                 9/3/2019         am                personal 

 1                 9/3/2019         pm                personal 

 2                 9/3/2019         pm                personal 

 1                 9/4/2019         am                sick

 1                 9/5/2019         am                sick

 1                 9/8/2019         am                sick

 1                 9/8/2019         pm                sick

 2                 9/8/2019         pm                sick

 2                 9/9/2019         pm                sick

 2                 9/10/2019       pm                sick

 

 

I can create a measure to count either the "am" or the "pm" but not both (aka 'and' or '&&').  

 

For example, I can create a measure to count the morning absences.  

 

#absent =
CALCULATE (
DISTINCTCOUNT ( BADetail[Person] ),
Filter(BADetail, BADetail[SchoolPeriod] ="AM"
))
 
 
I can then get the following chart to show 
 

Person#       Date              Period         Abs Type               #absent 

 1                 9/1/2019         am                sick                          1.0 

 1                 9/2/2019         am                sick                          1.0

 1                 9/3/2019         am                personal                  1.0 

 1                 9/4/2019         am                sick                          1.0

 1                 9/5/2019         am                sick                          1.0

 1                 9/8/2019         am                sick                          1.0

 ___________________________________________________________________

 Total                                                                                       1.0

 

So I created a new measure 

#total for Date = SUMX(BADetail, BADetail[# absent])
 
And this will show a correct count of 6.0 for just the 'am' absences, but not for the full days (am and pm)
 
I have tried to alter my first measure 
#absent =
CALCULATE (
DISTINCTCOUNT ( BADetail[Person] ),
Filter(BADetail, BADetail[SchoolPeriod] ="AM" && BADetail[SchoolPeriod] ="PM" 
))
 
And there is an error code.
I had thought about alternative ways of doing this, I can't figure out why this does not work?  
 
Through the process I'd like to be able to create the summary chart below   The correct answer for full absences is 3.0
 

Person#       Date              #absent 

 1                 9/1/2019          1.0

 1                 9/3/2019          1.0

 1                 9/8/2019          1.0

 

 
Eventually, I'd like to find a way to note that some people were off full day, but used two different reasons.  This will be another day. 
 
 

 Much appreciated for any thoughts?

 

Thanks again 

 
 
 

2 Replies

  • sturlaws's avatar
    sturlaws
    Resident Rockstar

    Hi, SO,

     

    could you try this dax code:

    Measure =
    VAR _am =
        CALCULATETABLE (
            VALUES ( 'Table'[Person#] );
            FILTER ( 'Table'; 'Table'[Period] = "am" )
        )
    VAR _pm =
        CALCULATETABLE (
            VALUES ( 'Table'[Person#] );
            FILTER ( 'Table'; 'Table'[Period] = "pm" )
        )
    RETURN
        COUNTROWS ( INTERSECT ( _am; _pm ) )

     

    Cheers,
    Sturla

    If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

    • SO's avatar
      SO
      Helper III

      Dear Strula, 

       

      First, I want to thank you very much for responding and being so helpful!

       

      I tried to enter in your code into the measure and I noticed that I needed to convert the ";" to ","s for the code to work. 

      When I did this, the measure produced a count of 1.  I don't think the commas vs semicolons would make a difference in the outcome though?

       

      Second, I should have added that I was looking to count only those days where a person was absent for both the AM and the PM and ignore the days where they were absent in either of the AM or the PM.  

       

      Again, thank you for your input and efforts. 

       

      Much appreciated.