Forum Discussion
Dynamically calculate Average
I need to create a table where we can dynamically group by year and month with average on one axis and total on the other. I have been able to create one but it is static and if I try to make it dynamic Average doesn't come. Static Mock up is attached below:
Thank you for your reply. I have actually edited the query and transposed the data in a way I get month-year and all the activites from a single query. I am no longer using the Date Query. Instead, I have used group_by to group it based on the activity dates and get the sum for each activities. Next step, I put conditional columns to get the number of sessions occured in a month-year by checking the sum column of that particular activity - i.e. if the sum is > 0 the output in conditional column in 1. Hence I get the number of sessions occured in a month - year. After that, I transposed the query and got columns as month-year and all activities in the row. Then I calculated Average using this Power Query - each List.Average(Record.FieldValues(_ & [Session Type = null])). It did work perfect!
14 Replies
- TheoCCommunity Champion
You will need to have a column that is properly formatted with a Date. Currently, your date columns (i.e. Month Year) are suggested not to be in this state. Do you have a Date table? If not, please refer to here for a simple date table: https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/
Once you have established that, you can use the Date field hierarchy to drag the Year and Month properly into your table and that should allow you to get the dynamic outputs you are after.
I hope this helps 🙂
Theo
- NKotak_LeecareHelper III
Thank you, Theo! I have made Date table for it as well before. I tired using that to create the table but the outcome is not that useful. Below is the attached snapshot if it.
- TheoCCommunity Champion
Try to use the Matrix visual, not the table visual. Selected in the below snippet in red box.
- NKotak_LeecareHelper III
Yes, using Matrix I do get the average but the format is messed up. Plus, when we expand the exercise with year and months the average is not accurate. Below are the attached snapshots of it.
Thank you, Theo!
- TheoCCommunity Champion
Have you created the Average measure? Measure = AVERAGE ( 'Table'[Amount Column] )
If you drag that into your value field of the Matrix, it should be correct.
- v-chenwuz-msftCommunity Support
Hi NKotak_Leecare ,
I recommend matrix table and Date Hierarchy to reach that.
First, you need a calendar table or date column, this will be used for date hierarchy.
Second, create a measure like the following:
Measure = IF( HASONEVALUE( 'Table'[Date].[Month] ), "expression of total", IF( HASONEFILTER( 'Table'[Date].[Year] ), " expression of avg this year", "expression of avg all years" ) )Please change the "expression" part to calculate what you want. For example, avg = AVERAGE('Table'[Value]).
You can calculate total or avg group by year , month via drill down ( marked in red).
Result:
Pbix in the end you can refer.Best Regards
Community Support Team _ chenwu zhu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- NKotak_LeecareHelper III
Thank you for your reply. I have actually edited the query and transposed the data in a way I get month-year and all the activites from a single query. I am no longer using the Date Query. Instead, I have used group_by to group it based on the activity dates and get the sum for each activities. Next step, I put conditional columns to get the number of sessions occured in a month-year by checking the sum column of that particular activity - i.e. if the sum is > 0 the output in conditional column in 1. Hence I get the number of sessions occured in a month - year. After that, I transposed the query and got columns as month-year and all activities in the row. Then I calculated Average using this Power Query - each List.Average(Record.FieldValues(_ & [Session Type = null])). It did work perfect!
- v-chenwuz-msftCommunity Support
Hi NKotak_Leecare ,
I'm glad you've solved your problem. Please mark it as a solution and anyone who has the same problem as you will soon find the answer.
Best Regards
Community Support Team _ chenwu zhu