Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Need help with DAX

Hi,

 

I'm trying to write a DAX query for weekly rolling avg per emp, however, I'm unable to get the desired results in my report.

I've got Week_Key, Date_Key, Emp_ID, ActualWorkingHours, Avg_Hours.

I need help with getting a weekly rolling Avg per emp. Below is sample of the data and the outcome is mentioned as "Rolling Avg Working Time per week":

EmpIDweek_KeyWorking hours on that weekAbsence hours on that weekFixed working time, from Maconomy timecardCount of week (1 if no absences)Rolling Average working time per week
11300301.0030.00
1237.57.537.50.8037.50
1340037.51.0038.39
1430037.51.0036.18
150037.51.0028.65
1637.5037.51.0030.17
1737.5037.51.0031.25
28037.537.50.000.00
21060037.51.0060.00
21170037.51.0065.00
21237.5037.51.0055.83
21339037.51.0051.63
214340301.0048.10

 

Any help on this would be appreciated!!!

Thanks in advance!

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi Anonymous ,

    I read your requirements and I do some changes with my DAX code.

    Column =
    VAR _A =
        SUMX (
            FILTER (
                ALLSELECTED ( 'Table' ),
                'Table'[EmpID] = EARLIER ( 'Table'[EmpID] )
                    && 'Table'[week_Key] <= EARLIER ( 'Table'[week_Key] )
            ),
            'Table'[Working hours on that week]
        )
            / SUMX (
                FILTER (
                    ALLSELECTED ( 'Table' ),
                    'Table'[EmpID] = EARLIER ( 'Table'[EmpID] )
                        && 'Table'[week_Key] <= EARLIER ( 'Table'[week_Key] )
                ),
                'Table'[Count of week (1 if no absences)]
            )
    RETURN
        IF ( 'Table'[Count of week (1 if no absences)] = 0, 0, _A )

    Then you will get what you want.

     

     

     

    Best Regards

    Yilong Zhou

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

6 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi,
      I've already tried that but it's giving me an error every time I input the values in it.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

    Based on your problems, here are my answers.

    write a DAX query for weekly rolling average per employee.

    Rolling Avg Working Time per week =
    VAR CurrentEmpID =
        MAX ( 'Table'[EmpID] )
    VAR CurrentWeekKey =
        MAX ( 'Table'[week_Key] )
    VAR PreviousWeeks =
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[EmpID] = CurrentEmpID
                && 'Table'[week_Key] <= CurrentWeekKey
        )
    VAR Result =
        AVERAGEX ( PreviousWeeks, 'Table'[Working hours on that week] )
    RETURN
        Result

    Then I get the result.

    But I don't get the result you need, can you give me the specific Rolling Average working time per week calculation process or give me the corresponding .pbix file?

     

     

     

    How to Get Your Question Answered Quickly 

    If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

    Best Regards

    Yilong Zhou

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous ,

      I'm unable to share the .pbix as there're certain restrictions for file sharing in my org.


      This solution isn't working for me as I have got the information in three different tables namely "DimDate",  "DimEmployee" and "FactProjectHours".

      The fields that I'm considering for this requirement are:

      TableName  FieldName
      DimEmployee  EmpID
      DimEmployee  EmpName
      DimEmployee  FixedHours per Week
      DimDate  YearKey
      DimDate  WeekKey
      FactProjectHours  WorkingHours on the given Week
      FactProjectHours  AbsenceHouse


      WeekCount ( (FixedHours Per Week - AbsenceHouse) / FixedHours Per Week) ----- This is a measure to count the week as shown in the previous data sample table.


      Please refer to the below screenshot for the expected result of rolling avg which is calculated in Excel.

       






      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Anonymous ,

        I read your requirements and I do some changes with my DAX code.

        Column =
        VAR _A =
            SUMX (
                FILTER (
                    ALLSELECTED ( 'Table' ),
                    'Table'[EmpID] = EARLIER ( 'Table'[EmpID] )
                        && 'Table'[week_Key] <= EARLIER ( 'Table'[week_Key] )
                ),
                'Table'[Working hours on that week]
            )
                / SUMX (
                    FILTER (
                        ALLSELECTED ( 'Table' ),
                        'Table'[EmpID] = EARLIER ( 'Table'[EmpID] )
                            && 'Table'[week_Key] <= EARLIER ( 'Table'[week_Key] )
                    ),
                    'Table'[Count of week (1 if no absences)]
                )
        RETURN
            IF ( 'Table'[Count of week (1 if no absences)] = 0, 0, _A )

        Then you will get what you want.

         

         

         

        Best Regards

        Yilong Zhou

        If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.