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
valcat27
Helper III
Helper III

Rank a column for each group but only for some cases

Hello all,

I have a table where each line represents a transaction (a sale), whose prices I want to rank by product, but only for some cases.

 

This table is what I have now, but not what I want.
(The horizontal lines are only to better visualise the ranking of each product.)

table1.png

 

This is my formula for rank column:

Rank =

VAR CurVal =  Table1[Price]

RETURN

CALCULATE(RANKX( Table1, Table1[Price], CurVal, ASC, Dense), FILTER(Table1,Table1[ProductID]=EARLIER(Table1[ProductID])))

 

 

Goal: to get the rank for each price (lower to highest), for each product… but there´s a catch: the flag. The flag is “on” when a certain characteristic is verified. I only care about ranking for a product if there is a transaction with the flag and if that transaction has the lowest price with the flag on.

Moreover, I would like to ignore prices equal zero (like ProductID 4) when ranking.

 

Therefore, the output that I want looks like this:

table2.png

 

Can anyone help me?

Thanks in advance.

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

Hi @valcat27 , 

 

You can create the following calculated column:

 

Rank =
VAR A =
    SUMMARIZE (
        'Table',
        'Table'[TranscationID],
        'Table'[ProductID],
        'Table'[Flag],
        'Table'[Price],
        "Rank",
            IF (
                'Table'[Price] = 0,
                BLANK (),
                RANKX (
                    FILTER (
                        ALL ( 'Table' ),
                        'Table'[ProductID] = EARLIER ( 'Table'[ProductID] )
                            && NOT ( 'Table'[Price] = 0 )
                    ),
                    'Table'[Price],
                    ,
                    ASC
                )
            )
    )
VAR b =
    SUMX (
        FILTER (
            A,
            'Table'[ProductID] = EARLIER ( 'Table'[ProductID] )
                && NOT ( ISBLANK ( 'Table'[Flag] ) )
        ),
        [Rank]
    )
RETURN
    IF (
        b = 1,
        SUMX (
            FILTER ( A, 'Table'[TranscationID] = EARLIER ( 'Table'[TranscationID] ) ),
            [Rank]
        ),
        BLANK ()
    )

Capture2.PNG

 

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

 

Best Regards,

Dedmon Dai

 

 

View solution in original post

2 REPLIES 2
v-deddai1-msft
Community Support
Community Support

Hi @valcat27 , 

 

You can create the following calculated column:

 

Rank =
VAR A =
    SUMMARIZE (
        'Table',
        'Table'[TranscationID],
        'Table'[ProductID],
        'Table'[Flag],
        'Table'[Price],
        "Rank",
            IF (
                'Table'[Price] = 0,
                BLANK (),
                RANKX (
                    FILTER (
                        ALL ( 'Table' ),
                        'Table'[ProductID] = EARLIER ( 'Table'[ProductID] )
                            && NOT ( 'Table'[Price] = 0 )
                    ),
                    'Table'[Price],
                    ,
                    ASC
                )
            )
    )
VAR b =
    SUMX (
        FILTER (
            A,
            'Table'[ProductID] = EARLIER ( 'Table'[ProductID] )
                && NOT ( ISBLANK ( 'Table'[Flag] ) )
        ),
        [Rank]
    )
RETURN
    IF (
        b = 1,
        SUMX (
            FILTER ( A, 'Table'[TranscationID] = EARLIER ( 'Table'[TranscationID] ) ),
            [Rank]
        ),
        BLANK ()
    )

Capture2.PNG

 

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

 

Best Regards,

Dedmon Dai

 

 

Hello @v-deddai1-msft , 

 

Thank you very much for your answer. It works exactly as I wanted. 

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
Top Kudoed Authors