cancel
Showing results for
Did you mean:
Helper I

## Calculate Average of Last 25 Data Points

I have a data set that will continue to add records over time, but I want to average the last 25 data points and then plot it on a line chart as a straight line.  However, when I plot it my measure is recalculating the average at each date using the previous 25 data points from that date.   If I put the measure in a card I get the answer I'm looking for though.

Here is the measure I've created using an example I found on the web.

VAR thisdate =
MAX ('Final FV Alcolyzer Data (2)'[Alcolyzer DT])

VAR Last25DataPoints =
TOPN (
25,
CALCULATETABLE (
DISTINCT ('Final FV Alcolyzer Data (2)'[Alcolyzer DT]),
ALL ('Final FV Alcolyzer Data (2)'[Alcolyzer DT]),
'Final FV Alcolyzer Data (2)'[Alcolyzer DT] <= thisdate
),
'Final FV Alcolyzer Data (2)'[Alcolyzer DT], DESC
)
RETURN
CALCULATE (
Average ('Final FV Alcolyzer Data (2)'[Value]),
Last25DataPoints, REMOVEFILTERS('Final FV Alcolyzer Data (2)'[Alcolyzer DT])
)

I though the REMOVEFILTERS function would resolve the issue for me in the chart, but it is not.  So my chart has 'Final FV Alcolyzer Data (2)'[Alcolyzer DT] on the x-axis and that is also what I'm using in my date slicer.

Chart looks like this:

I have the average calculated for the entire data set on here is well.

Thank you for the help!

17 REPLIES 17
Helper I

Any other thoughts?

Frequent Visitor

Hi,

My understanding is that you want to preserve all filters in your selection except the date filter. In order to ignore the data filter column selection, you simply add the column within your ALL clause. Doing so will ignore your date selection but presever all other filter selections.

Try the following

Helper I

Another thing I just thought of is that if I have a brand that doesn't have 25 data points yet the measure returns 'blank'.  How can I make it just use however many data points I have to calculate the average and then I'll make a card on the display indicating we don't have 25 data points or something.

Guessing some sort of IF statement?

Helper I

I did this and it does now give me a static average, but when I export the data to double check the average is incorrect.  It should be 6.42 for the example I'm looking at for the last 25 values but my Power BI measure is returning 6.37.

Here is the data set I'm looking at:

 DT Value 5/21/2023 11:17 6.41 5/19/2023 16:22 6.32 5/14/2023 12:17 6.42 5/9/2023 11:47 6.33 5/9/2023 9:59 6.36 4/30/2023 14:05 6.43 4/26/2023 11:03 6.4 4/21/2023 10:34 6.29 4/18/2023 11:26 6.67 4/14/2023 12:34 6.55 4/11/2023 12:28 6.43 4/7/2023 10:34 6.41 4/5/2023 11:45 6.41 4/2/2023 12:11 6.54 3/30/2023 9:52 6.54 3/22/2023 15:54 6.39 3/21/2023 10:16 6.48 3/15/2023 15:18 6.46 3/6/2023 12:29 6.44 3/5/2023 13:44 6.49 3/1/2023 13:38 6.34 2/27/2023 17:48 6.34 2/21/2023 11:02 6.36 2/20/2023 10:41 6.37 2/19/2023 16:54 6.37 2/12/2023 12:28 6.27 2/8/2023 12:21 6.32 2/7/2023 11:58 6.36 1/23/2023 12:50 6.45 1/11/2023 11:02 6.39 1/10/2023 12:49 6.39 1/3/2023 11:25 6.5 12/27/2022 13:22 6.51 12/27/2022 13:10 6.39 12/22/2022 13:14 6.36 12/16/2022 13:10 6.43 12/15/2022 11:05 6.43 12/5/2022 11:09 6.47 11/20/2022 11:30 6.47 11/17/2022 12:37 6.74 11/14/2022 10:25 6.67 11/11/2022 13:38 6.66 10/31/2022 10:31 6.68 10/31/2022 10:27 6.45 10/27/2022 10:14 6.63 10/24/2022 11:25 6.48 10/12/2022 9:48 6.59 10/10/2022 11:55 6.61 10/5/2022 9:15 6.67 10/5/2022 9:11 6.66 9/30/2022 11:27 6.62 9/25/2022 14:43 6.68 9/22/2022 10:11 6.73 9/18/2022 15:09 6.64 9/18/2022 15:05 6.55 9/9/2022 14:09 6.61 9/9/2022 14:05 6.74 9/7/2022 9:55 6.74 9/1/2022 9:36 6.66 8/24/2022 9:49 6.27 8/21/2022 13:08 6.26 8/12/2022 8:19 6.8 8/7/2022 13:09 6.64 8/7/2022 13:05 6.68 8/3/2022 15:02 6.66 7/28/2022 10:23 6.59 7/22/2022 10:49 6.65 7/20/2022 10:16 6.72 7/17/2022 14:44 6.61 7/14/2022 10:06 6.62 7/8/2022 11:57 6.52 7/6/2022 10:05 6.64 7/4/2022 12:37 6.72 6/27/2022 9:38 6.63 6/24/2022 13:08 6.8 6/21/2022 10:21 6.77 6/17/2022 12:38 6.79 6/13/2022 9:33 6.67 6/6/2022 9:43 6.84
Frequent Visitor

Yes that will do the trick as all selected only removes filters from current query while preserving filters from outer query

Frequent Visitor

Hi, see attached image. Does this resolve your issue?

LastXDataPoints = CALCULATE(AVERAGE(Data[Value]), TOPN(25, ALL(Data), Data[Date], DESC))​

Helper I

This has me close!  I have a straight, static line now, but it's averaging all the [value] data points together and ignoring my attribute slicer on the page.

Helper I

I changed ALL to ALLSELECTED and it worked!

Helper I

Turns out it didn't work.  When I adjust my date slicer the average is changing for some reason.

What I have is a table of brands that have attributes and then values associated to those attributes.  I am filtering by brand, attribute and analysis date and then displaying the control chart with data points and control limits.  I need the average and standard deviation calculated using the last 25 data points, but so far everything I have tried doesn't give me static numbers.  They're chaning based on the date slider.  HEre is the latest attemp that is not working:

Attribute Avg Last 25 data points =

CALCULATE(Average ('Final FV Alcolyzer Data (2)'[Value]), TOPN(25, ALLSELECTED('Final FV Alcolyzer Data (2)'), 'Final FV Alcolyzer Data (2)'[Alcolyzer DT], DESC), REMOVEFILTERS('Final FV Alcolyzer Data (2)'[Alcolyzer DT]))

I also tried this:

Attribute Avg Last 25 data points =

CALCULATE(Average ('Final FV Alcolyzer Data (2)'[Value]), TOPN(25, ALL('Final FV Alcolyzer Data (2)'), 'Final FV Alcolyzer Data (2)'[Alcolyzer DT], DESC))
Helper I

Figured something out.  It's calculating the average and std dev as long as the date range I've selected has 25 data points or more in it.  If it has less then it calculates it off of however many data points are in my date range.

My problem is still this.  I have hundreds of data points in this data set for each attribute.  I should still be able to calculate the stats on the last 25 data points regardless of what my date range is set at.  How do I do that though?

Frequent Visitor

Are you able to share sample data or a sample pbix?

Helper I

So here's an example.

The last 5 data points in my data set are below and each analyzed on a different day.

6.28

6.42

6.33

6.36

6.43

The average is 6.36 and I'd like that to be displayed as a single value across the chart and then plot the data points also on the chart.  I was able to calculate the average across the entire data set and plot this using REMOVEFILTERS to get my straight line.

Helper I

Here is the issue I'm seeing.  You can see I have 25 data points in my date range and so the regular average of the value is correct at 6.42.  The measure is returning 6.37 and I can't figure it out.

Frequent Visitor

So did you maange to resolve this using REMOVEFITLERS?

Helper I

No, the removefilters function doesn't change anything.  I'm not sure if I have it in the wrong spot or what.

Frequent Visitor

Hi,

Without the pbix, its difficult to test this but could you try the following.

Last25DataPoints = CALCULATE(Average ('Final FV Alcolyzer Data (2)'[Value]), TOPN(25, ALL('Final FV Alcolyzer Data (2)'), 'Final FV Alcolyzer Data (2)', DESC))

Helper I

That produced this error: