cancel
Showing results for
Did you mean:

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Helper III

## 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:

1 ACCEPTED SOLUTION
Helper 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!

14 REPLIES 14
Community Support

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.

Helper 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!

Community Support

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

Super User

Try to use the Matrix visual, not the table visual. Selected in the below snippet in red box.

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Helper 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!

Super User

Also, I am not understanding how the average for the month is incorrect?  The Average column that you first presented (highlighted in red below) is the Total Average for All Months combined. It is not at the "month" level.

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Helper III

Yes, you are right. The average is incorrect in the first mock. Ideally, the Average for Bed Exercise should have been 13/8, where 8 is the total number of months. But we are not getting that there. Here is the outcome of Average taken as measure which is still incorrect.

The other pic is how we ideally want the outcome to be like:

Super User

I think the following measure will provide you what I believe you are after:

`Measure = CALCULATE ( AVERAGE ( 'Table'[Amount] ) , ALLEXCEPT ('Table' ,'Table'[Session Type] ) )`

Hope this helps.

Theo

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Helper III

Yes, I tried it but the outcome is same as before. Here is the attached snapshot of it.

Super User

Can you please provide a snapshot of the Matrix table and the values you are using in each of the fields? I think there is some kind of anomaly in what you are wanting in your output versus the current structure of fields being brought into your model.

Thanks,

Theo

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Helper III

Required details:

Super User

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.

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Super User

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

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Helper 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.

Announcements

#### Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

#### Power BI Monthly Update - July 2024

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

#### Fabric Community Update - July 2024

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

Top Solution Authors
Top Kudoed Authors