Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Last 4 weeks Year on Year

Am calculating year on year based on last 4 weeks. My issue is for some reason my calculation seems to stop working as soon as i filter, i am working with Start of Week dates which makes it a little bit of a pain;

 

Last 4 weeks = CALCULATE(SUM(Revenue[Cost]), DATESINPERIOD(Revenue[Start of Week Date],MAX(Revenue[Start of Week Date]),-27,DAY))
 
Last 4 weeks Last Year for Comparison = CALCULATE(SUM(Revenue[Cost]), DATESINPERIOD(Revenue[Start of Week Date],(MAX(Revenue[Start of Week Date])-365),-27,day))
 
 
What am i doing wrong here?
  • Hi Anonymous ,

     

    The problem is the DATESINPERIOD(). The table Revenue inputed to DATESINPERIOD to be calculated has been filtered by field in the visual. So the result of DATESINPERIOD is not all Revenue table.

    For example , the result of MAX(Revenue[Start of Week Date]) is 2021/11/19, then the result of DATESINPERIOD will be 2021/11/1 to 2021/11/19 instead of 2021/10/24.

     

    May be you can try FILTER() instead of DATESINPERIOD().

    Last 4 weeks =
    CALCULATE(
        SUM( Revenue[Cost] ),
        FILTER(
            ALL( Revenue ),
            Revenue[Start of Week Date]
                >= MAX( Revenue[Start of Week Date] ) - 27
                && Revenue[Start of Week Date] <= MAX( Revenue[Start of Week Date] )
        )
    )
    

     

    If I misunderstood you please let me know. And you can share some example data without sensitive data.

     

    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.

     

1 Reply

  • v-chenwuz-msft's avatar
    v-chenwuz-msft
    Icon for Community Support rankCommunity Support

    Hi Anonymous ,

     

    The problem is the DATESINPERIOD(). The table Revenue inputed to DATESINPERIOD to be calculated has been filtered by field in the visual. So the result of DATESINPERIOD is not all Revenue table.

    For example , the result of MAX(Revenue[Start of Week Date]) is 2021/11/19, then the result of DATESINPERIOD will be 2021/11/1 to 2021/11/19 instead of 2021/10/24.

     

    May be you can try FILTER() instead of DATESINPERIOD().

    Last 4 weeks =
    CALCULATE(
        SUM( Revenue[Cost] ),
        FILTER(
            ALL( Revenue ),
            Revenue[Start of Week Date]
                >= MAX( Revenue[Start of Week Date] ) - 27
                && Revenue[Start of Week Date] <= MAX( Revenue[Start of Week Date] )
        )
    )
    

     

    If I misunderstood you please let me know. And you can share some example data without sensitive data.

     

    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.