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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
significant
Frequent Visitor

Calculate weekly moving average for blank values and beginning of year

I need to create a rolling avarage with a window of 5 weeks.

 

The following measure:

CALCULATE(if(countrows(values(logData[dateStampWeek])) = 1,
	CALCULATE(
		 AVERAGEX(VALUES(logData[dateStampWeek]), logData[sumHoursDayPart])
		 ,logData[dateStampWeek] <= VALUES(logData[dateStampWeek]) &&  logData[dateStampWeek] > VALUES(logData[dateStampWeek])-5
		)
, blank()), DATESBETWEEN('Calendar'[Date],STARTOFYEAR('Calendar'[Date]),ENDOFMONTH('Calendar'[Date])))

works partly and I get stuck with two issues, namely:

 1 for the first 4 weeks the numbers of last year are not taking into account

 2 for blank values the avarage is not calculated

2018-10-09_11h58_44.png

The image shows the weeknumbers, values, and avarages as calculated by the measure respectively.

 

Issue 1 arrires from the fact that I bound the calculation to a year which of the selected user input. Issue 2 arrises from the values approach.

Mitigating issue 2 by using a startOfWeek date with a datedifference in days does not yield correct results.

 

Can one hint me on the correct approach?

1 REPLY 1
v-lili6-msft
Community Support
Community Support

hi, @significant

You may try to use this measure:

Measure 2 = var numberweek = CALCULATE (
    IF (
        COUNTROWS ( VALUES ( logData[dateStampWeek] ) ) = 1,
        CALCULATE (
            COUNTROWS(  VALUES ( logData[dateStampWeek] )),
            logData[dateStampWeek] <= VALUES ( logData[dateStampWeek] )
                && logData[dateStampWeek]
                    > VALUES ( logData[dateStampWeek] ) - 5
        ),
        BLANK ()
    )) return
CALCULATE (
    IF (
        COUNTROWS ( VALUES ( logData[dateStampWeek] ) ) = 1,
        CALCULATE (
            SUMX(  VALUES ( logData[dateStampWeek] ), CALCULATE(SUM(logData[sumHoursDayPart])) ),
            logData[dateStampWeek] <= VALUES ( logData[dateStampWeek] )
                && logData[dateStampWeek]
                    > VALUES ( logData[dateStampWeek] ) - 5
        )/numberweek,
        BLANK ()
    ))

Result:

8.PNG

and weeknumbers is you bound the calculation to a year which of the selected user input

 

 

Best Regards,

Lin

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

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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