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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Moving average for non-consecutive dates.

I understand that there have been some articles regarding this issue, however, none of them seem to work for me. Would like some help specifically to my situation/data.

 

Basically I have a table with a few columns. Date, country, size. I would like to calculate the 7 day moving average for a country's size
(E.g. Libya's size). However, Libya does not have size data for all the dates, as shown in the picture below. The moving average formula I used, as shown in the other picture, includes the days where Libya does not have data. What formula should I be using if I want the moving average to consist of 7 non-zero days?

Thanks for the help.

mathiasljy_2-1596506645837.png

 

mathiasljy_1-1596506616294.png

mathiasljy_3-1596506665201.png

 

 

9 REPLIES 9
mahoneypat
Microsoft Employee
Microsoft Employee

Please try this measure expression in a table or matrix visual with the Date and Country columns in it.

 

MA7 =
VAR thisdate =
    MIN ( Test[Date] )
RETURN
    CALCULATE (
        AVERAGE ( Test[Size] ),
        ALL ( Test[Date] ),
        TOPN (
            7,
            FILTER ( ALL ( Test[Date] ), Test[Date] <= thisdate ),
            Test[Date], DESC
        )
    )

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Hi @mahoneypat ,

 

It does not seem to work. Below are the results in PowerBI (Date, Country, Size, MA7), vs. what I calculated manually via Excel.

Thanks!

mathiasljy_0-1596508281922.png

 

@Anonymous , Try @mahoneypat solution. If it does not work out.

Can you share sample data and sample output in table format? 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

I put in your sample data and tried that measure out, and got the results below that match with what I calculate in Excel too.  It is just the MA7 of the Size column (no extra math beyond that).  Please reply if you figure out the difference in what we are doing.

 

mahoneypat_0-1596509456766.png

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Hi @mahoneypat ,

Yeah yours looks great. Not sure why it doesnt work on mine, maybe because of the different filters on size? I'll let you know if i figure it out thanks!

Anonymous
Not applicable

Hi @amitchandak ,

I have done @mahoneypat 's method, and have the results in the screenshot as above. Unfortunately, still not getting the correct results, thanks!

amitchandak
Super User
Super User

@Anonymous , create a dense Rank on the date inside the country and try a formula like this

 

Like

Last 7 Dates = CALCULATE(SUM(Sales[Net Sales]),FILTER(allselected('Table'),'Table'[Rank]>=min('Table'[Rank])-7
&& 'Table'[Rank]<=max('Table'[Rank])))

or

 

Last 7 Dates = CALCULATE(SUM(Sales[Net Sales]),FILTER(allselected('Table'),'Table'[Rank]>=min('Table'[Rank])-7
&& 'Table'[Rank]<=max('Table'[Rank]) && && 'Table'[Country]=max('Table'[Country]) ))

 

 

https://community.powerbi.com/t5/Community-Blog/Any-Weekday-Week-Decoding-Date-and-Calendar-2-5-Power-BI-Turning/ba-p/1187482

https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-Last-Week/ba-p/1051123

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

Hi @amitchandak,
To create a rank on the date inside the country, how should I do it?
Do i create a conditional column for the dates and size when country = "Libya", then index that column? 
Thanks!

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors