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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
jthomson
Solution Sage
Solution Sage

TOPN sort order with USERELATIONSHIP

Hi,

 

Banging my head against the wall trying to work out how to get this to work. This is the measure in question:

 

 

 

Top2% = 
var claims = CALCULATE(COUNTROWS(dcl),USERELATIONSHIP(DateTable[Date],dcl[Settled Date]))
var top2calc = CALCULATE(ROUNDDOWN(divide(claims,50),0),USERELATIONSHIP(DateTable[Date],dcl[Settled Date]))
var sumofthat = CALCULATE(sum(dcl[Total Incurred]),topn(top2calc,dcl,dcl[Total Incurred],desc))
return divide(sumofthat,top2calc)

 

 

 

Basically I have a date table which does what it says, then I have a table dcl, which has a list of data with three pertinent fields:

 

- reported date, which has the active relationship between dcl and the date table

- settled date, which has an inactive relationship between dcl and the date table

- total incurred, which is the important value on each line

 

What I'm wanting to do is to calculate the average cost of the top 2% of claims based on when they were settled - to do this, I'm using the variable claims to work out how many claims were settled in a given time period, then using the variable top2calc to work out how many of these claims I'm interested in looking at. Both of these work. The issue I'm having is with the variable sumofthat - as of right now, it is correctly returning the sum of the top 2% of claims, but based on reporting date, and I'm not sure where exactly in the measure to tell it to use the other relationship instead - most places where i've put in USERELATIONSHIP that I've tried has caused the measure to not return a value at all. Any ideas on how to fix?

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

Hi @jthomson ,

 

You want to add Userelationship to sumofthat?

If yes, please try add CALCULATETABLE() to the TOPN()'s table, second parameter.

 

Top2% =
VAR claims =
    CALCULATE(
        COUNTROWS( dcl ),
        USERELATIONSHIP ( DateTable[Date], dcl[Settled Date] )
    )
VAR top2calc =
    CALCULATE(
        ROUNDDOWN( DIVIDE( claims, 50 ), 0 ),
        USERELATIONSHIP ( DateTable[Date], dcl[Settled Date] )
    )
VAR sumofthat =
    SUMX(
        TOPN(
            2,
            CALCULATETABLE( 'dcl', USERELATIONSHIP ( DateTable[Date], dcl[Settled Date] ) ),
            [total incurred], DESC
        ),
        [total incurred]
    )
RETURN
    DIVIDE( sumofthat, top2calc )

 

If i misunderstood you, please share your pbix file without sensitive data and expect result.

Pbix in the end you can refer.

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

1 REPLY 1
v-chenwuz-msft
Community Support
Community Support

Hi @jthomson ,

 

You want to add Userelationship to sumofthat?

If yes, please try add CALCULATETABLE() to the TOPN()'s table, second parameter.

 

Top2% =
VAR claims =
    CALCULATE(
        COUNTROWS( dcl ),
        USERELATIONSHIP ( DateTable[Date], dcl[Settled Date] )
    )
VAR top2calc =
    CALCULATE(
        ROUNDDOWN( DIVIDE( claims, 50 ), 0 ),
        USERELATIONSHIP ( DateTable[Date], dcl[Settled Date] )
    )
VAR sumofthat =
    SUMX(
        TOPN(
            2,
            CALCULATETABLE( 'dcl', USERELATIONSHIP ( DateTable[Date], dcl[Settled Date] ) ),
            [total incurred], DESC
        ),
        [total incurred]
    )
RETURN
    DIVIDE( sumofthat, top2calc )

 

If i misunderstood you, please share your pbix file without sensitive data and expect result.

Pbix in the end you can refer.

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.

 

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.