Forum Discussion
average on matrix
- 3 years ago
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
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
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