Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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
1aaa
2bbb
3ccc


Ex: Sales 

Sales ID    Client ID    CategoryDate
11A25/11/2021
21A24/11/2021
32A24/11/2021
42A20/11/2021

 

Ex: Date of Today: 25/11/2021

My goal is to create a measure based on "Clients" table, filter by sales category with the date of last purchase, like:
Last Purchase: Today     -      nº clients: 1
Last Purchase: Yesterday   -  nº clients: 1

One measure so far:

 

Purchase Yesterday: =
CALCULATE (
    DISTINCTCOUNT ( Clients[ID] ),
    FILTER (
        Sales,
        Sales[Category] = "A"
        && IF (
                DATEDIFF (  Calculate(
                    LASTDATE ( Sales[Date] ),
                    REMOVEFILTERS(Sales[Date])), today(), DAY ) > 1
                && DATEDIFF (  Calculate(
                    LASTDATE ( Sales[Date] ),
                    REMOVEFILTERS(Sales[Date])), today(), DAY ) <= 2,
                TRUE(),
                FALSE()
            )
    )
)

 

But this measure is giving higher values than supposed.
Does anyone know a workaround for this problem?

Thank you very much,

Simao

1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

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 )
    )

vstephenmsft_0-1638435851839.png

 

 

 

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.

View solution in original post

3 REPLIES 3
v-stephen-msft
Community Support
Community Support

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 )
    )

vstephenmsft_0-1638435851839.png

 

 

 

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.

PaulDBrown
Community Champion
Community Champion

Try:

Purchase Yesterday =
VAR Yesterday =
    TODAY () - 1
RETURN
    CALCULATE (
        DISTINCTCOUNT ( Sales[Client ID] ),
        FILTER (
            ALLEXCEPT ( Sales, Sales[Category], Sales[Client ID] ),
            Sales[Date] = Yesterday
        )
    )




Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Anonymous
Not applicable

Thanks for your answer,

But this does not solve my problem because if a client purchases today and yesterday will count in the two categories. I need to know the frequency of last purchase date

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors