cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
lokosrio
Helper I
Helper I

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.

View solution in original post

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]))
Did I answer your question? Mark my post as a solution! Appreciate your Kudos!! !!
Dashboard of My Blogs !! Connect on Linkedin
Want To Learn Power BI
Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
Proud to be a Super User!

Helpful resources

Announcements
Power BI December 2021 Update_carousel 768x460.jpg

Check it Out!

Click here to read more about the December 2021 Updates!

Jan 2022 Dev Camp 768x460 copy.png

Power BI Dev Camp- January 27th, 2022

Mark your calendars and join us for our next Power BI Dev Camp!

UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Top Solution Authors
Top Kudoed Authors