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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. 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
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 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.