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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
PieInDaFace
Frequent Visitor

Filtering with DatesInPeriod and Max

Howdy all,

 

I’ve been trying to get customers previous 3 sales for a while, and below is what I came up with, but man is it slow. Researching more, it seems due to using a max in the filter that causes the sluggish calculation.

I’m hoping somebody can point me in the right direction. I’d hate to use sql to resolve the issue…..

 

Sales:=SUM([gross sales])

 

Previous three Sales :=CALCULATE([Sales], DATESINPERIOD(‘Dates'[TimeDate],LASTDATE('Dates'[TimeDate]),-13,MONTH), Customers[Territory_Group] = "OOT", FILTER( ALLEXCEPT('Customer History', 'Customers'[CustomerID]), 'Customer History'[TripNumber] > MAX('Customer History'[TripNumber] ) - 3))

1 ACCEPTED SOLUTION

@v-ljerr-msft,

 

Thanks for the reply. Turns out I was being a dunce and completely forgot about how to use the TopN function....

 

Below is what I came up with for a faster measurement. 

 

Max Trip Number:= MAX([TripNumber])

 

Previous three Sales:=CALCULATE([Sales], Customers[Territory_Group] = "OOT", TOPN(3,DATESINPERIOD(‘Dates'[TimeDate],LASTDATE('Dates'[TimeDate]),-13,MONTH),[Max Trip Number],DESC ))

 

For the table structure,

 

Three tables

  • Customer table – distinct customers
  • Sales table – distinct sales, by customer, by date
  • Dates table – distinct date

The relationship

  • Sales has a many to one between the customers table and dates table.
    • Many sales to one customer
    • Many sales to one date

View solution in original post

3 REPLIES 3
v-ljerr-msft
Microsoft Employee
Microsoft Employee

Hi @PieInDaFace,

 

Could you post your table structures(including the relationships) with some sample/mock data which can help us to reproduce the issue, so that we can better assist on it? It's better that you can share a sample pbix file(with just some sample/mock data) which can reproduce the issue. You can upload it to OneDrive or Dropbox and post the link here. Do mask sensitive data before uploading. Smiley Happy

 

Regards

@v-ljerr-msft,

 

Thanks for the reply. Turns out I was being a dunce and completely forgot about how to use the TopN function....

 

Below is what I came up with for a faster measurement. 

 

Max Trip Number:= MAX([TripNumber])

 

Previous three Sales:=CALCULATE([Sales], Customers[Territory_Group] = "OOT", TOPN(3,DATESINPERIOD(‘Dates'[TimeDate],LASTDATE('Dates'[TimeDate]),-13,MONTH),[Max Trip Number],DESC ))

 

For the table structure,

 

Three tables

  • Customer table – distinct customers
  • Sales table – distinct sales, by customer, by date
  • Dates table – distinct date

The relationship

  • Sales has a many to one between the customers table and dates table.
    • Many sales to one customer
    • Many sales to one date

Hi @PieInDaFace,

 

Brilliant! Could you accept your reply above as solution to close this thread(it will also help others who have similar issue easily find the answer). Smiley Happy

 

Regards

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors