Forum Discussion
Comparing current week with previous week production
Hi,
Can some please help to write a dax to compare current 7 days with previous 7 days (week or month) progress based on the following table.
Thank you.
| Date | Production | Cumulative produciton | |
| 1/15/2022 | 5 | 5 | |
| 1/16/2022 | 7 | 12 | |
| 1/17/2022 | 8 | 20 | |
| 1/18/2022 | 3 | 23 | |
| 1/19/2022 | 2 | 25 | |
| 1/20/2022 | 0 | 25 | |
| 1/21/2022 | 10 | 35 | |
| 1/22/2022 | 1 | 36 | |
| 1/23/2022 | 1 | 37 | |
| 1/24/2022 | 9 | 46 | |
| 1/25/2022 | 4 | 50 | |
| 1/26/2022 | 4 | 54 | |
| 1/27/2022 | 6 | 60 | |
| 1/28/2022 | 7 | 67 | |
| 1/29/2022 | 8 | 75 | |
| 1/30/2022 | 3 | 78 | |
| 1/31/2022 | 5 | 83 | |
| 2/1/2022 | 5 | 88 | |
| 2/2/2022 | 2 | 90 | |
| 2/3/2022 | 6 | 96 | |
| 2/4/2022 | 4 | 100 |
2 Replies
- ValtteriNCommunity Champion
Hi,
If you aren't comparing data on a multi year level the easiest way to do this is like this:
This week =
var _TW = WEEKNUM(MAX(Table[Date])) return
CALCULATE(Sum(Table[Production]),ALL(Table),WEEKNUM(Table[Date])=_TW)
Last week =
var _LW = WEEKNUM(MAX(Table[Date]))-1 return
CALCULATE(Sum(Table[Production]),ALL(Table),WEEKNUM(Table[Date])=_TW)
Diff = [This Week]-[Last Week]
I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!
My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/ - v-chenwuz-msftCommunity Support
Hi mohammadyousaf ,
I need to make some additions.
The code ValtteriN has some problems in encountering data across years. This is because it is calculated based on the results returned by weeknum. So this problem can be avoided by using the following formula.Current week = VAR _cweekday = WEEKDAY( SELECTEDVALUE( 'Table'[Date] ), 3 ) VAR _cstart = SELECTEDVALUE( 'Table'[Date] ) - _cweekday VAR _cend = _cstart + 6 RETURN CALCULATE( SUM( 'Table'[Production] ), FILTER( ALLSELECTED( 'Table'[Date] ), [Date] >= _cstart && [Date] <= _cend ) ) Previous week = VAR _cweekday = WEEKDAY( SELECTEDVALUE( 'Table'[Date] ), 3 ) VAR _cstart = SELECTEDVALUE( 'Table'[Date] ) - _cweekday - 7 VAR _cend = _cstart + 6 RETURN CALCULATE( SUM( 'Table'[Production] ), FILTER( ALLSELECTED( 'Table'[Date] ), [Date] >= _cstart && [Date] <= _cend ) )Pbix in the end you can refer.
Best Regards
Community Support Team _ chenwu zhu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.