Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
Hi,
The below code will create a measure that displays the entropy of an attribute. The lower the entropy value the better the separation between two classes. The example used here is from this webpage: http://www.learnbymarketing.com/481/decision-tree-flavors-gini-info-gain/
The table was loaded and then uppivoted. The attributes themselves have become values to filter on. The attachment contains the Power BI file where you can review the Power M code. Hope that you find this usefull.
Entropy = MINX (
ADDCOLUMNS (
VALUES ( 'Table (2)'[Value] ),
"v",
VAR _selVal =
CALCULATE ( SELECTEDVALUE ( 'Table (2)'[Value] ) )
VAR _PercALT =
CALCULATE ( [PercA], FILTER ( 'Table (2)', [Value] <= _selVal ) )
VAR _PercBLT =
CALCULATE ( [PercB], FILTER ( 'Table (2)', [Value] <= _selVal ) )
VAR _PercAGT =
CALCULATE ( [PercA], FILTER ( 'Table (2)', [Value] > _selVal ) )
VAR _PercBGT =
CALCULATE ( [PercB], FILTER ( 'Table (2)', [Value] > _selVal ) )
VAR _CountRowsLT =
COUNTROWS ( FILTER ( 'Table (2)', [Value] <= _selVal ) )
VAR _CountRowsGT =
COUNTROWS ( FILTER ( 'Table (2)', [Value] > _selVal ) )
VAR _CountRows =
COUNTROWS ( 'Table (2)' )
VAR _EntLT =
-1
* (
( _PercALT * LOG ( _PercALT, 2 ) )
+ ( _PercBLT * LOG ( _PercBLT, 2 ) )
)
VAR _EntGT =
-1
* (
( _PercAGT * LOG ( _PercAGT, 2 ) )
+ ( _PercBGT * LOG ( _PercBGT, 2 ) )
)
RETURN
( _CountRowsLT / _CountRows ) * _EntLT + ( _CountRowsGT / _CountRows ) * _EntGT
),
[v]
)ClassA = COUNTROWS ( FILTER ( 'Table (2)', [Class] = "A" ) )
ClassAll = COUNTROWS ( 'Table (2)' )
ClassB = COUNTROWS ( FILTER ( 'Table (2)', [Class] = "B" ) )
PercA = [ClassA] / [ClassAll]
PercB = [ClassB] / [ClassAll]
Kind regards, Steve.
eyJrIjoiNGNjNWY4ZmMtNDcyNi00N2IyLTk0OWUtNDRiODk0ZDc1NTAxIiwidCI6IjQxOTQ4NWNlLTE4ZDYtNDE2Yi04ODAyLTQ0YmQ2ZGNiNTU1OCIsImMiOjl9&pageName=ReportSection
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!