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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

calculate current week data using current week and previous week info in DAX

Hi,
I hope everyone's well.
I am trying to create a measure that uses previous week and current week data. The table looks like the following-

 

table.JPG

I am trying to create a measure net_product_margin which is calculated by using last week's net_product_margin value - current week 'loss' + current week 'gain'. Here's the code that I tried but it doesn't work perfectly. This does calculate the net_product_margin value correctly for 8th May 2022 but since I am using the column reference_for_1_may it doesn't calculate the later week net_product_margin value correctly.

net_product_margin = var _lastweek = CALCULATE('All Measures'[reference_for_1_may], FILTER(ALL('Dim_Date'),'Dim_Date'[End_of_Week] = SELECTEDVALUE(Dim_Date[End_of_Week]) -7 )) RETURN _lastweek - 'All Measures'[loss] + 'All Measures'[profit]

This is how the formula looks in excel.

net_margin1.pngnet_margin2.png

I have the data from 1st May 2022 in my table so to have a reference I have added a reference column called reference_for_1_may that acts as reference data to calculate later week net_product_margin. I am scratching my head on this. Any help would be much appreciated.

Thank you.

2 REPLIES 2
v-xiaotang
Community Support
Community Support

Hi @Anonymous 

I just want to confirm if you resolved this issue? If yes, you can accept the answer helpful as the solution or share you method and accept it as solution, thanks for your contribution to improve Power BI.

If you need more help, please let me know.

 

Best Regards,

Community Support Team _Tang

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@Anonymous , With help from a date table and week Rank

 

new columns
Week Start date = 'Date'[Date]+-1*WEEKDAY('Date'[Date],2)+1
Week End date = 'Date'[Date]+ 7-1*WEEKDAY('Date'[Date],2)
Week Rank = RANKX(all('Date'),'Date'[Week Start date],,ASC,Dense)
OR
Week Rank = RANKX(all('Date'),'Date'[Year Week],,ASC,Dense) //YYYYWW format

 

measures
This Week = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])))
Last Week = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])-1))

 

 

or 7 days behind measure

7 behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-7,DAy))

 

 

Power BI — Week on Week and WTD
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-Last-Week/ba-p/1051123
https://www.youtube.com/watch?v=pnAesWxYgJ8

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
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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