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
Anonymous
Not applicable

Rankx within group combined with dateadd

Hello everyone,

 

I found Rankx can be completed if used as a measure. Any help will be greatly appreciated! 

 

I have built a Date table and one to many relationship with the Test Table on the “Date” Column ( Please see the screenshot below).

WhistleBee_0-1646306186822.png

 

 

I would like to achieve the following:

Date

Link Code

 Volumes

PM Volumes

Rank

PM Rank

01/04/2021

A

18

 

3

1

01/04/2021

B

62

 

1

1

01/04/2021

C

19

 

2

1

01/05/2021

A

12

19

3

2

01/05/2021

B

103

12

1

3

01/05/2021

C

15

103

2

1

01/06/2021

A

33

15

1

3

01/06/2021

B

18

33

3

1

01/06/2021

C

29

18

2

2

However, this is what returned with my Dax below

Date

Link Code

 Volumes

PM Volumes

Rank

PM Rank

01/04/2021

A

18

 

3

1

01/04/2021

B

62

 

1

1

01/04/2021

C

19

 

2

1

01/05/2021

A

12

19

3

1

01/05/2021

B

103

12

1

1

01/05/2021

C

15

103

2

1

01/06/2021

A

33

15

1

1

01/06/2021

B

18

33

3

1

01/06/2021

C

29

18

2

1

 

Rank =

RANKX(filter(all('Test table'[Date],'Test table'[Link Code]),'Test table'[Date]=max('Test table'[Date])),CALCULATE(sum('Test table'[ Volumes])),,DESC)

 

PM Volumnes = CALCULATE(sum('Test table'[ Volumes]),dateadd('Dim Date 2'[Date],-1,month))

 

PM rank =

rankx(filter(all('Test table'[Date],'Test table'[Link Code]),'Test table'[Date]=max('Test table'[Date])),[PM Volumnes],,desc)

 

May I ask where I did wrong? Why PM Rank doesn’t return as expected?

 

Thank you.

 

Jing

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

RANKX() is one of caviars of DAX; but also is tremendously tricky one in spite that it's very commonly used.

 

PM Rank = 
VAR __pm = DATEADD( DATES[Date], -1, MONTH )
RETURN
    IF(
        NOT ISEMPTY( CALCULATETABLE( TEST, __pm ) ),
        RANKX(
            SUMMARIZE(
                CALCULATETABLE( TEST, __pm, ALLSELECTED( TEST[Link Code] ) ),
                TEST[Link Code],
                DATES[yyyy-MMM]
            ),
            [Total Vol],
            CALCULATE( [Total Vol], __pm )
        )
    )

 

CNENFRNL_0-1646335059256.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

3 REPLIES 3
CNENFRNL
Community Champion
Community Champion

RANKX() is one of caviars of DAX; but also is tremendously tricky one in spite that it's very commonly used.

 

PM Rank = 
VAR __pm = DATEADD( DATES[Date], -1, MONTH )
RETURN
    IF(
        NOT ISEMPTY( CALCULATETABLE( TEST, __pm ) ),
        RANKX(
            SUMMARIZE(
                CALCULATETABLE( TEST, __pm, ALLSELECTED( TEST[Link Code] ) ),
                TEST[Link Code],
                DATES[yyyy-MMM]
            ),
            [Total Vol],
            CALCULATE( [Total Vol], __pm )
        )
    )

 

CNENFRNL_0-1646335059256.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

@CNENFRNL  Thank you very much for your help! It works. I can't say I fully understand the codes yet but I think I will understand it better while I am geting more advanced in Dax. Many thanks! 

amitchandak
Super User
Super User

@Anonymous , Please date frmpm date table in Rank and visual

 

rankx(filter(all('Date'[Date],'Test table'[Link Code]),'Date'[Date]=max('Date'[Date])),[PM Volumnes],,desc)

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.