Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Calculate average with reference to another table - matrix

I'm struggling with the below case. I have created a measure which calculates total number of hours per certain activities:

SUM_Activities12 = CALCULATE(SUM(activities[TimeSpent]),activities[Activity] IN {"Activity 1", "Activity 2"})
QuarterActivityTimeSpent [h]
Q1Activity 13
Q2Activity 12
Q3Activity 36
Q3Activity 41
Then I would like to show summary of hours and averages per quarter in the matrix. The sum of hours per quarter is displayed correctly, but I have problem with calculating averages per quarter. To calculate the average per quarter, I'd like to divide sum of hours per quarter for certain activities -SUM_Activities12 measure  by value from another table, which looks like this - number of employees is different each quarter:
Year QuarterNumber of employees
Q15
Q22
Q38
 
 So I'd like the calculation to take number of employees for corresponding quarter, and then display in the matrix like below:
Rows: Quarter from Activity table
Values: SUM of hours, AVG hours per person involved in listed activities.
 
Please help!
 
  • hnguy71's avatar
    hnguy71
    1 year ago

    Hi Anonymous ,

    My apologies, seems like you've already added up number of employees in your other table. In that case, you'll just need to do the sum:

    Employees = SUM(employees[Number of employees])

     

    then divide the two measures:

    Average Employee Time = 
    VAR _Hours = [SUM_Activities12]
    VAR _Employees = [Employees]        // Employees = SUM(employees[Number of employees])
    RETURN
    DIVIDE(_Hours, _Employees)

     

    Based on your sample data I would return this expected output:

     

7 Replies

  • Hi Anonymous 

    Seems pretty easy enough. Do you have sample expected output results?

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi hnguy71 , result would be matrix which looks something like this: (numbers are random)

      QuarterTotal time spent per quarterAverage time spent per employee per quarter
      Q1344
      Q2225
      Q3687
      • hnguy71's avatar
        hnguy71
        Super User

        Hi Anonymous ,

        Got it.


        I am going to assume that you have both tables connected to a date table and the Quarter is coming from there. All you would need to do is create a new measure to return the average time spent per employee:

        Average Employee Time = 
        VAR _Hours = [SUM_Activities12]
        VAR _Employees = DISTINCTCOUNT(YOUR_EMP_TABLE[YOUR_EMP_COLUMN])
        RETURN
        DIVIDE(_Hours, _Employees) 

         

    • Anonymous's avatar
      Anonymous
      Not applicable

      Ashish_Mathur thank you, this really helped me. It turns out I had to change relations between the tables