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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Calculations by Rank

Capture.PNGI

In my Var column I'd like to calculate the variance between the current rank and the next rank down/lower rank (for each Type). Eg: for Type B (on row 7) rank 4 is 120 less rank 3 (on row 6) is 105 thefore variance = 15.  Many thanks in advance!

3 ACCEPTED SOLUTIONS
ChrisMendoza
Resident Rockstar
Resident Rockstar

@Anonymous -

 

Would the below work for you?

 

Var = 
VAR nextRank = Table1[Rank] + 1
VAR differnce = 
    CALCULATE(
        VALUES(Table1[Value]),
        FILTER(ALLEXCEPT(Table1,Table1[Type]),Table1[Rank] = nextRank)
    ) - Table1[Value]
RETURN
    IF(
        differnce >= 0,
        differnce,
        BLANK()
    )





Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



View solution in original post

@Anonymous -

 

The entire code block is your calculated column. Documentation on using VAR can be found @ https://docs.microsoft.com/en-us/dax/var-dax and a nice example article can be found @ https://www.sqlbi.com/articles/variables-in-dax/

 

 






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



View solution in original post

@Anonymous -

 

You're missing closing ' ) ' in a couple places.

 

Copy -> Paste my code and replace 'Table1' with 'Primary_ID' or try formatting multi-line by clicking ALT + ENTER so you can see your opening and closing Parentheses ' ( ' & ' )' respectively a little easier.

 

 






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



View solution in original post

10 REPLIES 10
ChrisMendoza
Resident Rockstar
Resident Rockstar

@Anonymous -

 

Would the below work for you?

 

Var = 
VAR nextRank = Table1[Rank] + 1
VAR differnce = 
    CALCULATE(
        VALUES(Table1[Value]),
        FILTER(ALLEXCEPT(Table1,Table1[Type]),Table1[Rank] = nextRank)
    ) - Table1[Value]
RETURN
    IF(
        differnce >= 0,
        differnce,
        BLANK()
    )





Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



Anonymous
Not applicable

Hi Chris,

I'm afraid I don't really understand your reply (my fault).

Which bit, exactly, do I need to type into my formula bar?

Many thanks

Peter

 

Anonymous
Not applicable

In particular I don'r understand how to feed this expression into the formula;

 

Var = 
VAR nextRank = Table1[Rank] + 1

Thanks 

@Anonymous -

 

The entire code block is your calculated column. Documentation on using VAR can be found @ https://docs.microsoft.com/en-us/dax/var-dax and a nice example article can be found @ https://www.sqlbi.com/articles/variables-in-dax/

 

 






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



Anonymous
Not applicable

Many thanks Chris.

 

I'll give it a go!

Anonymous
Not applicable

It almost works Chris.

 

However this is the result I'd really like;

 

Primary IDTypeDateValueRankVar
Type A20170101Type A01/01/1710010
Type A20170201Type A01/02/17110210
Type A20170301Type A01/03/17125315
Type B20180101Type B01/01/189010
Type B20180201Type B01/02/189525
Type B20180301Type B01/03/18105310
Type B20180401Type B01/04/18120415

 

Where the Var for Rank 1 is alway 0 as there is no lower rank. The var for rank 2 onwards is the value of rank 2 less rank 1, value of rank 3 less rank 2 etc.

 

Sorry to be a nusiance!

@Anonymous -

 

Var = 
VAR prevRank = Table1[Rank] - 1
VAR differnce = 
    Table1[Value] -
    CALCULATE(
        VALUES(Table1[Value]),
        FILTER(ALLEXCEPT(Table1,Table1[Type]),Table1[Rank] = prevRank )
    )
RETURN
    IF(
        differnce < Table1[Value],
        differnce,
        0
    )

Capture.PNG

 






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



Anonymous
Not applicable

Here's my code...

 

= VAR prevRank = Primary_ID[Rank] - 1 VAR differnce = Primary_ID[Value] - CALCULATE( VALUES(Primary_ID[Value], FILTER(ALLEXCEPT(Primary_ID,Primary_ID[Type],Primary_ID[Rank] = prevRank ) ) RETURN IF( differnce < Primary_ID[Value], differnce, 0 )

 

and here's the error I get...

 

Capture.PNG

@Anonymous -

 

You're missing closing ' ) ' in a couple places.

 

Copy -> Paste my code and replace 'Table1' with 'Primary_ID' or try formatting multi-line by clicking ALT + ENTER so you can see your opening and closing Parentheses ' ( ' & ' )' respectively a little easier.

 

 






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



Anonymous
Not applicable

Perfect! Many thanks for perservering with me Chris.

Much appreciated.

I learnt a lot.

Thanks again.

Peter

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors