cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
P0WER_BI
Frequent Visitor

Weighted Average in a Hierarchy Visualized by a Matrix

I am trying to create a measure to show the weighted average at multiple levels of a hiearchy.  I will be presenting this data in a matrix visualization in Power BI.  I will provide sample data to illustrate this in more detail.

P0WER_BI_0-1637090932053.png

In this example, I have three levels of the hierachy: Product; Location; and Business.

I want the average Unit Price weighted by Qty Sold.

 

In Excel, the results would look like this for Location:

P0WER_BI_1-1637091148752.png

 

and like this for Business:

P0WER_BI_2-1637091206162.png

 

P0WER_BI_3-1637091397038.png

 

In Power BI, the expectation is that when you collapse a level of the hierarchy in the matrix, you see the corresponding weighted average in an adjacent column.  At the lowest level you would simply show the original Unit Price per product. 

P0WER_BI_4-1637091748442.png

 

Is this possible?  Is there another solution I should consider?

 

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

Try this:

Avg =
DIVIDE (
    SUMX ( Table1, Table1[Unit Price] * Table1[Qty Sold] ),
    SUMX ( Table1, Table1[Qty Sold] )
)

 

View solution in original post

4 REPLIES 4
AlexisOlson
Super User
Super User

Try this:

Avg =
DIVIDE (
    SUMX ( Table1, Table1[Unit Price] * Table1[Qty Sold] ),
    SUMX ( Table1, Table1[Qty Sold] )
)

 

Thank you!  This is perfect!  However, I found one anomaly during my validation--maybe you will know what is causing it.  One of the values at the lowest granularity came up as a lower value compared to the actual.  But others seemed unafflicted by this.  In this example I get ~$87k where I would expect ~$155k.

P0WER_BI_2-1637191926395.png

 

Is your Unit Price column using a SUM aggregation? I bet you have more than one row in your data table for 1A and it's adding the prices together like it does for the subtotals.

You nailed it.  Duh.  I should have realized that.  I have what I need now and it's working perfectly.  Thank you so much!

Helpful resources

Announcements
Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May 23 2022 epsiode 5 without aka link.jpg

The Power BI Community Show

Welcome to the Power BI Community Show! Jeroen ter Heerdt talks about the importance of Data Modeling.

Power BI Dev Camp Session 22 with aka link 768x460.jpg

Check it out!

Mark your calendars and join us on Thursday, May 26 at 11a PDT for a great session with Ted Pattison!

charticulator_carousel_with_text (1).png

Charticulator Design Challenge

Put your data visualization and design skills to the test! This exciting challenge is happening now through May 31st!