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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
alvin_alpha
Frequent Visitor

Calculating the cumulative sum of the previous 2 wks

hi all,,

Has anyone went thru on calculating the "CurrentWeek = Cummulative sum of the Past 2Wks".

For the table below: Prev2Wks_Cummulative_SUM = "W01 QTY + W02 QTY".

Can please help to guide me how to calculate such scenarios?

 

 

alvin_alpha_1-1599816724053.png

Thanks

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

hi  @alvin_alpha 

First, Week column in your table is a text column, so you need a rank/weekno column for this text week column.

Second, you could use this simple formula to get your requirment:

Result = 
CALCULATE(SUM('Table'[QTY]),FILTER('Table','Table'[WeekNum]=EARLIER('Table'[WeekNum])-1||'Table'[WeekNum]=EARLIER('Table'[WeekNum])-2))

or use a conditional to exclude first two rows.

Result = IF(COUNTROWS(FILTER('Table','Table'[WeekNum]=EARLIER('Table'[WeekNum])-1||'Table'[WeekNum]=EARLIER('Table'[WeekNum])-2))=2,
CALCULATE(SUM('Table'[QTY]),FILTER('Table','Table'[WeekNum]=EARLIER('Table'[WeekNum])-1||'Table'[WeekNum]=EARLIER('Table'[WeekNum])-2)))

 Result:

6.JPG

 

here is sample pbix file, please try it.

Regards,

Lin

Community Support Team _ Lin
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-lili6-msft
Community Support
Community Support

hi  @alvin_alpha 

First, Week column in your table is a text column, so you need a rank/weekno column for this text week column.

Second, you could use this simple formula to get your requirment:

Result = 
CALCULATE(SUM('Table'[QTY]),FILTER('Table','Table'[WeekNum]=EARLIER('Table'[WeekNum])-1||'Table'[WeekNum]=EARLIER('Table'[WeekNum])-2))

or use a conditional to exclude first two rows.

Result = IF(COUNTROWS(FILTER('Table','Table'[WeekNum]=EARLIER('Table'[WeekNum])-1||'Table'[WeekNum]=EARLIER('Table'[WeekNum])-2))=2,
CALCULATE(SUM('Table'[QTY]),FILTER('Table','Table'[WeekNum]=EARLIER('Table'[WeekNum])-1||'Table'[WeekNum]=EARLIER('Table'[WeekNum])-2)))

 Result:

6.JPG

 

here is sample pbix file, please try it.

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Greg_Deckler
Super User
Super User

@alvin_alpha Best if you have an Index column or actual date column to use. Then one pattern is MTBF to refer to earlier rows. In general, to use a measure in that way, you need to use the Disconnected Table Trick as this article demonstrates: https://community.powerbi.com/t5/Community-Blog/Solving-Attendance-with-the-Disconnected-Table-Trick...

 

That's if you want DAX. If you want Power Query let me know, just @ me in reply.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...
amitchandak
Super User
Super User

@alvin_alpha , for that you need create a week rank on week year column on week column, prefer to have separate week/date table.

 

Week Rank = RANKX(all('Date'),'Date'[Week Start date],,ASC,Dense)  ///You use Week , any correct sortable column

 

last two weeks = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]<=max('Date'[Week Rank])-1

&& 'Date'[Week Rank]>=max('Date'[Week Rank])-3))
This Week = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])))
Last Week = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])-1))

 

Week -

https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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