Forum Discussion

mohammadyousaf's avatar
mohammadyousaf
Resolver II
4 years ago

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  ProductionCumulative produciton
1/15/2022 55
1/16/2022 712
1/17/2022 820
1/18/2022 323
1/19/2022 225
1/20/2022 025
1/21/2022 1035
1/22/2022 136
1/23/2022 137
1/24/2022 946
1/25/2022 450
1/26/2022 454
1/27/2022 660
1/28/2022 767
1/29/2022 875
1/30/2022 378
1/31/2022 583
2/1/2022 588
2/2/2022 290
2/3/2022 696
2/4/2022 4100

2 Replies

  • ValtteriN's avatar
    ValtteriN
    Community 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-msft's avatar
    v-chenwuz-msft
    Community 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.