How can I calculate a moving average with a variable period in DAX

0 votes

How can I calculate a moving average with a variable period in DAX?
I need to create a DAX measure that calculates a moving average over a variable period, allowing users to define the window size dynamically. The measure should adjust based on user selections and maintain accuracy across different date ranges. What is the best approach using DAX functions like AVERAGEX, FILTER, or DATESINPERIOD to ensure flexibility and correct calculations?

1 day ago in Power BI by Evanjalin
• 22,610 points
22 views

1 answer to this question.

0 votes

Moving average computation with variable periods in DAX would require a measure that adjusts dynamically according to the user's selection of computed values for different ranges created by dates. To achieve this, you could use a combination of AVERAGEX, FILTER, and DATESINPERIOD to create a dynamic moving average measure.

Best Approach Using DAX

Design a Parameter Table for Moving Average Window

Create a table called MovingAvgPeriod, having values like 7, 14, and 30, and give users the possibility to choose this period.

Add this as a disconnected table (with no relationships).

Use a slicer to let the user select the window's size.

Define the Dynamic Moving Average Measure

MovingAvg = 
VAR SelectedPeriod = SELECTEDVALUE(MovingAvgPeriod[Days], 7) 
RETURN 
AVERAGEX(
    DATESINPERIOD(Sales[Date], MAX(Sales[Date]), -SelectedPeriod, DAY), 
    CALCULATE(SUM(Sales[Amount]))
)

Ensure Flexibility and Accuracy

  • DATESINPERIOD dynamically selects the date range based on the user-defined window.

  • MAX(Sales[Date]) ensures the moving average is calculated for the most recent date in context.

  • CALCULATE(SUM(Sales[Amount])) ensures proper aggregation within the selected period.

answered 1 day ago by anonymous
• 22,610 points

Related Questions In Power BI

0 votes
1 answer

In DAX, how do I RETURN the sum of a calculated column from a DAX Table Variable (created via ADDCOLUMN)?

You can access column variables of previously ...READ MORE

answered Oct 8, 2020 in Power BI by Gitika
• 65,770 points
4,746 views
+1 vote
2 answers

How can I troubleshoot the “A circular dependency was detected” error in complex DAX calculations?

The error message "A circular dependency was ...READ MORE

answered Nov 7, 2024 in Power BI by pooja
• 21,730 points
203 views
0 votes
1 answer

How do I calculate a rolling average or cumulative total in Power BI without performance issues?

To enable rolling averages or cumulating summations ...READ MORE

answered Dec 30, 2024 in Power BI by Anu
• 3,020 points

edited Mar 6 165 views
0 votes
1 answer

I need to show a trendline for a moving average but only for selected date ranges—how can I do this?

To do a Moving Average Trendline that ...READ MORE

answered Mar 19 in Power BI by anonymous
• 22,610 points
30 views
+2 votes
0 answers

Assume referential integrity

1) What is Assume referential integrity? what ...READ MORE

Aug 19, 2019 in Power BI by vnk
1,950 views
+2 votes
1 answer

If Condition

DAX - I assume [Claim_line].[Allowed_Amount_B6] is one ...READ MORE

answered Aug 20, 2019 in Power BI by anonymous
• 33,050 points
1,754 views
+2 votes
1 answer

How to export data to CSV from power bi embedded url ?

Hi Arathi, You can open Visuals from embedded ...READ MORE

answered Aug 23, 2019 in Power BI by anonymous
• 33,050 points
6,799 views
+1 vote
1 answer

Power Bi to Machine Learning Model

Hello @vnk! Power BI only supports Azure Machine ...READ MORE

answered Dec 3, 2019 in Power BI by Priyanka
1,333 views
0 votes
1 answer

I need to calculate a running total but reset it at the start of each new quarter—how can I achieve this in DAX?

To calculate a running total that resets ...READ MORE

answered Mar 7 in Power BI by anonymous
• 22,610 points
68 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP