Forum Discussion

homboy27's avatar
homboy27
Helper III
1 year ago

Last 2 weeks average/ employee

Please see below data. I am calculating total sales divided by total hours in column I by employee. But I would like to calculate last 2 weeks average by employee in column J. 

 

So for example in Column J, I am calculating last 2 weeks average per employee. How would I do that in Power BI? How would I create column J? So for example for Mike it is calculating (10+10)/(25+25) and for Sonia it is calculating (10+15)/(25+25)

 

 

 

 

 

8 Replies

    • homboy27's avatar
      homboy27
      Helper III

      thank you, what is the formuala if I wanted to do average of last 8 weeks?

      • v-sathmakuri's avatar
        v-sathmakuri
        Community Support

        Hi homboy27 ,

         

        Thank you for reaching out to Microsoft Fabric Community.

         

        please find the below measure to calculate for 8 weeks. Also attached the pbix file for reference.

         

        Last 8 Weeks Average =
        VAR __WeekEnding = MAX ( 'Table'[Week Ending] )
        VAR __WeekStart = __WeekEnding - 56
        RETURN
        AVERAGEX (
            FILTER (
                ALLSELECTED ( 'Table'[Week Ending] ),
                'Table'[Week Ending] >= __WeekStart && 'Table'[Week Ending] <= __WeekEnding
            ),
            [% Hours]
        )
         

        If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it! 

  • homboy27 change the DAX measure to this, and replace the value of __lastHowManyWeeks with 8 or whatever value you want, you can also make it dynamic:

     

    Last x Weeks Average = 
    VAR __LastHowManyWeeks = 2 --change this to number of weeks you are looking for.
    VAR __WeekEnding = MAX ( 'Table'[Week Ending] )
    VAR __WeekStart = __WeekEnding - ( ( __LastHowManyWeeks - 1 ) * 7 )
    RETURN
    AVERAGEX (
        FILTER ( 
            ALLSELECTED ( 'Table'[Week Ending] ),
            'Table'[Week Ending] >= __WeekStart && 'Table'[Week Ending] <= __WeekEnding
        ),
        [% Hours]
    )
    
    • homboy27's avatar
      homboy27
      Helper III

      Thank you, what do you mean by make it more dynamic?

  • v-sathmakuri's avatar
    v-sathmakuri
    Community Support

    Hi homboy27 ,

     

    May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

     

    Thank you!!