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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
o59393
Post Prodigy
Post Prodigy

How to divide with distinct count depending on the combination of 2 columns

Hi all

 

I need a fix on the denominator of my calculated column in order to divide properly in function of the distinct count of monthrs for the combination BPP Code & Bottler.

 

The formula is:

 

 

 

Average = 
CALCULATE( 
    SUM ( 'Sheet1'[Mix]),
        FILTER ( 'Sheet1',
            [[ BPP Code ]]] = EARLIER ( [[ BPP Code ]]] )
                && 'Sheet1'[[L1.2 - Bottler]]]
                    = EARLIER ( 'Sheet1'[[L1.2 - Bottler]]] ) 
                        && 'Sheet1'[[ BU Ship From ]]] 
                            = EARLIER( 'Sheet1'[[ BU Ship From ]]])
        )
)
/
DISTINCTCOUNT('Sheet1'[[Month 445]]])

 

 

 

For example the BPP code "232031405" for Bottler "AB" has data for 7 months. However the current formula is dividing by 8.

 

Why 8? Because the total months for exisiting bottlers is 8:

 

223212.JPG

 

However, "AB" has data for 7 months and bottler "FCR" just data for 4 months.

 

The correct way to do it would be to divide only by the distinct months for the combination bottler+code, as stated above.

 

How can I fix this?

 

Pbix

 

https://1drv.ms/u/s!ApgeWwGTKtFdhynS9xpgFReudUKP?e=BgBQlU

 

Thanks!

1 ACCEPTED SOLUTION

Hi @o59393 ,

 

Please try this:

Average =
VAR mix_ =
    CALCULATE (
        SUM ( 'Sheet1'[Mix] ),
        FILTER (
            'Sheet1',
            [[ BPP Code ]]] = EARLIER ( [[ BPP Code ]]] )
                && 'Sheet1'[[L1.2 - Bottler]]] = EARLIER ( 'Sheet1'[[L1.2 - Bottler]]] )
                && 'Sheet1'[[ BU Ship From ]]] = EARLIER ( 'Sheet1'[[ BU Ship From ]]] )
        )
    )
VAR month_ =
    CALCULATE (
        DISTINCTCOUNT ( 'Sheet1'[[Month 445]]] ),
        ALLEXCEPT ( Sheet1, Sheet1[[L1.2 - Bottler]]] )
    )
RETURN
    DIVIDE ( mix_, month_ )

v-xuding-msft_0-1598866521748.png

 

Best Regards,
Xue Ding
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
amitchandak
Super User
Super User

@o59393 , Try like

 

Average =
divide(
CALCULATE(
SUM ( 'Sheet1'[Mix]),
FILTER ( 'Sheet1',
[ BPP Code ] = EARLIER ( [ BPP Code ] )
&& 'Sheet1'[[L1.2 - Bottler]]]
= EARLIER ( 'Sheet1'[L1.2 - Bottler] )
&& 'Sheet1'[ BU Ship From ]
= EARLIER( 'Sheet1'[ BU Ship From ])
)
)
,
DISTINCTCOUNT('Sheet1'[Month 445]))

 

 

or

Average =
CALCULATE(
divide(SUM ( 'Sheet1'[Mix]),
DISTINCTCOUNT('Sheet1'[Month 445])),
FILTER ( 'Sheet1',
[ BPP Code ] = EARLIER ( [ BPP Code ] )
&& 'Sheet1'[[L1.2 - Bottler]]]
= EARLIER ( 'Sheet1'[L1.2 - Bottler] )
&& 'Sheet1'[ BU Ship From ]
= EARLIER( 'Sheet1'[ BU Ship From ])
)
)

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi @amitchandak 

 

I used your second dax but got a different result to the expected which is in yellow:

dsfsdfdsfds3.JPG

 

The dax used was:

 

Average = 
CALCULATE(
    DIVIDE(
    SUM ('Sheet1'[Mix]), DISTINCTCOUNT('Sheet1'[[Month 445]]])),
        FILTER ( 'Sheet1',
            [[ BPP Code ]]] = EARLIER ( [[ BPP Code ]]] )
                && 'Sheet1'[[L1.2 - Bottler]]]
                    = EARLIER ( 'Sheet1'[[L1.2 - Bottler]]] ) 
                        && 'Sheet1'[[ BU Ship From ]]] 
                            = EARLIER( 'Sheet1'[[ BU Ship From ]]])
        )
)

 

The result:

 

9908989.JPG

 

For example OPL Zacapa is giving 2.05% in PBI but should be 0.88% as stated in the first image.

 

Thanks!

Here is the excel with the desired result in yellow

 

https://1drv.ms/x/s!ApgeWwGTKtFdhyju4mjDUCshNPDw?e=sF3zFd

 

Regards.

Hi @o59393 ,

 

Please try this:

Average =
VAR mix_ =
    CALCULATE (
        SUM ( 'Sheet1'[Mix] ),
        FILTER (
            'Sheet1',
            [[ BPP Code ]]] = EARLIER ( [[ BPP Code ]]] )
                && 'Sheet1'[[L1.2 - Bottler]]] = EARLIER ( 'Sheet1'[[L1.2 - Bottler]]] )
                && 'Sheet1'[[ BU Ship From ]]] = EARLIER ( 'Sheet1'[[ BU Ship From ]]] )
        )
    )
VAR month_ =
    CALCULATE (
        DISTINCTCOUNT ( 'Sheet1'[[Month 445]]] ),
        ALLEXCEPT ( Sheet1, Sheet1[[L1.2 - Bottler]]] )
    )
RETURN
    DIVIDE ( mix_, month_ )

v-xuding-msft_0-1598866521748.png

 

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

@v-xuding-msft  Awesome, thanks!

lbendlin
Super User
Super User

Don't use EARLIER(). Use variables.

Move the DISTINCTCOUNT() inside the CALCULATE so that it is affected by the desired filters.

Hi @lbendlin 

 

Don't know how to use variables instead of earlier. I did move the distinct count inside calculate but didnt give me the result desired:

Average = 
CALCULATE(
    DIVIDE(
    SUM ('Sheet1'[Mix]), DISTINCTCOUNT('Sheet1'[[Month 445]]])),
        FILTER ( 'Sheet1',
            [[ BPP Code ]]] = EARLIER ( [[ BPP Code ]]] )
                && 'Sheet1'[[L1.2 - Bottler]]]
                    = EARLIER ( 'Sheet1'[[L1.2 - Bottler]]] ) 
                        && 'Sheet1'[[ BU Ship From ]]] 
                            = EARLIER( 'Sheet1'[[ BU Ship From ]]])
        )
)

 

 

Thanks.

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.