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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Anonymous
Not applicable

Calculate the sum for the last 3 days of data where amount > 0

Trying to sum amounts by company for the last 3 days that they had any amount. So if a company had amounts yesterday, 4 days ago, and 7 days ago I would want to only sum those amounts.  Creating a date rank almost solved this but since its ranking every day regardless of company, the lookback function below in my measure is missing data.

 

This is what the overall table looks like with all companies. 

Company Amount Date date Rank
A  $          10 5/17/2021 91
B  $          16 5/16/2021 90
C  $          11 5/15/2021 89
C  $          13 5/14/2021 88
A  $          15 5/12/201 87
D  $            9 5/12/2021 87
D  $            9 5/10/2021 86
A  $            8 5/10/2021 86
A  $          12 5/1/2021 85

date rank = RANKX('Table1','Table 1'[Date],,ASC,Dense)  

Last 3 Days of Amount =
var _max = MAXX(allselected('Table 1'), 'Table 1'[date rank])
return
CALCULATE(SUM('Table 1'[Total Amount]),'Table 1',ALL('Table 1'),'Table 1'[date rank]>=_max-3)
 
The second formula would work if there was only one company in the dataset but since there are multiple companies then when it is filtered to company A for example the _max - 3 only goes back to the rank of 88 so it doesn't capture the amounts on 5/12 and 5/1 see below:
Company Amount Date date Rank
A  $   10 5/17/2021 91
A  $   15 5/12/2021 87
A  $   12 5/1/2021 85
1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Anonymous , Try with these changes

//column

date rank = RANKX(filter('Table1', [Company] =earlier([Company])),'Table 1'[Date],,ASC,Dense)

 

 

//measure
Last 3 Days of Amount =
var _max = MAXX(filter(allselected('Table 1'),[Company] =max([Company])), 'Table 1'[date rank])
return
CALCULATE(SUM('Table 1'[Total Amount]),'Table 1',ALL('Table 1'),'Table 1'[date rank]>=_max-3)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

2 REPLIES 2
Jihwan_Kim
Super User
Super User

Hi, @Anonymous 

I tried to create a sample pbix file based on the explanation. I added some more random data for the company B and C to check.

Please check the below picture and the sample pbix file's link down below.

 

Picture2.png

 

Amount total by value created recent 3 days =
VAR newtable =
ADDCOLUMNS (
SUMMARIZE ( 'Table', 'Table'[Company], 'Table'[Date] ),
"@recent3days",
SUMX (
KEEPFILTERS (
TOPN (
3,
CALCULATETABLE ( 'Table', ALL ( 'Table'[Date] ) ),
CALCULATE ( MAX ( 'Table'[Date] ) ), DESC
)
),
CALCULATE ( SUM ( 'Table'[Amount] ) )
)
)
RETURN
SUMX ( newtable, [@recent3days] )
 
 

Hi, My name is Jihwan Kim.


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.


Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
amitchandak
Super User
Super User

@Anonymous , Try with these changes

//column

date rank = RANKX(filter('Table1', [Company] =earlier([Company])),'Table 1'[Date],,ASC,Dense)

 

 

//measure
Last 3 Days of Amount =
var _max = MAXX(filter(allselected('Table 1'),[Company] =max([Company])), 'Table 1'[date rank])
return
CALCULATE(SUM('Table 1'[Total Amount]),'Table 1',ALL('Table 1'),'Table 1'[date rank]>=_max-3)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

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.