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

How Can I Create a RANKX Measure Without Triggering a Crossjoin?

Hi, I have two tables Companies and Transactions that have a 1-to-many company-to-transaction relationship and are linked in the model by a Company ID field.

 

The Companies table has the following fields:

  1. Company ID
  2. Company Name

While the Transactions table has the following fields (note the fund field in particular - a single company can have multiple transactions with 1 or more fund(s)):

  1. Transaction ID
  2. Related Company ID
  3. Date
  4. Fund
  5. Amount

I'm trying to create a measure (DateRank) that ranks the transactions by date in each Company-Fund group and then display that in a table that shows the Company Name, Fund, Transaction ID, and calculated rank.

 

I haven't been able to do this without ending up with a Cartesian product of the two tables where the rank is calculated correctly for rows where the Companies[Company ID] matches the Transactions[Related Company ID], but the table also ends up having extra rows for all items in the Transactions table (even the ones where the company id's don't match) with the calculated value being 1.

 

As part of my research online I found a bunch of blogs (example) that talk about DAX auto-exist resulting in behavior like this, but while they did a really good job of explaining why that behavior was happening I couldn't find in them any solution that worked for my use case.

 

I would really appreciate someone's help here as there must be some way to do this!

 

Here is the formula that I have so far:

 

 

 

Measure DateRank =
    VAR thisFund = SELECTEDVALUE(Transactions[Fund])
    VAR thisDate = SELECTEDVALUE(Transactions[Date])
    Return
        CALCULATE(
            RANKX(
                ALLSELECTED(Transactions),
                Transactions[Date],
                thisCloseDate,
                DESC
            ),
            KEEPFILTERS(Transactions[Fund] = thisFund)
        )

 

 

 

2 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@Hamzehn , if you need rank on date in this table this can be column not measure

 

rank = rankx(filter(Table, [related company id] = earlier( [related company id] )  && [Fund] =earlier(Fund) ), [Date],,desc, dense)

 

You can have measure rank on max date

 

example

rank = rankx(filter(Table, [related company id] = max( [related company id] ) ), calculate(max([Date])),,desc, dense)

 

 

For Rank Refer these links
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures
https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns
https://radacad.com/how-to-use-rankx-in-dax-part-3-of-3-the-finale
https://community.powerbi.com/t5/Community-Blog/Dynamic-TopN-made-easy-with-What-If-Parameter/ba-p/3...

View solution in original post

v-yetao1-msft
Community Support
Community Support

Hi @Hamzehn 

Is this the result you want ? Group by Related Company ID and Fund , rank by Date .

Ailsamsft_0-1630400369099.png

You can create a column to return rank .

rank = rankx(filter(Transactions, [related company id] = earlier( [related company id] ) && [Fund] =earlier(Transactions[Fund]) ), [Date],,ASC, dense)

I have attached my pbix file ,you can refer to it .

 

Best Regards

Community Support Team _ Ailsa Tao

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

View solution in original post

3 REPLIES 3
v-yetao1-msft
Community Support
Community Support

Hi @Hamzehn 

Is this the result you want ? Group by Related Company ID and Fund , rank by Date .

Ailsamsft_0-1630400369099.png

You can create a column to return rank .

rank = rankx(filter(Transactions, [related company id] = earlier( [related company id] ) && [Fund] =earlier(Transactions[Fund]) ), [Date],,ASC, dense)

I have attached my pbix file ,you can refer to it .

 

Best Regards

Community Support Team _ Ailsa Tao

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

amitchandak
Super User
Super User

@Hamzehn , if you need rank on date in this table this can be column not measure

 

rank = rankx(filter(Table, [related company id] = earlier( [related company id] )  && [Fund] =earlier(Fund) ), [Date],,desc, dense)

 

You can have measure rank on max date

 

example

rank = rankx(filter(Table, [related company id] = max( [related company id] ) ), calculate(max([Date])),,desc, dense)

 

 

For Rank Refer these links
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures
https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns
https://radacad.com/how-to-use-rankx-in-dax-part-3-of-3-the-finale
https://community.powerbi.com/t5/Community-Blog/Dynamic-TopN-made-easy-with-What-If-Parameter/ba-p/3...

Thanks @amitchandak. I think you're right and I may not need a measure exactly to do rankings. For my purpose, since this was going to be an intermediate step of what I was trying to do, I figured I may even get away without having to do the rank by just relying on the date column that's already there.

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.