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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
dfs29
Helper I
Helper I

average on matrix

Hello, 

 

In my matrix , I want to create a new column or measure that calculates the average of my columns 1 and 2.

I would like to point out that the moving average is a measure

 

dfs29_0-1685553428577.png

 

Best regards 

 

1 ACCEPTED SOLUTION

Hi , @dfs29 

Thanks for your quick response!

According to your description, you want to "For example in line 1 (cf below) I have no values in column 2 so I want to divide by 1".

You can try to use dax code :

Measure = var _t =ADDCOLUMNS( CROSSJOIN( ALLSELECTED('Table'[time]) ,  ALLSELECTED('Table'[user case])) , "mv avg" , [moving average])
var _cur_time = MAX('Table'[time])  
var _column =  MAX('Table 2'[user case])
var _t2 = FILTER( _t,[time] =_cur_time && [user case] = _column)
var _value= SUMX(_t2,[mv avg])+0
var _count  =CALCULATE( COUNTROWS('Table 2') , 'Table 2'[user case] <> "AVG")
var _count2 =COUNTROWS( FILTER(_t , [time]=_cur_time && [mv avg] <> BLANK()))
var _sum =SUMX( FILTER( _t , [time] = _cur_time) , [mv avg])
return
IF( _column= "AVG" , _sum/_count2  ,_value)

 

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

View solution in original post

4 REPLIES 4
v-yueyunzh-msft
Community Support
Community Support

Hi , @dfs29 

According to your description, you want to  create a new column or measure that calculates the average of my columns 1 and 2.

First we can modify the [user case] to Text type.

vyueyunzhmsft_0-1685670782271.png

Then we can click "New Table" create a dimension table as the column field , and we do not need to create any relationship between tables.

Table 2 = UNION( VALUES('Table'[user case]) , {"AVG"})

 

Then we can create a measure like this:

Measure = var _t =ADDCOLUMNS( CROSSJOIN( ALLSELECTED('Table'[time]) ,  ALLSELECTED('Table'[user case])) , "mv avg" , [moving average])
var _cur_time = MAX('Table'[time])  
var _column =  MAX('Table 2'[user case])
var _t2 = FILTER( _t,[time] =_cur_time && [user case] = _column)
var _value= SUMX(_t2,[mv avg])+0
var _count  =CALCULATE( COUNTROWS('Table 2') , 'Table 2'[user case] <> "AVG")
var _sum =SUMX( FILTER( _t , [time] = _cur_time) , [mv avg])
return
IF( _column= "AVG" , _sum/_count  ,_value)

 

Then we can put this measure, 'Table2'[user case]  and the 'Table'[time] on the Matrix visual and we can get the result as follows:

vyueyunzhmsft_1-1685670874013.png

 

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Hello @v-yueyunzh-msft , 

 

thank you very much for taking the time to explain it to me it seems to be correct.

However, what should I do if I want to divide by the number of non-empty columns?

 

For example in line 1 (cf below) I have no values in column 2 so I want to divide by 1
in line 3 I have a value for column 1 and a value for column 2, so I divide by 2

do you have an idea please

 

 

dfs29_0-1685700220883.png

 

 

Kind regards 

Hi , @dfs29 

Thanks for your quick response!

According to your description, you want to "For example in line 1 (cf below) I have no values in column 2 so I want to divide by 1".

You can try to use dax code :

Measure = var _t =ADDCOLUMNS( CROSSJOIN( ALLSELECTED('Table'[time]) ,  ALLSELECTED('Table'[user case])) , "mv avg" , [moving average])
var _cur_time = MAX('Table'[time])  
var _column =  MAX('Table 2'[user case])
var _t2 = FILTER( _t,[time] =_cur_time && [user case] = _column)
var _value= SUMX(_t2,[mv avg])+0
var _count  =CALCULATE( COUNTROWS('Table 2') , 'Table 2'[user case] <> "AVG")
var _count2 =COUNTROWS( FILTER(_t , [time]=_cur_time && [mv avg] <> BLANK()))
var _sum =SUMX( FILTER( _t , [time] = _cur_time) , [mv avg])
return
IF( _column= "AVG" , _sum/_count2  ,_value)

 

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Greg_Deckler
Super User
Super User

@dfs29 This looks like a measure aggregation problem. See my blog article about that here: https://community.powerbi.com/t5/Community-Blog/Design-Pattern-Groups-and-Super-Groups/ba-p/138149

The pattern is:
MinScoreMeasure = MINX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
MaxScoreMeasure = MAXX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
AvgScoreMeasure = AVERAGEX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
etc.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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