Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
Maheshguptaz
Helper III
Helper III

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!

1 ACCEPTED SOLUTION

Hi @Maheshguptaz ,

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.

vyilongmsft_0-1711011997296.png

 

 

 

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.

View solution in original post

6 REPLIES 6
v-yilong-msft
Community Support
Community Support

Hi @Maheshguptaz ,

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.

vyilongmsft_0-1709709343216.png

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.

Hi @v-yilong-msft ,

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.

Maheshguptaz_0-1709895822663.png

 






Hi @Maheshguptaz ,

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.

vyilongmsft_0-1711011997296.png

 

 

 

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.

Hello @v-yilong-msft ,

 

I don't think this DAX will work as the Week_key comes from DimDate, EmpID comes from DimEmp and WorkingHours, absenceHours and FixedWorkingTime come from FactTable.

Could you help me with DAX according to the above conditions?

regards,
Mahesh

miTutorials
Super User
Super User

Have you explored the new Visual Calculations feature yet ? If not take a look at this video, this should solve your query.

 

🚀 NEW FEATURE : Magic of Visual Calculations in Power BI | MiTutorials - YouTube

 

Regards

Ismail 

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

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

Check out the October 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

Find out what's new and trending in the Fabric Community.