Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Filter on the dimClient based on factSales

Hello DAX experts,   I have two tables, a customer table and a sales table, linked by the client ID, in a one-to-many relationship.   Ex: Clients Client ID     Name 1 aaa 2 bbb 3 ...
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Anonymous ,

     

    Here's my solution. 

    1. Create a meaure to get the ranking. Calculate the ranking based on the date, and then the first one is the last purchase date.

    Rank =
    RANKX (
        FILTER ( ALL ( 'Sales' ), [Client ID] = MAX ( 'Sales'[Client ID] ) ),
        CALCULATE ( MAX ( 'Sales'[Date] ) ),
        ,
        DESC,
        DENSE
    )
    

    If there are other categories, you can modify it to

    Rank =
    RANKX (
        FILTER (
            ALL ( 'Sales' ),
            [Client ID] = MAX ( 'Sales'[Client ID] )
                && [Category] = MAX ( 'Sales'[Category] )
        ),
        CALCULATE ( MAX ( 'Sales'[Date] ) ),
        ,
        DESC,
        DENSE
    )
    

     

    2.Create the following two measures to get the results.

    Purchase Yesterday: = VAR _TODAY=DATE(2021,11,25)
    RETURN CALCULATE(DISTINCTCOUNT(Sales[Client ID]),FILTER('Sales',[Category]="A"&&[Date]=_TODAY-1&&[Rank]=1))Purchase Today: = 
    VAR _TODAY =
        DATE ( 2021, 11, 25 )
    RETURN
        CALCULATE (
            DISTINCTCOUNT ( Sales[Client ID] ),
            FILTER ( 'Sales', [Category] = "A" && [Date] = _TODAY && [Rank] = 1 )
        )
    
    Purchase Yesterday: = 
    VAR _TODAY =
        DATE ( 2021, 11, 25 )
    RETURN
        CALCULATE (
            DISTINCTCOUNT ( Sales[Client ID] ),
            FILTER ( 'Sales', [Category] = "A" && [Date] = _TODAY - 1 && [Rank] = 1 )
        )
    

     

     

     

    Best Regards,

    Stephen Tao

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.