Search code examples
daxrank

Power BI DAX RankX only returns 1


I am new in Dax and I am studying the Rankx function. I have a simple table with Id, price, product, category.

When I use the function in the folowing way I only get rank 1 for all products and I am not understood why.

RankX = RANKX(
        all(dProduto),
        SUM(dProduto[preco])
)

What I am doing wrong?


Solution

  • The RANKX is used to rank rows based on a specific expression. In your example, you are trying to rank products based on their price.

    Since you are using ALL(dProduto), that means you are considering all the rows in the dProduto table. (This part seems fine).

    But for SUM(dProduto[preco]) here lies the problem. You are trying to rank the sum of all the prices from the dProduto table. This will always return the same value (the total sum) for each row, thus every row gets the same rank of 1.

    I assume that you have a ProductID column in your table, the measure is like below :

    RankX = RANKX(
        ALL(dProduto[id]), 
        CALCULATE(SUM(dProduto[preco]))
    )
    

    enter image description here