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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
lokosrio
Helper II
Helper II

Sum of rows for max id in a group

hi,

 

I have a problem with dax measure. I want to count the rows where the flag = 1, but only those rows where max_id column have biggest value in a group, which represents group_dimension. At the end I want to divide it by all rows considerd in the calculation.

 

Sample data:

iddimensionmax_idgroup_dimensionflag
1dimension_999123dimension11
2dimension_999124dimension10
3dimension_999125dimension21
4dimension_999126dimension20
5dimension_999127dimension31
6dimension_999128dimension31
7dimension_999129dimension41
8dimension_999130dimension41
9dimension_999130dimension41

 

Final calculation for sample data:

Rows where flag = 1: 3

Rows considered in the calculation: 5

Result: 3/5 = 0,6

 

Please keep in mind that in the final table there will not be max_id and group_dimension columns, so I want to have aggregated result for other dimensions. What is more there are some filters, so it need to change dynamically.

 

I started with below formula, but it is not working for me:

 

 

var maxID =
MAXX (
    ALLSELECTED ( sample_data ),
    CALCULATE ( MAX ( sample_data [max_id] ), VALUES ( sample_data [group_dimension] ) )
)
return
CALCULATE(
COUNTROWS(sample_data ), sample_data [flag] = 1
, sample_data [max_id] = maxID)

 

 

 

1 ACCEPTED SOLUTION
v-chenwuz-msft
Community Support
Community Support

Hi @lokosrio,

 

Create two columns to mark this row if considered in calculation with 0/1.

First column:

Numerator =

VAR IF_max = CALCULATE(MAX('Table'[max_id]),FILTER('Table',[group_dimension]=EARLIER('Table'[group_dimension])))

return

IF(IF_max= 'Table'[max_id] &&'Table'[flag]=1,1,0)

 

Denominator =

VAR IF_considered = CALCULATE(MAX('Table'[Numerator]),FILTER('Table',[group_dimension]=EARLIER('Table'[group_dimension])))

return

if(IF_considered=1,1,0)

 

then create a measure:

Result = CALCULATE(DIVIDE(SUM('Table'[Numerator]),SUM('Table'[Denominator])),FILTER(ALL('Table'),'Table'[dimension]=MAX([dimension])))

 

The result:

vchenwuzmsft_0-1631497066490.png

 

With the measure, you can have aggregated result for other dimensions, it calculates dynamically.

 

 

Best Regards

Community Support Team _ chenwu zhu

 

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

2 REPLIES 2
v-chenwuz-msft
Community Support
Community Support

Hi @lokosrio,

 

Create two columns to mark this row if considered in calculation with 0/1.

First column:

Numerator =

VAR IF_max = CALCULATE(MAX('Table'[max_id]),FILTER('Table',[group_dimension]=EARLIER('Table'[group_dimension])))

return

IF(IF_max= 'Table'[max_id] &&'Table'[flag]=1,1,0)

 

Denominator =

VAR IF_considered = CALCULATE(MAX('Table'[Numerator]),FILTER('Table',[group_dimension]=EARLIER('Table'[group_dimension])))

return

if(IF_considered=1,1,0)

 

then create a measure:

Result = CALCULATE(DIVIDE(SUM('Table'[Numerator]),SUM('Table'[Denominator])),FILTER(ALL('Table'),'Table'[dimension]=MAX([dimension])))

 

The result:

vchenwuzmsft_0-1631497066490.png

 

With the measure, you can have aggregated result for other dimensions, it calculates dynamically.

 

 

Best Regards

Community Support Team _ chenwu zhu

 

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

amitchandak
Super User
Super User

@lokosrio , I doubt it it 3 by 5. I am getting only 2/4 with data you shared

 

check this

 

 divide(sumx(VALUES(DT1[group_dimension]), LASTNONBLANKVALUE(DT1[id], sum(DT1[flag]))), DISTINCTCOUNT(DT1[group_dimension]))

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.