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
Anonymous
Not applicable

Average in DAX based on grouping column that takes into consideration ZERO values for count

Hello.


I want to calculate average for a single column based on months. The issue is that I have the dates in my fact table and the rows with the value 0 should be taken into consideration when calculating based on month. Therfore if I have 10 rows with 0 in January 2020, there will be counted as 10, but I want them to be counted as 1. And of course any other external filters (not coming from the Calendar Dimmension) table should be taken into consideration if applied.

 

Please see the below example ( the left part is what I get now, the right is what I want to get).

 
 

pbi-ex-avg.png

Since I have a lot of rows, creating a separate table for this requirement would slow things down. I would like to obtain this by using only DAX, if possible, since I am connecting to a live model and I don't have Query Editor or Custom Columns available.

 

You can find a sample PBI file that you can use here:

https://drive.google.com/file/d/1O20pNok50-Hb02olyZ2PtlU8A8kG_wmv/view?usp=sharing

 

Thank you!

 

1 ACCEPTED SOLUTION
v-deddai1-msft
Community Support
Community Support

Hi @Anonymous ,

 

Would you please refer to the following measure by your sample:

Average of value = AVERAGEX(SUMMARIZE(Sales,'Date'[Month],Sales[Product Type],"Value",SUM(Sales[Value])),[Value])

 

Capture6.PNG

 

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

 

Best Regards,

Dedmon Dai

View solution in original post

4 REPLIES 4
v-deddai1-msft
Community Support
Community Support

Hi @Anonymous ,

 

Would you please refer to the following measure by your sample:

Average of value = AVERAGEX(SUMMARIZE(Sales,'Date'[Month],Sales[Product Type],"Value",SUM(Sales[Value])),[Value])

 

Capture6.PNG

 

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

 

Best Regards,

Dedmon Dai

Anonymous
Not applicable

Thank you very much @v-deddai1-msft . Your DAX works great. I still have to put all the columns that I want to group by inside the Summarize function, but PBI handles this very good.

amitchandak
Super User
Super User

@Anonymous , Try a measure like


Measure =
Var _1 = sum(Table[Value])
var _2 = countx(summarize(Table, Table[type], table[Date], "_1", calculate(sum(Table[Value]))), [_1]>0)
var _3 = if(isblank(countx(summarize(Table, Table[type], table[Date], "_1", calculate(sum(Table[Value]))), [_1]=0)),0,1)
return
divide(_1,_2+_3)

 

If value is a measure remove sum

Anonymous
Not applicable

@amitchandakthank you for your answer. Unfortunatelly it does not work.

 

It says that COUNTX can not be used with boolean value (for VAR _2). Besides I can't use your approach because you put there the column "type". I have around 15 columns coming from various tables that might change over time. I can't put all of them in the DAX formula.

However if I try to put COUNTAX instead of COUNTX i get the same values no matter what I put in the condition. I can put [_1] = -1000000 and I still get the same numbers.

 

Maybe you could download the sample file I posted above and give it a try.


Thank you!

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.