Forum Discussion

renegar's avatar
renegar
Microsoft Employee
6 years ago
Solved

Count Distinct date based on current row condition

I have a table [sample] with the following structure   Date        Category1       Category2     Result 1-1-19     Blue                Square 3-1-19     Blue                Square          1  3-...
  • jdbuchanan71's avatar
    jdbuchanan71
    6 years ago

    Hello renegar 

    Give something like this a try.

    Date Count = 
    VAR _RowDate = 'Table'[Date]
    RETURN
    CALCULATE ( 
        COUNTROWS ( VALUES ( 'Table'[Date] ) ), 
        ALLEXCEPT ( 'Table', 'Table'[Category 1], 'Table'[Category 2] ),
        'Table'[Date] < _RowDate
    )

    VALUES ( 'Table'[Date] ) gives us the unique list of dates for that row
    we expand it to the range of Cat 1 and Cat 2 with ALLEXCEPT ( 'Table', 'Table'[Category 1], 'Table'[Category 2] )
    then bring it back to just the lower dates with the 'Table'[Date] < _RowDate

    Then we count the rows left so the number of unique dates for that cat 1 and cat 2 that are lower than the current row date.