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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
zgaljiv
New Member

weighted average calculation in R and DAX -- return same results

Hi, I am computing a weighted average in R using the below code.  I'd like the weighted average result produced in Power BI to match the R code but the results differ -- any thoughts on how I should modify the DAX formula so results in Power BI and R are the same?

 

R code:

raw_data<-data.frame(group = c(rep("batting sports",3),"contact sports"),
subgroup = c("baseball", "cricket", "softball", "football"),
current_fan_base = c(15873742, 330102240, 92893081, 106040),
popularity_grade = c(.5,.9,NA,.99))


summary <- raw_data %>% group_by(group) %>%
dplyr::summarize(
group_pop_grade = sum(popularity_grade * current_fan_base, na.rm = T)/sum(current_fan_base[!is.na(popularity_grade)]))

 

 

R results:

 batting sports 0.882
 contact sports 0.99

 

Power BI DAX formula

(created using the quick measure tool, with sum of popularity grade as the base value, current fan base as the weight and group as the category)

popularity_grade weighted by current_fan_base per popularity_grade =

VAR __CATEGORY_VALUES = VALUES('raw_data'[popularity_grade])

RETURN

    DIVIDE(

        SUMX(

            KEEPFILTERS(__CATEGORY_VALUES),

            CALCULATE(

                SUM('raw_data'[popularity_grade])

                    * SUM('raw_data'[current_fan_base])

            )

        ),

        SUMX(

            KEEPFILTERS(__CATEGORY_VALUES),

            CALCULATE(SUM('raw_data'[current_fan_base]))

        )

    )

 

Power BI DAX results

group

popularity_grade weighted by current_fan_base per popularity_grade

contact sports

0.99

batting sports

0.695033924

 

 

0 REPLIES 0

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.