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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Nyarlathorep300
Regular Visitor

Computing chi squared in Dax - missing values

I have a regularly changing dataset, on which I'm trying to create a measure that computes chi squared statistics. I want a measure so the underlying data can be filtered and the statistic will be recalcualted accordingly.

 

The raw data is surey responses for people exiting, one row per person, that hold personal information about the individual and information about motives for exiting.  I made a measure that summarizes the table by a pair of characteristics, computes the expected values, then the the mean squared variation statistic, and then sums over the rows to get the chi squared statistics.

Table = ADDCOLUMNS ( 
                  ADDCOLUMNS (
                       SUMMARIZE (
                             LeavingFactorMaster,LeavingFactorMaster[Ethnicity], LeavingFactorMaster[LeavingFactor],
                             "people", count(LeavingFactorMaster[id])
                          ), 
                         "EFx1", CALCULATE(count(LeavingFactorMaster[id]),ALL(LeavingFactorMaster[Ethnicity])),
                          "EFx2", CALCULATE(count(LeavingFactorMaster[id]),ALL(LeavingFactorMaster[LeavingFactor])),
                           "EFx3", CALCULATE(count(LeavingFactorMaster[id]),ALL(LeavingFactorMaster[LeavingFactor],LeavingFactorMaster[Ethnicity])),
                            "EFx4", divide(                                                  CALCULATE(count(LeavingFactorMaster[id]),ALL(LeavingFactorMaster[Ethnicity]))*                                            	            	CALCULATE(count(LeavingFactorMaster[id]),ALL(LeavingFactorMaster[LeavingFactor])),                                        	CALCULATE(count(LeavingFactorMaster[id]),ALL(LeavingFactorMaster[LeavingFactor],LeavingFactorMaster[Ethnicity]))
                             )
                   ), 
                   "EFx5", divide(([people]-[EFx4])^2,[EFx4])
)

 

So far so good, EFx5 looks about right, but testing the output, there is an issue. If zero people in a ethinc group gave a particular LeavingFactor answer then there is no row in the summarized table, however this combination can have a non zero expected value. This means that the chi squared stat is lower than it should be.
I've tried to gap fill using code like this.

table2 =

var _tabl = ADDCOLUMNS(                         

                                GROUPBY(

                                                UNION (

                                                                SUMMARIZE (

                                                                                LeavingFactorMaster,LeavingFactorMaster[Ethnicity], LeavingFactorMaster[LeavingFactor],

                                                                                "pp", count(LeavingFactorMaster[id])

                                                                ),

                                                                ADDCOLUMNS(

                                                                                CROSSJOIN(

                                                                                                VALUES(LeavingFactorMaster[Ethnicity]), VALUES(LeavingFactorMaster[LeavingFactor])

                                                                                ),

                                                                "pp", 0)

                                                ),

                                                [Ethnicity],[LeavingFactor],

                                                "people1",sumx(CURRENTGROUP(),[pp])

                                ), "EFx1", 1

)

return _tabl

This does ensure that the zero rows are added, but I can't work out any way of doing calculations on "people1" to compute the chi-squared statistic like the first chunk of code.

 

So my question is am I on the right track? Is can this be tweaked to work? If not, is there an different approach that will compute the chi-squared stat correctly?

3 REPLIES 3
lbendlin
Super User
Super User

Any particular reason for not using the built-in function?

 

CHISQ.DIST function (DAX) - DAX | Microsoft Learn

That function requires the input X the sum ofvthe squares of the variation from the expected values, which is the number I'm  trying to compute.

 If zero people in a ethnic group gave a particular LeavingFactor answer

Is that because there were no leavers in that group or because they left but didn't fill the survey?

 

You can consider using COALESCE to fill voids.

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.