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

Column that can SUMIF for Last X number of days AND for values within its own source column

I am trying to build a Column in DAX that allows me to Add the last 3 Days worth of Sales for each ID. More specifically, I want this column to SUM the Sales by filtering the ID column for only results with the same ID column, and then have a condition along the lines of AND('Query1'[Day]<= Whatever the Day in that row is, 'Query1'[Day]>=  Whatever the date in that row is minus 3). "Last 3 Weeks of Sales"  below is an example of the column I'm trying to build.

GooseHelpful199_1-1727992063965.png

 

2 ACCEPTED SOLUTIONS
lbendlin
Super User
Super User

Best to use the WINDOW functions for this. Make sure you present the data aggregated by ID and Date.

 

lbendlin_0-1727997921340.png

or alternatively

lbendlin_1-1727998066840.png

 

 

View solution in original post

Kedar_Pande
Super User
Super User

Last 3 Days Sales = 
VAR CurrentDay = 'Query1'[Day]
VAR CurrentID = 'Query1'[ID]
RETURN
CALCULATE(
SUM('Query1'[Sales]),
FILTER(
'Query1',
'Query1'[ID] = CurrentID &&
'Query1'[Day] <= CurrentDay &&
'Query1'[Day] >= CurrentDay - 2
)
)


If this helped, a Kudos 👍 or Solution mark would be great!
Cheers,
Kedar Pande
www.linkedin.com/in/kedar-pande

View solution in original post

4 REPLIES 4
Kedar_Pande
Super User
Super User

Last 3 Days Sales = 
VAR CurrentDay = 'Query1'[Day]
VAR CurrentID = 'Query1'[ID]
RETURN
CALCULATE(
SUM('Query1'[Sales]),
FILTER(
'Query1',
'Query1'[ID] = CurrentID &&
'Query1'[Day] <= CurrentDay &&
'Query1'[Day] >= CurrentDay - 2
)
)


If this helped, a Kudos 👍 or Solution mark would be great!
Cheers,
Kedar Pande
www.linkedin.com/in/kedar-pande

This worked perfectly as well, thanks!

lbendlin
Super User
Super User

Best to use the WINDOW functions for this. Make sure you present the data aggregated by ID and Date.

 

lbendlin_0-1727997921340.png

or alternatively

lbendlin_1-1727998066840.png

 

 

Sorry I should have been more specific, but the example that I was showing is supposed to represent the Query, not the visual. The Sales figures you see should not be a Sum of Sales, just the raw data of the Sales. I am looking to have it as a Column because I have existing measures that Im hoping I can simply change to re-direct to this new column.

Is there a way to alter the formula so that it would work with how my raw data was presented? I tried using that formula but ran into an issue with duplicated rows being encountered in WINDOW's Relation parameter.

EDIT: I was able to adjust it to work to my needs, thanks for the help!

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