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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
palkamj
Frequent Visitor

How to calculate GINI coefficient as a measure

Hi,

 

I'm wondering if it's possible to calculate the GINI coefficient as a measure.  I have data on panel studies which contains for each study ID the number of answers in that study. So basically one column for study IDs and another for the number of answers. These studies belong in different categories, although one study can belong in many categories at once.. I'd like to use the GINI coefficient to portray how evenly the answers are divided between studies, and then show this on a multi-row card for each category.

1 ACCEPTED SOLUTION
Mikelg
New Member

Hi @palkamj,

 

This is definitely possible.

 

If we have a table ("IncomeTable") which contains a column with the data by individual:

Capture.JPG

 

And if we add this measure to the data model:

(Note: adjust row 2 so that SELECTCOLUMNS points to the column that contains your data)

 

 

Gini Coefficient2 = 
VAR DataColumn = SELECTCOLUMNS(IncomeTable,"Data",IncomeTable[Income])
VAR Areas_Under_Lorenz_Curve =
    ADDCOLUMNS (
        DataColumn,
        "Area",
        VAR IteratingIncome = [Data]
        VAR LessOrEqual_Table =
            FILTER ( DataColumn, [Data] <= IteratingIncome )
        VAR LessOrEqualCummulative =
            SUMX ( LessOrEqual_Table, [Data] )
        VAR Less_Table =
            FILTER ( DataColumn, [Data] < IteratingIncome )
        VAR Less_Cummulative =
            SUMX ( Less_Table, [Data] )
        VAR TotalIncome =
            SUMX ( DataColumn, [Data])
        VAR NoOfDataPoints =
            COUNTROWS ( DataColumn )
        RETURN
            DIVIDE (
                DIVIDE ( LessOrEqualCummulative + Less_Cummulative, 2 ),
                TotalIncome * NoOfDataPoints
            )
    )
RETURN
    ( 0.5 - SUMX ( Areas_Under_Lorenz_Curve, [Area] ) ) / 0.5

 

 

Then your report will calculate gini coefficients at any filter level:

Capture1.JPG

@v-shex-msft if you agree with please mark it as the solution to this question

View solution in original post

3 REPLIES 3
Mikelg
New Member

Hi @palkamj,

 

This is definitely possible.

 

If we have a table ("IncomeTable") which contains a column with the data by individual:

Capture.JPG

 

And if we add this measure to the data model:

(Note: adjust row 2 so that SELECTCOLUMNS points to the column that contains your data)

 

 

Gini Coefficient2 = 
VAR DataColumn = SELECTCOLUMNS(IncomeTable,"Data",IncomeTable[Income])
VAR Areas_Under_Lorenz_Curve =
    ADDCOLUMNS (
        DataColumn,
        "Area",
        VAR IteratingIncome = [Data]
        VAR LessOrEqual_Table =
            FILTER ( DataColumn, [Data] <= IteratingIncome )
        VAR LessOrEqualCummulative =
            SUMX ( LessOrEqual_Table, [Data] )
        VAR Less_Table =
            FILTER ( DataColumn, [Data] < IteratingIncome )
        VAR Less_Cummulative =
            SUMX ( Less_Table, [Data] )
        VAR TotalIncome =
            SUMX ( DataColumn, [Data])
        VAR NoOfDataPoints =
            COUNTROWS ( DataColumn )
        RETURN
            DIVIDE (
                DIVIDE ( LessOrEqualCummulative + Less_Cummulative, 2 ),
                TotalIncome * NoOfDataPoints
            )
    )
RETURN
    ( 0.5 - SUMX ( Areas_Under_Lorenz_Curve, [Area] ) ) / 0.5

 

 

Then your report will calculate gini coefficients at any filter level:

Capture1.JPG

@v-shex-msft if you agree with please mark it as the solution to this question

Hello @Mikelg,
Thanks a lot for sharing,

when applying your formula to a simple test table 

GroupIncome
A500
B0
C0

 

The results obtain is 0.67 while it should be 1 as all income is detain by one group...
What surprise me is that nowhere in the formula we precise the grouping category
Let me know if i do something wrong or if any fixes need to be done to the formula


problema Gini.png

v-shex-msft
Community Support
Community Support

HI @palkamj,

 

I don't think it is possible to achieve this requirement, may be your can take a look at below link which told about use excel to achieve gini coefficient.(include coding formula)

Fast Gini – calculating Gini coefficients in Excel efficiently

 

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.