Conditional Weighted Average - DAX - Help Please

lager1001

Board Regular
Joined
May 17, 2019
Messages
88
Need help with the DAX coding for a conditional weighted average.

See below example.

There are 4 unique accounts (but 7 line items) in CW24, each with their respective Score. If I do a simple average, it takes all scores for all line items and I get 63.86. What I need is the weighted average so it calculates each score only once for each account and provide the average by the unique number of accounts for the calendar week. The desired outcome is the "Weighted Average Column" which would be 70.39+65.45+99.96+0.00 / 4. Some of my scores are 0.00 so these have to be inclusive to the formula. Can anyone assist?

Year/CWAccountScoreAverage ScoreWeighted Average
2020-CW24ABC70.3963.8658.95
2020-CW24ABC70.3963.8658.95
2020-CW24ABC70.3963.8658.95
2020-CW24ABC70.3963.8658.95
2020-CW24DEF65.4563.8658.95
2020-CW24GHI99.9663.8658.95
2020-CW24JKL0.0063.8658.95
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You don’t provide any information about your table structure. I will assume you have a single table and the only data is visible in your sample. You could write this
AVERAGEX(VALUES(TableName[Account]),CALCULATE(Average(TableName[Score])))
 
Upvote 0
You don’t provide any information about your table structure. I will assume you have a single table and the only data is visible in your sample. You could write this
AVERAGEX(VALUES(TableName[Account]),CALCULATE(Average(TableName[Score])))
Hi Matt,

Thanks for the reply. Yes, it is a table from power query with rows of data for each account by calendar week. When I try the formula above I do not get the correct weighted average however. Each score should be counted only once per CW for the account in the weighted average. So in the above example, even though I have 7 rows of data, I really only have 4 scores and 4 accounts that need to be averaged, but this weighted average should appear on each line item for calendar week.
 
Upvote 0
You may need to SUMMARIZE on the Account to get the total, then divide by the count of distinct accounts. The code below assumes that Account alone is sufficient for uniquely identifying the correct score, but if there are other fields that contribute to a primary key you'll need to add those to the SUMMARIZE section.

Try this to start...

[Account Sum]:= CALCULATE (
SUMX (
SUMMARIZE (
MyTable,
MyTable[Account]
),
CALCULATE (
MAX ( MyTable[Score] ) // Assuming all Score values are the same within this summary you can use MAX, MIN, AVG, whatever
)
)

[Distinct Accounts]:=DISTINCTCOUNT ( Mytable[Account] )

[Weighted Average] = DIVIDE ( [Account Sum] , [Distinct Accounts] )
 
Upvote 0
You may need to SUMMARIZE on the Account to get the total, then divide by the count of distinct accounts. The code below assumes that Account alone is sufficient for uniquely identifying the correct score, but if there are other fields that contribute to a primary key you'll need to add those to the SUMMARIZE section.

Try this to start...

[Account Sum]:= CALCULATE (
SUMX (
SUMMARIZE (
MyTable,
MyTable[Account]
),
CALCULATE (
MAX ( MyTable[Score] ) // Assuming all Score values are the same within this summary you can use MAX, MIN, AVG, whatever
)
)

[Distinct Accounts]:=DISTINCTCOUNT ( Mytable[Account] )

[Weighted Average] = DIVIDE ( [Account Sum] , [Distinct Accounts] )
I can take this route if I need to. I am trying to keep it one column, as there are 16 different scenarios where I need this weighted average. If I go this route then I am looking at adding many more columns. DAX is frustrating that way but if that's the solution then that is what I will have to use. Thank you.
 
Upvote 0
Please be more specific. What are some of the 16 other scenarios? You can have one measure that reports back on which of the 16 scenarios was triggered, for example. (Bulky, but acceptable.) That's if they're mutually exclusive of course... otherwise you'll certainly need more columns for more measures.
 
Upvote 0
Please be more specific. What are some of the 16 other scenarios? You can have one measure that reports back on which of the 16 scenarios was triggered, for example. (Bulky, but acceptable.) That's if they're mutually exclusive of course... otherwise you'll certainly need more columns for more measures.
I have these scores broken out by 4 different categories, then by 4 different time periods.
 
Upvote 0
I can take this route if I need to. I am trying to keep it one column, as there are 16 different scenarios where I need this weighted average. If I go this route then I am looking at adding many more columns. DAX is frustrating that way but if that's the solution then that is what I will have to use. Thank you.
I used the approach above but get a circular dependency error. DAX frustrates me that way. I can write this in Excel in one column without an issue but can't figure out how to go about it in DAX given the dynamics and syntax.
 
Upvote 0
Circular dependencies usually occur on calculated columns. Did you create all 3 as measures?
 
Upvote 0
Circular dependencies usually occur on calculated columns. Did you create all 3 as measures?
I entered it as measures with a calculated column as you indicated and I get the exact same results as I do when I do an Average X formula on the Scores with an AllExcept filter on the CW and the Account.

Since each CW and each account can have multiple line items, it should only be taking that score one time and dividing over the unique count of unique CW/Score lines items but applying it to all line items with that combination.

For instance if I take all accounts and all scores for CW46 my average is 99.10 but if I only take the score for CW46 1 time for each unique account in CW46 (essentially removing duplicates) my weighted average becomes 98.38.

Calendar WeekAccountScoreAverageWeighted Average by CW and Unique Account
CW46ABCDE100.0099.198.38
CW46ABCDE100.0099.198.38
CW46FGHJ99.7799.198.38
CW46FGHJ99.7799.198.38
CW46FGHJ99.7799.198.38
CW46FGHJ99.7799.198.38
CW46LMNO100.0099.198.38
CW46LMNO100.0099.198.38
CW46LMNO100.0099.198.38
CW46LMNO100.0099.198.38
CW46PQRS100.0099.198.38
CW46PQRS100.0099.198.38
CW46PQRS100.0099.198.38
CW46PQRS100.0099.198.38
CW46TUVW99.7799.198.38
CW46TUVW99.7799.198.38
CW46TUVW99.7799.198.38
CW46TUVW99.7799.198.38
CW46XYZ99.8899.198.38
CW46XYZ99.8899.198.38
CW46XYZ99.8899.198.38
CW46XYZ99.8899.198.38
CW46A12399.8899.198.38
CW46A12399.8899.198.38
CW46A12399.8899.198.38
CW46A12399.8899.198.38
CW46B234100.0099.198.38
CW46B234100.0099.198.38
CW46B234100.0099.198.38
CW46B234100.0099.198.38
CW46C12399.9799.198.38
CW46C12399.9799.198.38
CW46C12399.9799.198.38
CW46C12399.9799.198.38
CW46D23499.8999.198.38
CW46D23499.8999.198.38
CW46D23499.8999.198.38
CW46D23499.8999.198.38
CW46E12399.9299.198.38
CW46E12399.9299.198.38
CW46E12399.9299.198.38
CW46E12399.9299.198.38
CW46F234100.0099.198.38
CW46F234100.0099.198.38
CW46F234100.0099.198.38
CW46F234100.0099.198.38
CW46G12399.9699.198.38
CW46G12399.9699.198.38
CW46G12399.9699.198.38
CW46G12399.9699.198.38
CW46H234100.0099.198.38
CW46H234100.0099.198.38
CW46H234100.0099.198.38
CW46H234100.0099.198.38
CW46I123100.0099.198.38
CW46I123100.0099.198.38
CW46I123100.0099.198.38
CW46I123100.0099.198.38
CW46J234100.0099.198.38
CW46K123100.0099.198.38
CW46L23499.7899.198.38
CW46M12398.9799.198.38
CW46M12398.9799.198.38
CW46M12398.9799.198.38
CW46M12398.9799.198.38
CW46M12398.9799.198.38
CW46N23499.8199.198.38
CW46N23499.8199.198.38
CW46N23499.8199.198.38
CW46N23499.8199.198.38
CW46N23499.8199.198.38
CW46AA123100.0099.198.38
CW46O12383.3799.198.38
CW46P23499.5499.198.38
CW46Q12399.7199.198.38
CW46R23492.3599.198.38
CW46S12399.3699.198.38
CW46S12399.3699.198.38
CW46T23499.8399.198.38
CW46T23499.8399.198.38
CW46T23499.8399.198.38
CW46T23499.8399.198.38
CW46T23499.8399.198.38
CW46U12379.9099.198.38
CW46U12379.9099.198.38
CW46V23499.7699.198.38
CW46W123100.0099.198.38
 
Upvote 0

Forum statistics

Threads
1,215,601
Messages
6,125,758
Members
449,259
Latest member
rehanahmadawan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top