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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Tuan
Helper III
Helper III

Moving Average - Datesinperiod excluding Weekend

I been trying to caclulate a moving average ignoring weekend dates. I can't seem to figure out how to combine Datesinperiod() with Filter(). How do i filter the date table and then do the moving average?

 

My moving average calculation

50SMA = 
CALCULATE (
    AVERAGEX ( 'Stock Data', 'Stock Data'[price.close]),
    DATESINPERIOD (
        'Date'[Date],
        LASTDATE ( 'Date'[Date] ),
        -50,
        DAY
    )
)

 

My Filter

 

FILTER('Date','Date'[WeekdayNum]<>6 && 'Date'[WeekdayNum] <>0)

 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

Create a date column that does no have a weekend , means blank for week end. Create a rank on it. All wek end will get one other date will get rank. You can use now this in place of Date. -50 will give 50 working days . something like

 

CALCULATE(Average(Sales[Net Sales]),FILTER(all('Date'),'Date'[WorkDayRank]>=min('Date'[WorkDayRank])-50 && 'Date'[WorkDayRank]<=max('Date'[WorkDayRank])))

 

The problem with this approach is if someone selects week end in calendar/slicer it will not work.

 

To make that work, you have to fill the weekend with the last working date.

 

-1 from Saturday date, -2 from Sunday date

This will keep same rank for week end as friday

 

WorkingDate = if(WEEKDAY('Date'[Date],2)<6,[Date],[Date]-WEEKDAY('Date'[Date],2)+5)

View solution in original post

2 REPLIES 2
amitchandak
Super User
Super User

Create a date column that does no have a weekend , means blank for week end. Create a rank on it. All wek end will get one other date will get rank. You can use now this in place of Date. -50 will give 50 working days . something like

 

CALCULATE(Average(Sales[Net Sales]),FILTER(all('Date'),'Date'[WorkDayRank]>=min('Date'[WorkDayRank])-50 && 'Date'[WorkDayRank]<=max('Date'[WorkDayRank])))

 

The problem with this approach is if someone selects week end in calendar/slicer it will not work.

 

To make that work, you have to fill the weekend with the last working date.

 

-1 from Saturday date, -2 from Sunday date

This will keep same rank for week end as friday

 

WorkingDate = if(WEEKDAY('Date'[Date],2)<6,[Date],[Date]-WEEKDAY('Date'[Date],2)+5)

Interesting work around. It worked for me.

 

Thank you very much!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.