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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
jct999
Advocate II
Advocate II

Complex calculation with 2 steps : Sum then average.

Hello,

I have a dataset that contains Sales data.  It has the following columns :

  • Period_id (integer : 1, 2, 3,... 250, 251, ... etc)
  •  Country_id
  •  Product_id
  •  Color_id
  •  Category_id
  •  Quantity

I want set up a table widget with 2 columns :

  •  Col #1 : Period_Id
  •  Col #2 : Complex calculation :

  Step 1) Calculate the sum of quantities per Period_id, for each Period_id starting from (Period_id - 4) to Period_id of the current row.
  Step 2) Then calculate the average of the 4 sums previously calculated.

Obviously, the dataset can be also filtered with slicers on Country_id, Product_id, ...

Could you help me to write the DAX formula to perform this complex calculation ?

Thanks !


Best Regards

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

3 REPLIES 3
wdx223_Daniel
Super User
Super User

@jct999 

wdx223_Daniel_2-1611104916209.png

 

THanks wdx223_Daniel for you reply.

I have a question in order to understand deeper :

if I refer to the DAX documentation, AVERAGEX accept, as a 1st argument, a table or an expression that specifies the table over which the aggregation can be performed.

Your 1st argument for AVERAGEX if a TOPN function : TOPN(4, FILTER(...)).
This TOPN function return a table with a single column : 'Period'[Period_id] ; and it contains no column with Quantities.

So my question is : How can AVERAGEX function extract the average from this table that has only one column and no quantity values / scalar values ?

Regards

@jct999 this about context transition

when you refer a measure in the 2nd parameter of a iterator, the context transition happens. it will transform the row context brought in by 1st parameter of the iterator into filter context and then use it to calculate the measure.

that is the AVGERAGEX generate a row context of period id, and tranform it to filter context, then used it in the calculation of [QTY Total].

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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