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
 
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
What I am needing is the get the weighted average per week, per account, not the average of all lines.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I only gave 3 formulas, all should be measures. I didn't say anything (I think) about a calculated column.

If Calendar Week and Account are the unique identifiers for a Score (e.g. CW46 and Account A234 always have 65.2, but CW47 and Account A234 always have 84.3) then you need the 2nd parameter for the Account Sum measure.

VBA Code:
[Account Sum] :=
CALCULATE (
    SUMX (
        SUMMARIZE ( MyTable, MyTable[Account], MyTable[Calendar Week] ),
        CALCULATE (
            MAX ( MyTable[Score] ) // Assuming all Score values are the same within this summary you can use MAX, MIN, AVG, whatever
        )
    )
)
 
Upvote 0
I only gave 3 formulas, all should be measures. I didn't say anything (I think) about a calculated column.

If Calendar Week and Account are the unique identifiers for a Score (e.g. CW46 and Account A234 always have 65.2, but CW47 and Account A234 always have 84.3) then you need the 2nd parameter for the Account Sum measure.

VBA Code:
[Account Sum] :=
CALCULATE (
    SUMX (
        SUMMARIZE ( MyTable, MyTable[Account], MyTable[Calendar Week] ),
        CALCULATE (
            MAX ( MyTable[Score] ) // Assuming all Score values are the same within this summary you can use MAX, MIN, AVG, whatever
        )
    )
)
I need to have this as a column. The way I read your original response was to use two measures and the third was a calculated column based off of the two measures. When I applied that I got a standard average and not the weighted average I need.
 
Upvote 0
Why as a column? It's much less flexible, and you'll still be duplicating values. In any case, when you used a column it was pulling all values without filtering by your calendar week so you got a weighted average for the entire table - that's why it didn't change.

You'll have to play with the following column formula - since I don't have the data I can't tweak it the way to test it. You need to basically combine the logic from all 3 measures into a single column. I have a tough time with FILTER arguments and I'm never quite sure if I need the FILTER(ALL(...)) construct until I see the data. Anyway, try this and tweak it to see if your column gives what you need.
VBA Code:
[Account Sum] =
VAR my_account = MyTable[Account]
VAR my_week = MyTable[Calendar Week]
VAR sum_of_items =
    CALCULATE (
        SUMX (
            SUMMARIZE ( MyTable, MyTable[Account], MyTable[Calendar Week] ),  // not sure if you need a FILTER here or if this will operate on the whole table
            CALCULATE (
                MAX ( MyTable[Score] ) // Assuming all Score values are the same within this summary you can use MAX, MIN, AVG, whatever
                ,
                MyTable[Calendar Week] = my_week // only add values for accounts in the current week
            )
        )
    )
VAR count_of_accounts =
    CALCULATE (
        DISTINCTCOUNT ( MyTable[Account] ),
        FILTER ( ALL ( MyTable[Calendar Week] ), MyTable[Calendar Week] = my_week )
    )
VAR weighted_score =
    DIVIDE ( sum_of_items, count_of_accounts )
RETURN
    weighted_score
 
Upvote 0
Why as a column? It's much less flexible, and you'll still be duplicating values. In any case, when you used a column it was pulling all values without filtering by your calendar week so you got a weighted average for the entire table - that's why it didn't change.

You'll have to play with the following column formula - since I don't have the data I can't tweak it the way to test it. You need to basically combine the logic from all 3 measures into a single column. I have a tough time with FILTER arguments and I'm never quite sure if I need the FILTER(ALL(...)) construct until I see the data. Anyway, try this and tweak it to see if your column gives what you need.
VBA Code:
[Account Sum] =
VAR my_account = MyTable[Account]
VAR my_week = MyTable[Calendar Week]
VAR sum_of_items =
    CALCULATE (
        SUMX (
            SUMMARIZE ( MyTable, MyTable[Account], MyTable[Calendar Week] ),  // not sure if you need a FILTER here or if this will operate on the whole table
            CALCULATE (
                MAX ( MyTable[Score] ) // Assuming all Score values are the same within this summary you can use MAX, MIN, AVG, whatever
                ,
                MyTable[Calendar Week] = my_week // only add values for accounts in the current week
            )
        )
    )
VAR count_of_accounts =
    CALCULATE (
        DISTINCTCOUNT ( MyTable[Account] ),
        FILTER ( ALL ( MyTable[Calendar Week] ), MyTable[Calendar Week] = my_week )
    )
VAR weighted_score =
    DIVIDE ( sum_of_items, count_of_accounts )
RETURN
    weighted_score
I used the measures was able to get the results I needed for charting. I will refer to your above code if it is requested that these be in columns.
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,925
Members
449,094
Latest member
teemeren

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