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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
sinanalmac
Resolver I
Resolver I

RANX - Create a sort column from a column that starts wih diffeent numbers

Hi all,


I have 12 weeks of data for every year. Some year's week starts with 24, someone starts 25 and some others start at 23, it changes year by year.
I realized I have always 12  weeks  and  week numbers are  not in order  and they start with different numbers.

I just want to create a new column with a condition like this.

no matter which number starts that year. the column will always start at 1 and end at 12



@MartynRamsden  helped me about this issue

 

But I have a trouble  with your formule  When I add more product  to the data  Count RANX number is increasing.
is there a soulution for  this ??

 

 

 

 here is power BI  link

 

https://www.dropbox.com/sh/rrcmf97oupdb3fr/AAAEW6-mDPVOjc6k5WBKS90ba?dl=0

 

His  formula

Calculated Column = VAR RowYear = 'Table'[Year] VAR Result =    RANKX (        FILTER (            'Table',            'Table'[Year] = RowYear        ),        'Table'[Number],        ,        ASC    ) RETURN    Result

 

My formula

Column =  var minnumber= CALCULATE(MIN('table'[Number]),ALLEXCEPT(table,table[year])) return IF('table'[Number] = minnumber,1,'table'[Number]-minnumber+1)

 



 

here is my table 

 

YearProduct Number Amount Quene
2018A23101
2018A24122
2018A25143
2018A26204
2018A2755
2018A2836
2018A2967
2018A30308
2018A311019
2018A321710
2018A332511
2018A356512
2019A24411
2019A25222
2019A26303
2019A2734
2019A2865
2019A2956
2019A3097
2019A31668
2019A32459
2019A331810
2019A346111
2019A365712
2018B23101
2018B24122
2018B25143
2018B26204
2018B2755
2018B2836
2018B2967
2018B30308
2018B311019
2018B321710
2018B332511
2018B356512
2019B24411
2019B25222
2019B26303
2019B2734
2019B2865
2019B2956
2019B3097
2019B31668
2019B32459
2019B331810
2019B346111
2019B365712

 

1 ACCEPTED SOLUTION
MartynRamsden
Solution Sage
Solution Sage

Hi @sinanalmac 

 

Sorry, I didn't realise when answering your original question that the same year and week could appear multiple times.

 

RANKX has an optional 5th argument which allows you to determine how ties should be handled.  When set to 'DENSE', all elements in a tie are counted only once. As such, my original solution can be modified as follows to give you the result you're looking for:

 

Calculated Column =
VAR RowYear = 'Table'[Year]
VAR Result =
    RANKX (
        FILTER (
            'Table',
            'Table'[Year] = RowYear
        ),
        'Table'[Number],
        ,
        ASC,
        DENSE
    )
RETURN
    Result

 

When dealing with multiple row contexts, I find it much easier to use variables rather than the EARLIER function (as suggested by @Anonymous ) but that's entirely personal preference.

View solution in original post

4 REPLIES 4
MartynRamsden
Solution Sage
Solution Sage

Hi @sinanalmac 

 

Sorry, I didn't realise when answering your original question that the same year and week could appear multiple times.

 

RANKX has an optional 5th argument which allows you to determine how ties should be handled.  When set to 'DENSE', all elements in a tie are counted only once. As such, my original solution can be modified as follows to give you the result you're looking for:

 

Calculated Column =
VAR RowYear = 'Table'[Year]
VAR Result =
    RANKX (
        FILTER (
            'Table',
            'Table'[Year] = RowYear
        ),
        'Table'[Number],
        ,
        ASC,
        DENSE
    )
RETURN
    Result

 

When dealing with multiple row contexts, I find it much easier to use variables rather than the EARLIER function (as suggested by @Anonymous ) but that's entirely personal preference.

hi @MartynRamsden ,
Thank you for your  quick reply. You are the best . If you  want something from istanbul /Turkey. Just tell me
May the force be with you

Anonymous
Not applicable

@sinanalmac 

Column = RANKX(FILTER('Table','Table'[Year]=EARLIER('Table'[Year])&& 'Table'[Product ]=EARLIER('Table'[Product ])),'Table'[Number ],,ASC)

Please try this calculated column to get year & product wise ranking 

Hi @Anonymous 
thank you for  Reply  I will try  your solution.

Best regards.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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