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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Problem with Moving Average 12

Hi.

 

I have an issue. I was trying to replicate the formula from this post: https://community.powerbi.com/t5/Desktop/5-Day-Moving-average-No-Weekends-in-Data-and-how-I-skip-wee... in a Moving average of 12 data.

 

I have stocks information, then I just have information on weekdays, replicating this formula worked in moving average of 5, but I need to use it on 12 as well. Following is the formula: 

Movil 12 = AVERAGEX(DATESINPERIOD(Tecnicos[FECHA],MAX(Tecnicos[FECHA]),-16,DAY),[Total Precios])
 
The problem is that on mondays I get the wrong average, because counting 16 gets to a sunday (blank) and I need to get to the previous friday. But If I try to move the -16 to a -17 all the other averages will be wrong.
 
I tried looking for that information in many places I couldn't make it.
 
I aprecciate help.
6 REPLIES 6
daxer-almighty
Solution Sage
Solution Sage

// For this stuff to work you need
// Calendar (a date table in the model).

[12D MA] =
var __maxVisibleDay = MAX( 'Calendar'[Date] )
var __countOfDaysToAvgOver = 12
var __datesToAvgOver =
    TOPN( __countOfDaysToAvgOver,
        // This table gets you the correct
        // number of days of the correct
        // type but you have to remember
        // that you might be too close
        // to the beginning of the
        // Calendar and in fact you'll
        // get fewer days than needed.
        CALCULATETABLE(
            VALUES( 'Calendar'[Date] ),
            'Calendar'[DayType] = "Weekday",
            'Calendar'[Date] <= __maxVisibleDay,
            ALL( 'Calendar' )
        ),
        'Calendar'[Date],
        DESC
    )
// This check is needed in case there
// are not enough days in the calendar
// (see the comment above).
var __thereAreEnoughDays =
    COUNTROWS( __datesToAvgOver )
        = __countOfDaysToAvgOver
return
if( __thereAreEnoughDays,
    CALCULATE(
        AVERAGEX(
            __datesToAvgOver,
            [Total Precios]
        ),
        // This directive is probably not
        // needed but only if 'Calendar'
        // is a proper Date table in
        // the model. But you can leave it
        // in as it works in any setting.
        ALL( 'Calendar' )
    )
)
Greg_Deckler
Community Champion
Community Champion

@Anonymous - One, I would personally avoid DATESINPERIOD. https://community.powerbi.com/t5/Quick-Measures-Gallery/To-bleep-With-DATEADD/m-p/1259467#M583

 

You may find this helpful - https://community.powerbi.com/t5/Community-Blog/To-bleep-With-Time-Intelligence/ba-p/1260000

Also, see if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TITHW/m-p/434008

 

Finally, Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi.

 

Thank you for your feedback.

I tried using the formulas, however I couldn't make it.

 

I'm using a lot of companies to analyze, my original chart has dates in rows and companies in columns, therefore I had to unpivot table to use it in BI properly, That's how my table looks like (unpivot)

 

FECHAEmpresaPrice
23/12/2009Acerias$39.40
23/12/2009Isagen$2,165.00
23/12/2009GrupoArgos$18,980.00
24/12/2009Acerias$41.65
24/12/2009Isagen$2,200.00
24/12/2009GrupoArgos$19,000.00

 

That's an example. I have more than 2000 rows with more companies (Data since 2009)

 

What I was using with the dateadd function was MovingAverage12 = AVERAGEX(DATESINPERIOD(Tecnicos[FECHA],MAX(Tecnicos[FECHA]),-16,DAY),[Total Precios])

 

The table I am receiving is the next one:

FECHAPriceMovingAverage12
Friday, August 14, 202010,260.0010,313.00
Monday, August 17, 202010,260.0010,315.00
Tuesday, August 18, 202010,520.0010,332.00
Wednesday, August 19, 202010,660.0010,357.00
Thursday, August 20, 202010,740.0010,402.00
Friday, August 21, 202010,980.0010,453.00
Monday, August 24, 202011,320.0010,549.00
Tuesday, August 25, 202011,860.0010,658.00
Wednesday, August 26, 202011,820.0010,785.00
Thursday, August 27, 202011,900.0010,920.00

 

The values I'm looking for, are the following.

Friday, August 14, 202010,260.0010,313.33
Monday, August 17, 202010,260.0010,305.00
Tuesday, August 18, 202010,520.0010,331.67
Wednesday, August 19, 202010,660.0010,356.67
Thursday, August 20, 202010,740.0010,401.67
Friday, August 21, 202010,980.0010,453.33
Monday, August 24, 202011,320.0010,533.33
Tuesday, August 25, 202011,860.0010,658.33
Wednesday, August 26, 202011,820.0010,785.00
Thursday, August 27, 202011,900.0010,920.00

 

As you may see, monday's averages are wrong because takes data from previous 16 days. In addition, Dates Column (Fecha) Just have dates from monday to friday, there are no weekends on that table as I'm not using it.

 

Thank you once again for your support. I'll appreciate your help once again.

CNENFRNL
Community Champion
Community Champion

Hi, there

 

Weekends are supposed to be filtered out in Tecnicos[FECHA] column beforehand;

or filter down Tecnicos[FECHA] if it contains all consecutive dates

 

 

_12workdays =
TOPN (
    12,
    CALCULATETABLE (
        VALUES ( Calendar[Date] ),
        NOT WEEKDAY ( Calendar[Date] ) IN { 1, 7 },
        FILTER ( ALL ( Calendar[Date] ), Calendar[Date] <= MAX ( Calendar[Date] ) )
    )
)

 

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

Hi.

 

Thank you for your answer.

The column FECHA that contains dates just have dates from monday to friday, however in the calculation is taking weekends as well (I Think because I'm using DATESINPERIOD). 

@Anonymous - Can you just use this?

https://community.powerbi.com/t5/Quick-Measures-Gallery/Rolling-Average/m-p/160720#M3

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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