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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.