Forum Discussion

GrahamR99's avatar
GrahamR99
Resolver I
2 years ago
Solved

Ranking on calculated measure

Hello
I have this formula;
Distance (Rank) = RANKX(FILTER('Legacy Events Planning 2024','Legacy Events Planning 2024'[Supporter_Postcode]=EARLIER('Legacy Events Planning 2024'[Supporter_Postcode])),'Legacy Events Planning 2024'[Distance (km)],,ASC,Dense)
I get this error;
A circular dependency was detected.
Distance (km) is a calculated field.  Which I guess is the reason I get this error.
How do I rank the data and get around this error?
 
Thank you for reading.
 
Regards
 
GrahamR99
  • hi, GrahamR99 

    GrahamR99 

     

    try to make new column 

     

    Column = 
    RANK(
         DENSE,
         ALLSELECTED(
             'Table (2)'[Sup Postcode],
             'Table (2)'[Distance (km)]
        ),
        ORDERBY('Table (2)'[Distance (km)],ASC)
        ,,PARTITIONBY('Table (2)'[Sup Postcode])
    )

     

     

     

    if you want to measure then try below code

    Measure 2 = 
    var a = ALL('Table (2)'[Distance (km)],'Table (2)'[Sup Postcode])
    return
    RANK(
        DENSE,
        a,
        ORDERBY(
            'Table (2)'[Distance (km)],
            ASC
        ),,
        PARTITIONBY('Table (2)'[Sup Postcode])
        
    )

     

     

     

  • Hello Dangar332 

    Thank you for the formula, it worked when I change the data.

     

    When I first setup the Power BI report, I used Power BI mesure to calculate the distance between two locations.  When I recreated the data in SQL Server and used it's ablity to calulate the distance, the data was a column and I could use your formula and also slice the distance column in my Power BI report which I could not do when it was a mesure.

     

    The SQL Script I used was;

    DistanceInMiles = GEOGRAPHY::Point([Lat], [Long], 4326).STDistance(GEOGRAPHY::Point([latitude], [longitude], 4326)) / 1609.344

     

    Your formula I used was.

     

    Column = 
    RANK(
         DENSE,
         ALLSELECTED(
             'Table (2)'[Sup Postcode],
             'Table (2)'[Distance (km)]
        ),
        ORDERBY('Table (2)'[Distance (km)],ASC)
        ,,PARTITIONBY('Table (2)'[Sup Postcode])
    )

     

    Thank you for your help.

     

    Regards

     

    GrahamR99

     

20 Replies

  • 123abc's avatar
    123abc
    Community Champion

    You can try this modified Dax funciton:

    Distance (Rank) =
    RANKX(
    FILTER(
    'Legacy Events Planning 2024',
    'Legacy Events Planning 2024'[Supporter_Postcode] = EARLIER('Legacy Events Planning 2024'[Supporter_Postcode])
    ),
    CALCULATE('Legacy Events Planning 2024'[Distance (km)]),
    ,
    ASC,
    Dense
    )

     

    In this modified formula, I replaced 'Legacy Events Planning 2024'[Distance (km)] with CALCULATE('Legacy Events Planning 2024'[Distance (km)]). This can help break the circular dependency, as CALCULATE allows you to create a context transition and evaluate the measure in a different context.

    Try this modification and see if it resolves the circular dependency issue. If you encounter any further issues or if you have additional requirements, feel free to provide more details.

     

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

     

    In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

    • GrahamR99's avatar
      GrahamR99
      Resolver I

      Hello 123abc 

      I get a different error now which is;

      Earlier/Earliest refers to an earlier context which doesn't exist.

       

      Do you know how to resolve?

       

      Regards

       

      GrahamR99

      • 123abc's avatar
        123abc
        Community Champion

        Plz use this modified DAX:

        Distance (Rank) =
        RANKX(
        FILTER(
        'Legacy Events Planning 2024',
        'Legacy Events Planning 2024'[Supporter_Postcode] = EARLIER('Legacy Events Planning 2024'[Supporter_Postcode])
        ),
        CALCULATE(
        SUM('Legacy Events Planning 2024'[Distance (km)]),
        ALLEXCEPT('Legacy Events Planning 2024', 'Legacy Events Planning 2024'[Supporter_Postcode])
        ),
        ,
        ASC,
        Dense
        )

         

        if not work please sahre Pbxi file or excel file with sample data.