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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Arthur_NS
Helper I
Helper I

Calculating average with condition

Hi everyone,

 

I created a new measure with the below code.

It basically creates a weighted average of Share Individuals 3+ based on BT.

 

The thing is that in my database, not all lines have a Share Individuals but all lines have a BT.
So the end result is wrong.
What I need is the same calculation that filters out the lines with no Share Individuals, so that the BT is not taken into account for the lines without Share Individuals.
Any idea how to integrate this?

 

Market share =
VAR __CATEGORY_VALUES = VALUES('MM'[Share Individuals 3+])
RETURN
    DIVIDE(
        SUMX(
            KEEPFILTERS(__CATEGORY_VALUES),
            CALCULATE(SUMX('MM','MM'[Share Individuals 3+]*MM[BT]))
        ),
        SUMX(KEEPFILTERS(__CATEGORY_VALUES), CALCULATE(SUM('MM'[BT])))
    )
1 ACCEPTED SOLUTION

Hi @Arthur_NS ,

I think your problem should be caused by _Part2. It sums all BT, so return incorrect result in divide. I suggest you to add a filter in __CATEGORY_VALUES. Here I create a sample to have a test.

1.png

Try this code.

Market share =
VAR __CATEGORY_VALUES =
    FILTER (
        VALUES ( 'MM'[Share Individuals 3+] ),
        MM[Share Individuals 3+] <> BLANK ()
    )
VAR __PART1 =
    SUMX (
        KEEPFILTERS ( __CATEGORY_VALUES ),
        CALCULATE ( SUMX ( 'MM', 'MM'[Share Individuals 3+] * MM[BT] ) )
    )
VAR __PART2 =
    SUMX ( KEEPFILTERS ( __CATEGORY_VALUES ), CALCULATE ( SUM ( 'MM'[BT] ) ) )
RETURN
    DIVIDE ( __PART1, __PART2 )

I think the result you want is (3*4+1*2+5*4+7*8)90 / 18 (4+2+4+8 ) = 5.

1.png

 

Best Regards,
Rico Zhou

 

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

3 REPLIES 3
Arthur_NS
Helper I
Helper I

Not working, values are too low which leads me to believe too many lines are still taken into account

Shouldn't the MM BT be filtered as well?

Hi @Arthur_NS ,

I think your problem should be caused by _Part2. It sums all BT, so return incorrect result in divide. I suggest you to add a filter in __CATEGORY_VALUES. Here I create a sample to have a test.

1.png

Try this code.

Market share =
VAR __CATEGORY_VALUES =
    FILTER (
        VALUES ( 'MM'[Share Individuals 3+] ),
        MM[Share Individuals 3+] <> BLANK ()
    )
VAR __PART1 =
    SUMX (
        KEEPFILTERS ( __CATEGORY_VALUES ),
        CALCULATE ( SUMX ( 'MM', 'MM'[Share Individuals 3+] * MM[BT] ) )
    )
VAR __PART2 =
    SUMX ( KEEPFILTERS ( __CATEGORY_VALUES ), CALCULATE ( SUM ( 'MM'[BT] ) ) )
RETURN
    DIVIDE ( __PART1, __PART2 )

I think the result you want is (3*4+1*2+5*4+7*8)90 / 18 (4+2+4+8 ) = 5.

1.png

 

Best Regards,
Rico Zhou

 

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

 

amitchandak
Super User
Super User

@Arthur_NS , check if this exmaple can help

 

Market share =
VAR __CATEGORY_VALUES = VALUES('MM'[Share Individuals 3+])
RETURN
DIVIDE(
SUMX(
KEEPFILTERS(__CATEGORY_VALUES),
CALCULATE(SUMX(filter('MM','MM'[Share Individuals 3+]<> 0),'MM'[Share Individuals 3+] *MM[BT]))
),
SUMX(KEEPFILTERS(__CATEGORY_VALUES), CALCULATE(SUM('MM'[BT])))
)

 

 

or

 

Market share =
VAR __CATEGORY_VALUES = VALUES('MM'[Share Individuals 3+])
RETURN
DIVIDE(
SUMX(
KEEPFILTERS(__CATEGORY_VALUES),
CALCULATE(SUMX(filter('MM','MM'[Share Individuals 3+]<> 0 && not(isblank('MM'[Share Individuals 3+] )) ),'MM'[Share Individuals 3+] *MM[BT]))
),
SUMX(KEEPFILTERS(__CATEGORY_VALUES), CALCULATE(SUM('MM'[BT])))
)

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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