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
Pldoyon1
Helper I
Helper I

Calculate annual average

Hello guys, hope you're doing well ! I want to calculate the annual average so I try to simply divide  DIVIDE([Cost],DISTINCTCOUNT([Year]))  but in my dataset, I don't have the 0 value for each year for each dimension and I want also to filter the year with a slicer. So, I can't do this : DIVIDE([Cost],CALCULATE(DISTINCTCOUNT([Year]),ALL()) or DIVIDE([Cost],CALCULATE(DISTINCTCOUNT([Year]),ALLEXCEPT(Table, [Year]))

 

So there're some options that I'm thinking of :

- add all the rows with 0 that doesn't exist in my database but it's too many

- create a new table with only YEAR but because there's no relation between these 2 tables, I can't use the year slicer

- use many to many relation (CROSSFILTERED)

- what if parameter

 

Can someone help me please ?

 

I need to find a way to calculate the annual average with all the years that the user will choose in the slicer, not only the year of the dimension because there's no 0 for all year. Hope that's understandable.

 

Thanks a lot

13 REPLIES 13
v-yiruan-msft
Community Support
Community Support

Hi @Pldoyon1 ,

I'm not very clear about your requirement. Could you please explain more and provide your expected result with example? For example the scenario in your last post, what's the final expected result? I updated my sample pbix file, please check whether that is what you want.

yingyinr_0-1615968682564.png

Best Regards

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

Hello Yinggyinr, thanks a lot for your reply and effort to help me. The result that I'm looking for in your example is this :

 

With the data of the category A (but the lines of 0 value doesn't exist)

2017 : 0

2018 : 700

2019 : 0

2020 : 0 

 

The annual average is : 700 / 4 = 175

 

So because there's no data in your model for the category A for all years, it can't calculate correctly. Powr Bi should provide a way to consider the numbers of years selected by the user in the slicer. Now, we can't filter a category and get the right average corresponding with the date selected in the slicer.

 

Hope that is more clear.

 

Thanks

Hi @Pldoyon1 ,

I updated the formula of measure as below, please check whether the result is correct or not:

Measure =
VAR _mindate =
CALCULATE( MIN ( 'Table'[Date] ),REMOVEFILTERS('Table'[Category]))
VAR _maxdate =
CALCULATE( MAX( 'Table'[Date] ),REMOVEFILTERS('Table'[Category]))
RETURN
DIVIDE ( SUM ( 'Table'[Cost] ), DATEDIFF ( _mindate, _maxdate, YEAR ) + 1 )

yingyinr_0-1616032358991.png

Any comment or problem, please feel free to let me know.

Best Regards

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

Hello @v-yiruan-msft , thanks for your update. It's really appreciated. Unfortunatley, when I open your file, the measure is not 175 but still 725. So, I don't know if you configure something else with your Power Bi but mine (last version in fev 2021) gives me 725 ! The good answer is 175. I tried the same with ALL() or ALLEXCEPT() as I said in my first post and it doesn't work. If you calculate and use REMOVEFILTER(Category), the logic is that it won't consider the filter Category and this is what Power Bi is doing.

 

If you have other idea, it will be welcomed.

 

Best regards

Hi @Pldoyon1 ,

Actually, I didn't make any other configure for these visuals. You can check the attachment for the details.

yingyinr_0-1616493764037.png

Best Regards

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

Hello Yingyinr, I still have the same result of 725 and not 175 with your file. I don't know how you get this result.

Hi @Pldoyon1 ,

I updated my sample pbix file with the following steps:

1. Create a date dimension (don't create the relationship with the fact table)

2. Update the formula of measure as below:

Measure =
VAR _mindate =
CALCULATE( MIN ( 'Date'[Date] ))
VAR _maxdate =
CALCULATE( MAX( 'Date'[Date] ))
RETURN
DIVIDE ( SUM ( 'Table'[Cost] ), DATEDIFF ( _mindate, _maxdate, YEAR ) + 1 )

yingyinr_0-1616574013154.png

Best Regards

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

Hello @v-yiruan-msft , thanks for your effort. I think you are in the right path. The only solution I see is passing by a disconnected date table. But, it still doesn't work because if you filter the slicer of date, it won't filter the other table with the cost. So, the sum of cost won't be good with the dates selected in the slicer. There's certainly a way to filter the table of cost with a dax measure with the dates selected in the date table.

Pldoyon1
Helper I
Helper I

So, I solve my problem creating a table with the number of years between  a couple of period of times that the user can select instead to be able to personnalize the period with a normal slicer. If someone know how to do it with a normal slicer, it will be appreciated.

Hi @Pldoyon1 ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want.

yingyinr_0-1615888899405.png

If the above one is not applicable for your scenario, please provide some sample data(exclude sensitive data) and your expected result with example or screenshot. Thank you.

Best Regards

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

Hello Yingyinr, thanks a lot for your help. Unfortunately, it doesn't work. If you add another column in your table of categories and there's only one category (lets say A) in 2018 and you filter the date between 2017 and 2020, and filter the category A, the min and max date will be only one row of category A in 2018. So, the average will consider only the min and max date of this row. The average has to consider non existing data (data with 0 removed) between the dates in the slicer. 

AllisonKennedy
Super User
Super User

@Pldoyon1 This sounds like you need a DimDate table (or at very least DimYear), so yes, create a new table with YEAR and relate that to fact table. Preferrable it's a DimDate table: https://excelwithallison.blogspot.com/2020/04/dimdate-what-why-and-how.html

 

Use the AVERAGEX( VALUES(DimDate[Year]), [Cost]) to calculate the average.

https://excelwithallison.blogspot.com/2020/09/what-does-average-mean.html 


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Hello Allison, thanks for your response but it doesn't work. I add the DimDate as you said with a link with my fact table and that does not consider the non existing 0 value in the average calculation when I filter a dimension.

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.