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
Marion
Frequent Visitor

nth occurence of a value

Hi,

 

Id like PowerBI to give me the nth occurrence of a value, in DAX or M language.

 

For each SKU that can appear several times, I have several potential suppliers. Id like to display the nth vendor per row as follows :

 

DateSKUVendor1st vendor2nd vendor
2021-08-06AppleFranckFranckFranck
2021-09-07StrawberryMaryMaryFranck
2021-09-20StrawberryFranckMaryFranck
2021-09-21PineapplePaulPaulnull
2021-11-10AppleMaryFranckFranck
2021-11-15CherryPaulPaulnull

 

Thanks for your help

1 ACCEPTED SOLUTION

Hi @Marion , the solutions are to be utlized as measures and not calculated columns. I f you refer to the attached pbix, you will see.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

7 REPLIES 7
smpa01
Super User
Super User

@Marion  you can utilize the following measures

1stVendor = 
CALCULATE (
    MAXX (
        FILTER (
            ADDCOLUMNS (
                'Table',
                "rank",
                    RANKX (
                        FILTER ( 'Table', EARLIER ( 'Table'[SKU] ) = 'Table'[SKU] ),
                        CALCULATE ( MAX ( 'Table'[Date] ) ),
                        ,
                        DESC
                    )
            ),
            [rank] = 1
        ),
        [Vendor]
    ),
    ALLEXCEPT ( 'Table', 'Table'[SKU] )
)

2ndVendor = 
CALCULATE (
    MAXX (
        FILTER (
            ADDCOLUMNS (
                'Table',
                "rank",
                    RANKX (
                        FILTER ( 'Table', EARLIER ( 'Table'[SKU] ) = 'Table'[SKU] ),
                        CALCULATE ( MAX ( 'Table'[Date] ) ),
                        ,
                        DESC
                    )
            ),
            [rank] = 2
        ),
        [Vendor]
    ),
    ALLEXCEPT ( 'Table', 'Table'[SKU] )
)

 

smpa01_0-1637009876894.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Marion
Frequent Visitor

Thank you smpa01 ! Looks like it is the right formula.

However I have the following error message when I copy-paste the formula for the 1st vendor in a new column: 

A circular dependency was detected: Table[Column]

do you know how to fix it?

Thanks a lot

Hi @Marion , the solutions are to be utlized as measures and not calculated columns. I f you refer to the attached pbix, you will see.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Marion
Frequent Visitor

It works thank you !

smpa01
Super User
Super User

@Marion  you will get nth occurnece by SKU with the following measure

Measure =
CALCULATE (
    MAX ( 'Table'[1st vendor] ),
    FILTER (
        'Table',
        'Table'[Date]
            = CALCULATE ( MAX ( 'Table'[Date] ), ALLEXCEPT ( 'Table', 'Table'[SKU] ) )
    )
)

 

smpa01_0-1637006433708.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Marion
Frequent Visitor

Hi smpa01 thanks for your answer.

Columns "1st vendor" and "2nd vendor" are the outputs Id like PowerBI to generate.

The initial database only contains colums "date", "sku" and "vendor".

Marion
Frequent Visitor

Sorry I made a mistake in my table :

 

DateSKUVendor1st vendor2nd vendor
2021-08-06AppleFranckFranckMary
2021-09-07StrawberryMaryMaryFranck
2021-09-20StrawberryFranckMaryFranck
2021-09-21PineapplePaulPaulnull
2021-11-10AppleMaryFranckMary
2021-11-15CherryPaulPaulnull

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