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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
mbailey
Helper II
Helper II

Moving Weekly Average Excluding Weekends

I know there's been many posts on moving averages and I have tried many suggestions but can't seem to find a simple one that will work.

I have two tables I'm working with. The first, DMTA_OnTimeShippingSummary has columns: due_date, number_of_order_lines_to_ship, number_of_order_lines_shipped, percent_in_time_shippng. 

The second table is a Date_Table. There is a relationship between the two tables on DMTA_OnTimeShippingSummary[due_date] and Date_Table[Date]. The Date_Table has a column, IsWorkday, with values of 1 for a workday and 0 for weekends and holidays. Table examples below.

I want to create a Measure that calculates a moving weekly average excluding weekends and holidays.

Based on another post (Link ) I've attempted this formula below, but It gives an error (highlighted) of "EARLIER/EARLIEST refers to an earlier row context which does not exist."

One thing I noticed, if it matters, as shown in the screenshots below is that the Date_Table[Date] field has a calendar icon next to it, but the  DMTA_OnTimeShippingSummary[due_date] does not - even though both are  DataType: Date/Time.

If this can be fixed - great, if someone has a better solution - even better.

Thank you in advance for any help and suggestions!  -Mike

Weekly Avg =

VAR Last5Workdays =
SELECTCOLUMNS (
TOPN (
5,
FILTER (
Date_Table,
Date_Table[Date] <= EARLIER (DMTA_OnTimeShipmentSummary[due_date]<---- locaton of error
&& Date_Table[IsWorkday] = 1
),
Date_Table[Date], DESC
),
"Workday", Date_Table[Date]
)
RETURN
CALCULATE (
SUM ( DMTA_OnTimeShipmentSummary[percent_on_time_shipment] ),
DMTA_OnTimeShipmentSummary[due_date] IN Last5Workdays) / 5

 

Tables:

OnTimeShipmentSummary.png

Date_table.png

weeklyaverage error.png

OnTimeSummary Fields.pngdue_date format.png

 
7 REPLIES 7
Anonymous
Not applicable

Hi @mbailey ,

 

Is your formula a measure?

EARLIER() is a DAX function that acts exclusively on row context, and its purpose is to create a reference to a column value on an outer loop of the evaluation of the expression. It is commonly used in calculated columns during nested row by row iterations.

 

Best Regards,

Jay

 

To answer your question, Yes, this is a Measure I'm trying to create.  But, I'm not sure if this is the best or correct way to accomplish what I'm wanting. I'm assuming your response means that I can't use the function in this manner. So, I guess then the question becomes: How can I calculate the moving weekly average given my data?

 

Thanks for your help,

Mike 

 

Hi @mbailey ,

 

Have you tried replacing the filter function with the information I provided?

 

Use the Date table column on the earlier function?


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português





Yes, and it still produces an error. See below.

 

Weekly average Earlier error.png

Hi @mbailey ,

 

Not really sure if I got this correctly but believe that you don't need to have the isworking day.

 

If you pickup the maximum date - 6 days you will get all values within the week so last 5 days.

 

Try the following measure:

Weekly Avg =
CALCULATE (
    SUM ( DMTA_OnTimeShipmentSummary[percent_on_time_shipment] );
    FILTER (
        ALL ( DMTA_OnTimeShipmentSummary[due_date] );
        DMTA_OnTimeShipmentSummary[due_date] <= MAX ( Date_Table[Date] )
            && DMTA_OnTimeShipmentSummary[due_date]
                >= SELECTEDVALUE ( Date_Table[Date] ) - 6
    )
) / 5

Check if the calculations is correct based on your values. If not the only adjustment that may need to be done is the calculation to pick up the lower date.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português





MFelix
Super User
Super User

Hi @mbailey ,

 

The error on your measure is that you need to refer to the Date column so in your case the formula for the filter would be:

 

FILTER (
Date_Table,
Date_Table[Date] <= EARLIER (Date_Table[Date])  <---- locaton of error
&& Date_Table[IsWorkday] = 1
)

 

Using the EARLIER function you are abasically picking up the previous value of the date column so since you are in a calculated column that uses the row context you cannot pickup values from other tables.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português





Hi Miguel,

 

I'm assuming that based on the reply after yours that I can't use this formula in the manner I was trying. So I guess I'm back to just asking how can I create the moving weekly average?

 

Thanks for your help,

Mike

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 2026 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.