Rank products within their category with RANKX

Show each product’s place inside its own category, so the best accessory ranks 1 even though bikes outsell it.

You will learn to

  • Build a custom list of competitors with FILTER(ALL(…))
  • Capture the current category in a variable
  • Explain why ALLEXCEPT does not do this on its own

How it works

An overall rank puts all four bikes on top, so the best accessory sits in 5th place. Often you want a **rank within the group** instead: 1st among accessories, 1st among clothing.

The list of competitors must then be “all products in the same category as this one”. Build it in two steps: - Read the current product’s category into a variable with `SELECTEDVALUE(Product[Category])`. In a row for one product, there is exactly one category. - Use `FILTER(ALL(Product), Product[Category] = …)` to bring back every product and keep the ones in that category.

Why not `ALLEXCEPT(Product, Product[Category])`? It keeps a filter on Category only if one exists. In a matrix with just products on the rows, nothing filters Category directly, so it returns all twelve products and you get the overall rank again.

Practice it hands-on with a real data model and a live report that updates as you type.

Rank in Color =
VAR ThisColor = SELECTEDVALUE(Product[Color])
RETURN RANKX(FILTER(ALL(Product), Product[Color] = ThisColor), [Total Sales])