DAX RANKX: Why does it return 1? - powerbi

I am using AdventureWorks DB and I want to rank the resellers by their total sale amount. So basically the table looks like this:
I used the following measure to rank it:
Rank Reseller = RANKX(ALL(ResellerSales), MAX(ResellerSales[SalesAmount]),,1)
But it returns only 1 as the rank.
I followed these two videos:
https://www.youtube.com/watch?v=z2qzJVeYhTY
and
https://www.youtube.com/watch?v=SsZseKOgrWQ&t=603s
but I can't understand what is wrong!

This has to do with the current scope that Rankx evaluates de Aggregation.
Try wrapping your aggregation with CALCULATE, and you probably want the SUM not the MAX:
Rank Reseller = RANKX(ALL(ResellerSales), CALCULATE(SUM(ResellerSales[SalesAmount])))
You can create a Measure like so, and use it on RANKX, since it is a measure it will work without explicitly adding the CALCULATE:
Sales Amount = SUM(ResellerSales[SalesAmount])
Rank Reseller = RANKX(ALL(ResellerSales), [Sales Amount])
EDIT:
Rank Reseller = RANKX(ALL('ResellerSales'[Resellerkey]), [Sales Amount])
Try it like this.

To rank the [ReSellerkey] by [SalesAmount] you'd want to do something like this:
Rank Sales Amount :=
RANKX(
'Table',
'Table'[SalesAmount],
,
ASC,
Dense
)

Related

DAX - Rank by product category

I am trying to create a ranking for each product grouping in the product table (Product, Model, Product subcategory, Category).
I was unsuccessful with the rank by category.
I am trying to understand why the same formula pattern doesn't work for only the category.
Rank by Category =
RANKX(
ALLSELECTED('Product'[Category]),
[Amount]
)
Amount =
SUMX(
Sales,
Sales[Sales Amount]
)
Any input would be appreciated.
Attached PowerBI file.
Thanks,
This one is a little tricky; You get different output because your rank "Rank by Category" by two-column instead of one (as in rest of your example). The second column which is involved in your calculation is this one you are using to sort 'Product'[Category]; Change this to the default and you get a "correct" rank.

Power bi dynamic ranking with filters and groupby counts

I have a table where I generate counts using COUNTROWS and GROUPBY. I do this because several columns are needed to identify a distinct item for counting. I have a DAX measure of these counts. Now I want to rank the counts with largest count being 1. Once I have the ranks I want to make a bar graph of counts on the x-axis of ranking. I want to be able to filter this graph with other columns from the table and have the ranks auto calculate with the new filters. Thanks in advance for the help.
Edit: to explain what im trying to do better I have the table below to get accurate counts of people i need to groupby first name and last name. Then i need to rank the counts in the example below matt smith would be rank 1 with 2 counts and everyone else would be rank 2. I then want to make a graph with rank as the x axis and counts as the values. I want to be able to dynamically filter the graph with year and grade.
Add a measure, in my case, Record Count:
Record Count =
VAR tbl = SUMMARIZE(T
, [First_Name]
, [Last_Name]
, "#Count"
, VAR fname = [First_Name]
VAR lname = [Last_Name]
RETURN
CALCULATE(COUNTROWS(T), 'T'[First_Name] = fname, 'T'[Last_Name] = lname )
)
RETURN
CALCULATE(SUMX(tbl, [#Count]))
Then use RANKX in another measure:
Ranking = CALCULATE(RANKX(ALL('T'[Last_Name], T[First_Name]), [Record Count], ,DESC,Dense))
No filters selected in slicer:
"c" selected in grade slicer:
Data Table

Calculating the percentage of grand total by category with DAX for one category only

I need help calculating the percentage of each category in a column (based on the grand total) in DAX but for one specific category.
This is how the data is structured. Each row is an individual transaction with an ID column and item column.
I need to get the % of transactions that are for bagels only. This is my sql code I currently use.
`Select 100 - CAST(count([Items])
- count(case [Items] when 'Bagel' then 1 else null end) AS FLOAT)
/ count([Items]) * 100 as Percent_Bagel
from Items_Table where Items != 'Coffee'
and Items != 'Muffin'`
I need this to be converted to a DAX formula to use in a Power BI measure if possible, but I am new to DAX and don't know where to begin.
Thank you.
The "right" implementation for you always depends on the context. You can achieve your goal through different approaches.
I have used the following:
Measure =
DIVIDE(
-- Numerator: Filter all Bagel's transaction and count them
CALCULATE(
COUNT('Table'[Transaction ID]),
FILTER('Table', 'Table'[Item] = "Bagel")
),
-- Denominator: Remove any filter - essentially fixing the full table - and count all transactions we have
CALCULATE(
COUNT('Table'[Transaction ID]),
ALL('Table'[Item])
),
-- If something goes wrong with the DIVIDE, go for 0
0
)
You may also use the filters to rule out all measures that are not blank.
Without measure filter
With measure filter (Other categories are gone)
Hope this is what you are looking for!

How to Rank a Measure which is Average Field, Power BI

I am new in Power bi. I have a table where I have agents details. I have their scores as well. I have date wise data into table where the following columns are available -
I have created created measure for Average of Score where Metric is UES AGGREGATE -
I have to get the ranking of the advocate(s) for the Average Score (UES AGG). I have tried this measure for Ranking calculation -
Rank_UES = RANKX('RankSummary',[RKS_UESAGG])
I am getting wrong ranking. Please help me , how to solve the issue.
Thanking You.
Use ALL function with RANKX.
Rank_UES =
RANKX (
ALL ( 'RankSummary'[AgentfullName] ),
[RKS_UESAGG]
)
I do not know if your [RKS_UESAGG] get you what you want. Suppose you want average sales you make something like this:
Rank_UES =
RANKX (
ALL ( 'RankSummary'[AgentfullName] ),
AVERAGE ( 'RankSummary'[Amount] )
)
https://learn.microsoft.com/en-us/dax/rankx-function-dax

RANKX function gives duplicates across all values it is ranking on

I have a Table with a measure calculating difference of a KPI across a unique combination of three categories. I am trying to rank the measure that is calculating the difference but I get repetitive ranks although the value being ranked is different.
Table:
What I tried:
Rank Measure =
CALCULATE(
RANKX(
ALLSELECTED(Dim106),
[Difference between spend in region and store],,
DESC
)
)
[Difference between spend in region and store] = StoreTurnover - RegionTurnover.
RegionTurnover = CALCULATE(SUM(Dim106[Turnover |EJR|]),ALL(FactStore[Store ID]),ALL(FactStore[SOE]),ALL(FactStore[SOM]),ALLSELECTED(FactStore[Region]),ALLSELECTED(FactStore[YearMonth]),ALL(Dim106[WGI]),all(Dim106[Item Family]),ALL(Dim106[Item Subgroup]),ALL(Dim106[WGI Desc]),ALL(Dim106[Item Subgroup Desc]),ALL(Dim106[Item Family Desc]),ALL(Dim106[UniqueKey]))
StoreTurnover = CALCULATE(SUM(Dim106[Turnover |EJR|]),ALLSELECTED(FactStore[SOM]),ALLSELECTED(FactStore[SOE]),ALLSELECTED(FactStore[Store ID]),ALLSELECTED(FactStore[YearMonth]),ALLSELECTED(Dim106[Store]),ALLSELECTED(Dim106[Month]),ALL(Dim106[WGI]),ALL(Dim106[Item Subgroup]),ALL(Dim106[Item Family]),ALL(Dim106[WGI Desc]),ALL(Dim106[Item Subgroup Desc]),ALL(Dim106[Item Family Desc]),ALL(Dim106[UniqueKey]))
I have a fact table which has a higher hierarchy of store and and month, it has a crossfilter both directions relationship
Richt click on your dataset fields New Column and use the following expression:
Rank Column = RANKX('YourTable';'YourTable'[YourColumn];;ASC)
UPDATE:
Maybe this suits your scenario:
RankingBySale = Rankx(All(Table1[SalesID], Calculate(Sum(Table1[SalesValue])), , Asc, Dense)