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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
WhoCares535454
Regular Visitor

Counting the number of times a sale has happened within the last 12 months from the date of sale

Hi there. Dax is being annoying.

 

I have a large table containing the ID of sales, the date of the sale, a list of product IDs, and a customer name. Something like this:

 

Sale IDDateProduct IDCustomer
107/02/2020aHarry
229/04/2021bHarry
304/10/2020aDave
425/01/2021aHarry
531/12/2021bDave
607/10/2021bDave
712/01/2021bHarry
820/05/2020aHarry
901/12/2021aHarry
1011/04/2021bDave
1125/12/2021aDave

 

What I want is a new column. For each sale, I want to know how many of this product we've sold to harry in the year before it. In other words, For each sale ID, it will calculate the number of sales we've had:

- within the previous 12 months of the sale date

- to Harry (we don't care about Dave, those rows can just be blank)

- of the same product ID

So for example for sale ID 4, it would return the number of sales of product ID A, between 25/01/2020 and 25/01/2021, which in this case is only 1 - Sale ID 1, which took place on 07/02/2020 (English date formatting)

Currently my code looks something like this:

countofsales = var saledate =Table1[Date]
return
calculate(
countrows(
filter(
table1, table1[date]<=saledate)),
allexcept(table1, table1[sale id])

This works nicely to count the number of sales over all time, but I can't figure out a way to count the sales from only the 12 months previous to the current sale. Can anyone help?

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

Hi, @WhoCares535454 ;

You could create a column by dax.

Column = 
IF([Customer]<>"Harry",BLANK(),
CALCULATE(COUNT('Table'[Sale ID]),
          FILTER('Table', [Customer]="Harry"&&[Date]<=EARLIER('Table'[Date])&&[Date]>=EDATE(EARLIER([Date]),-12))))

The final show:

vyalanwumsft_0-1670309827752.png


Best Regards,
Community Support Team _ Yalan Wu
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

1 REPLY 1
v-yalanwu-msft
Community Support
Community Support

Hi, @WhoCares535454 ;

You could create a column by dax.

Column = 
IF([Customer]<>"Harry",BLANK(),
CALCULATE(COUNT('Table'[Sale ID]),
          FILTER('Table', [Customer]="Harry"&&[Date]<=EARLIER('Table'[Date])&&[Date]>=EDATE(EARLIER([Date]),-12))))

The final show:

vyalanwumsft_0-1670309827752.png


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.