Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
WishAskedSooner
Resolver II
Resolver II

Weighted Average on Narrow Table Using DAX Measure

My Fact Table is quite narrow with a few Dimension tables (think EAV model if you are familiar with dB architecture). Let's say I have the following 'Fact' table:

 

EntryIDAttrIDValue
115
2110
1215
2220

 

and the following 'Dim' table with a one-to-many join on [AttriID] in the Data Model:

 

AttrIDCategory
1A
2B

 

For the most part, my measures rely on:

 

 

CALCULATE(SUM('Fact'[Value]), ALL('Dim'), 'Dim'[Category] = "A")

 

 

which correctly returns 15. This works fine when I just need a simple aggregation like SUM, AVERAGE, etc. for a given Category.

For a handful of my measures, I have to wheel out the DIVIDE function to get to the proper aggregated value. This is simple if all I am doing is a SUM(A) / SUM(B) e.g.

 

 

DIVIDE(CALCULATE(SUM('Fact'[Value]), ALL('Dim'), 'Dim'[Category] = "A"), CALCULATE(SUM('Fact'[Value]), ALL('Dim'), 'Dim'[Category] = "B"))

 

 

which correctly returns 15 / 35 or 0.4285....

 

But, sometimes I need SUM(A(i)*B(i)) / SUM(B) where i is an index across all EntryIDs, but I can't get it to work. To be precise, I need (5*15 + 10*20) / (15 + 20) which equals 7.857...
Part of the complication arises comes from joining up the proper values by EntryID versus (5*20 + 10*15) or some other jumbled order when doing this across millions of rows.

 

I have researched this for hours with no solutions. And, for design reasons, I need to do this in a measure that is a single DAX line that does not rely on defining VARs, etc.

 

Please help and thank you in advance!

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

Hi @WishAskedSooner ,

First of all, many thanks to @lbendlin  for your very quick and effective replies.

Based on my testing, please try the following methods:

1.Create the simple table.

vjiewumsft_0-1716173319561.png

vjiewumsft_1-1716173350409.png

2.Create the new measure to calculate sum across all entry id.

Weighted Average = 
DIVIDE(
    SUMX(
        FILTER(
            'Fact Table',
            RELATED('Dim Table'[Category]) = "A"
        ),
        'Fact Table'[Value] * LOOKUPVALUE('Fact Table'[Value], 'Fact Table'[EntryID],'Fact Table'[EntryID], 'Dim Table'[Category], "B")
    ),
    CALCULATE(SUM('Fact Table'[Value]), ALL('Dim Table'), 'Dim Table'[Category] = "B")
)

3.Select the measure and edit the number of shown for the value.

vjiewumsft_2-1716173381521.png

4.Drag the measure into the card visual. The result is shown below.

vjiewumsft_3-1716173391917.png

 

You can also view the following links to learn about DAX function.

LOOKUPVALUE function (DAX) - DAX | Microsoft Learn

SUMX function (DAX) - DAX | Microsoft Learn

 

Best Regards,

Wisdom Wu

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

View solution in original post

7 REPLIES 7
v-jiewu-msft
Community Support
Community Support

Hi @WishAskedSooner ,

First of all, many thanks to @lbendlin  for your very quick and effective replies.

Based on my testing, please try the following methods:

1.Create the simple table.

vjiewumsft_0-1716173319561.png

vjiewumsft_1-1716173350409.png

2.Create the new measure to calculate sum across all entry id.

Weighted Average = 
DIVIDE(
    SUMX(
        FILTER(
            'Fact Table',
            RELATED('Dim Table'[Category]) = "A"
        ),
        'Fact Table'[Value] * LOOKUPVALUE('Fact Table'[Value], 'Fact Table'[EntryID],'Fact Table'[EntryID], 'Dim Table'[Category], "B")
    ),
    CALCULATE(SUM('Fact Table'[Value]), ALL('Dim Table'), 'Dim Table'[Category] = "B")
)

3.Select the measure and edit the number of shown for the value.

vjiewumsft_2-1716173381521.png

4.Drag the measure into the card visual. The result is shown below.

vjiewumsft_3-1716173391917.png

 

You can also view the following links to learn about DAX function.

LOOKUPVALUE function (DAX) - DAX | Microsoft Learn

SUMX function (DAX) - DAX | Microsoft Learn

 

Best Regards,

Wisdom Wu

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

@v-jiewu-msft 

 

Simply, simply Brilliant!!!! I was able to implement this into my actual data model which is slightly more complex than the example I provided (a total of three columns that need to be included in the LOOKUP to filter my fact table properly) and it works beautifully! I can't even begin to describe how thankful I am for your help. I have been agonizing over this for days. Thank you!

WishAskedSooner
Resolver II
Resolver II

Here is hopefully more meaningful data that fully describes my problem. I have three tables:

Fact

WishAskedSooner_0-1715977417856.png

DimE joined on EID

WishAskedSooner_1-1715977481625.png

DimA joined on AID

WishAskedSooner_2-1715977511387.png

and the following Data Model:

WishAskedSooner_3-1715977530244.png

What I am trying to do is multiply Category A with Category B (not Category C) for EID X. Repeat for EID Y. Then sum the individual results. I have created a Pivot to illustrate:

WishAskedSooner_5-1715977821135.png

The final result would be 75 + 200 = 275. In other words, I am trying to do the following:

 

(5*15 + 10*20) = 275

 

I am hoping I can do this in a DAX measure versus having to actually pivot the Fact table to a new table.

 

I hope this makes more sense. Thanks in advance for the help!

WishAskedSooner
Resolver II
Resolver II

It was suggested I provide sample data that fully covers my issue. So I will try to do that. I have the following three tables:

Fact

WishAskedSooner_6-1715975521762.png

DimE joined on EID

WishAskedSooner_4-1715975163976.png

DimA joined on AID

WishAskedSooner_7-1715975612499.png

The following Data Model

WishAskedSooner_5-1715975255229.png

I am trying to find a way to multiply the values for Category A and Category B (not Category C) in the Fact table by EID then sum the individual results.

 

For example, for EID 1, I want to multiply AID 1 and AID 2 together because they both belong to EID 1. Furthermore, I want to be able to specify this using the joined Category in DimA, i.e. for EID 1 multiply Category A by Category B. Then, for EID 2, multiply Category A and Category B. Repeat for each EID in the Fact table. Then sum the individual results. So,

 

(5*15 + 10*20) = 275

 

I would provide examples of what I have tried, but it is pointless because none come even close. I literally have no idea how to do this. Please help.

WishAskedSooner
Resolver II
Resolver II

Thank you for your reply. You seem to imply that I could use either SUMX or PRODUCTX to get to the correct value which is interesting.

 

However, I am very new to PowerBI and DAX, so if you could provide the syntax with how to use those functions with or without CALCULATE, that would be a huge help. As I mentioned above, I have researched this for at least four hours without success, and this includes experimenting with SUMX and PRODUCTX.

 

I am not sure what you mean by providing sample data. I can't upload my actual PBI model due to security reasons. I suppose I could create a PBI project with the data that I included above, but is that extra effort really necessary?

It's totally your choice.  I can only assist with meaningful sample data.

lbendlin
Super User
Super User

use aggregation functions like SUMX or PRODUCTX.

 

Please provide sample data that fully covers your issue.
Please show the expected outcome based on the sample data you provided.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.