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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
DRL
New Member

Average results over the last 4 records

Dear community, 

 

I have searched to solve this but did not find an answer that solves what I trying to achieve. 

We are capturing the result of chemical analysis on our samples. 

We use a table (in Excel currently) that averages the results over the last 4 samples, 3 days, week and month. 

 

I am struggling to find the way to create the average for the last 4 records. 

 

The table is as follow

 

 

Analysis typeSampling date and timeResult 1 (%)Result 2 (%)Result 3 (%)
Analysis 102/22/2022 08:00:001480.01
Analysis 202/22/2022 08:00:001.2470.02
Analysis 102/22/2022 10:00:000.8490.03
Analysis 202/22/2022 10:00:000.6500.02
Analysis 102/22/2022 12:00:001.5470.04
Analysis 202/22/2022 12:00:002.6460.03
Analysis 102/22/2022 14:00:000.147.50.05
Analysis 202/22/2022 14:00:000.4480.12

 

In this case, I'd like to have the following result for Analysis 1:

Sampling periodResult 1 (%)Result 2 (%)Result 3 (%)
Last 4 samples0.8547.8750.13
Last 3 daysAvAvAv
Last weekAvAvAv
Last monthAvAvAv

 

Could you please help? 

 

Thank you so much for your time, 

1 ACCEPTED SOLUTION
Daryl-Lynch-Bzy
Resident Rockstar
Resident Rockstar

Hi @DRL , I hope you find the following useful.

 

If I create the following DAX function, the result is 1.025 (for the table above).

Average Result 1 (All) = AVERAGE( table[Result 1] )

In your scenario you want separate Analysis 1 and Analysis 2 results, so you use the CALCULATE function to apply filter context.

Average Analysis 1 = CALCULATE ( AVERAGE( table[Result 1] ) 
, table[Analysis type] = "Analysis 1" ) --0.85
Average Analysis 2 = CALCULATE ( AVERAGE( table[Result 1] )
, table[Analysis type] = "Analysis 2" ) --1.2

Consider the scenario where the above is filtered to Date

Average Analysis 1 Last Month = CALCULATE ( AVERAGE( table[Result 1] ) 
, table[Analysis type] = "Analysis 1"
, PREVIOUSMONTH( calendar[Date] ) )
Average Analysis 1 Last Week = CALCULATE ( AVERAGE( table[Result 1] )
, table[Analysis type] = "Analysis 1"
, DATEADD( calendar[Start of Week] , -7 , DAY ) )

The last week calculation requires are Calendar table.  Refer here for instructions:

Week to Date Calculation in Power BI with DAX - RADACAD

 

To get the last 4 samples your "table" would need a Sequential Index (add using Power Query).  
Add an index column (Power Query) (microsoft.com)

 

With the index column, the following is possible:

 

Average Analysis 1 Last 4 Samples = 
VAR _maxID = MAX ( table[ID])
VAR _OffsetID = _maxID - 7 --This assumes that ROW ID is unique to each sample 1 / sample 2. If the max is 8, then 8, 6, 4, 2 are included.
RETURN
CALCULATE ( AVERAGE( table[Result 1] )
, table[Analysis type] = "Analysis 1"
, KEEPFILTERS ( table[ID] > _OffsetID ) )

 

 

View solution in original post

1 REPLY 1
Daryl-Lynch-Bzy
Resident Rockstar
Resident Rockstar

Hi @DRL , I hope you find the following useful.

 

If I create the following DAX function, the result is 1.025 (for the table above).

Average Result 1 (All) = AVERAGE( table[Result 1] )

In your scenario you want separate Analysis 1 and Analysis 2 results, so you use the CALCULATE function to apply filter context.

Average Analysis 1 = CALCULATE ( AVERAGE( table[Result 1] ) 
, table[Analysis type] = "Analysis 1" ) --0.85
Average Analysis 2 = CALCULATE ( AVERAGE( table[Result 1] )
, table[Analysis type] = "Analysis 2" ) --1.2

Consider the scenario where the above is filtered to Date

Average Analysis 1 Last Month = CALCULATE ( AVERAGE( table[Result 1] ) 
, table[Analysis type] = "Analysis 1"
, PREVIOUSMONTH( calendar[Date] ) )
Average Analysis 1 Last Week = CALCULATE ( AVERAGE( table[Result 1] )
, table[Analysis type] = "Analysis 1"
, DATEADD( calendar[Start of Week] , -7 , DAY ) )

The last week calculation requires are Calendar table.  Refer here for instructions:

Week to Date Calculation in Power BI with DAX - RADACAD

 

To get the last 4 samples your "table" would need a Sequential Index (add using Power Query).  
Add an index column (Power Query) (microsoft.com)

 

With the index column, the following is possible:

 

Average Analysis 1 Last 4 Samples = 
VAR _maxID = MAX ( table[ID])
VAR _OffsetID = _maxID - 7 --This assumes that ROW ID is unique to each sample 1 / sample 2. If the max is 8, then 8, 6, 4, 2 are included.
RETURN
CALCULATE ( AVERAGE( table[Result 1] )
, table[Analysis type] = "Analysis 1"
, KEEPFILTERS ( table[ID] > _OffsetID ) )

 

 

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.