DAX - Rank by product category - powerbi

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.

Related

RANKX without creating a summary table

I'm a newbie in DAX and I'm trying to create a visual in Power BI that shows the top 10 customers by sales.
I tried to create a calculated column in the 'Food sales' table, but I couldn't figure out how to create the rank by client ID.
I ended up creating a summary table in the model:
Consolidation Ventes =
SUMMARIZE('Food Sales',
'Food Sales'[ID Client],
"Ventes",SUMX('Food Sales', 'Food Sales'[Quantité]*'Food Sales'[Prix de vente])
)
and adding the rank calculated column like that:
Rank = RANKX('Consolidation Ventes', 'Consolidation Ventes'[Ventes],,DESC,Dense)
I obtained what I was looking for:
But I'm pretty sure there's a better way to do it, without creating an extra table in the model.
I tried creating a measure instead, but it always returned "1" and I couldn't understand why...
Is there a better way to achieve the same result?
Is there a way to create a measure instead of a calculated column, to create dynamic ranks in order to slice the with more than one column (like for example, customer name and product type)?
I hope this helps:
#Orders := COUNTROWS(Sales)
SalesAmount := SUMX(Sales,Sales[Quantity]*Sales[Price])
TopN :=
COUNTROWS(INTERSECT(
CALCULATETABLE(
TOPN(5,VALUES(Customers[Name]),[SalesAmount],DESC),
ALL(Customers)
),
CALCULATETABLE(VALUES(Customers[Name]))
))
You add the TopN measure to "Filters on this visual" and define the filter condition to "TopN is 1".

How do I manipulate measure values based on 2 other dimension tables

Power BI newbie here and I'm trying to figure how to craft my DAX to manipulate my measure values based on certain criteria in the other two tables.
Currently I have 2 separate tables which are joined by a One to Many relationship and a separate Measures table. (Total Sales Price is computed as sum of Sales Price)
My aim is to create a new measure where Total Sales Price is multiplied by 1.5x when DIM_Product_Type[Product Category] = "High".
New Measure =
CALCULATE (
SUM ( FACT_PriceDetails[Sales Price] ),
FILTER ( DIM_Product_Type, DIM_Product_Type[Product Category] = "High" )
) * 1.5
However this returns no values in my visual and I'm trying to discern if its a matter of the table joins or the DAX expressions.
Thank you for your time!
Your measure seems good.
It will select only those products with a Product Category of "High" and multiply them by 1.5 to give you result. i.e. Give me the sum of all "High" Product category Price details multiplied by 1.5.
What you need to check is:
Product Serial Numbers match across the two tables
Your Product Category does indeed contain the category "High"
You have entries in FACT_PriceDetails that link to a DIM_Product_Type that has a category of "High"
Check you have not set any filters that could be hijacking your results (e.g. excluding the "High" product category product type or the realated fact/s)
Option-1
You can do some Transformation in Power Query Editor to create a new column new sales price with applying conditions as stated below-
First, Merge you Dim and Fact table and bring the Product Category value to your Fact table as below-
You have Product Category value in each row after expanding the Table after merge. Now create a custom column as shown below-
Finally, you can go to your report and create your Total Sales measure using the new column new sales price
Option-2
You can also archive the same using DAX as stated below-
First, create a Custom Column as below-
sales amount new =
if(
RELATED(dim_product_type[product category]) = "High",
fact_pricedetails[sales price] * 1.5,
fact_pricedetails[sales price]
)
Now create your Total Sales Amount measure as below-
total_sales_amount = SUM(fact_pricedetails[sales amount new])
For both above case, you will get the same output.

DAX Calculate Sum of sales per productid filter by productid ( NOT IN TOP 20 )

I am fairly new to PowerBI DAX and I want to filter out the top 20 product ids in a measure.
I came up with this formula but it does not seem to be working and I was hoping to get some help here.
$ Amount Parcel =
CALCULATE(
SUM(Data[$ Amount Parcel]),
FILTER (Data, NOT (Data[idProduct], SUM(Data[NetSales])) IN TOPN(20, SUMMARIZE(Data, Data[idProduct], "NetSales", SUM(Data[NetSales]))))
)
I want to show sales per PID for all products except for our 20 best sellers.
Thank you !!
I would suggest an easier approach adding a dimension column.
First of all, you need to have Product dimension table separated from Sales fact table. Make sure to create one-to-many relationship between Product and Sales with "Single" cross filter direction.
Then you can create a calculated column on Product table, which can be used to filter out top selling products.
Sales Rank = RANKX('Product', CALCULATE(SUM(Sales[SalesAmount])))
Now drag and drop Sales Rank field into the Filters pane of your visualization, and set the filter condition so that top selling products will not be shown.

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)

Power BI Rank within Matrix Visual

I've spent many weeks trying to get this to work including a posting at another site. No luck. Not sure what I'm missing.
The following code gets me the table of data that I need. However, I want just the "rank" value and I want it for each row within a matrix visual.
First some background, the PowerBI page has slicers on dates and "base" product. Any given product may be sold in multiple countries. All carry the same base product suffix with a country-specific prefix.
The matrix in question displays several aspects of all the product variants for the selected base product. I want to show where each variant ranks (unit sales) within its country.
VAR tblCountrySales =
SUMMARIZE (
FILTER(
tblSalesUnits,
RELATED(tblProducts[Country])="US"
),
tblProducts[ProdID],
"Total Sales", SUM ( tblSalesUnits[Units] )
)
RETURN
ADDCOLUMNS (
tblCountrySales,
"ProductRank", RANKX ( tblCountrySales, [Total Sales] )
)
Is there a way to either pull just the rank for a single product from the above code, or, better yet, a DAX pattern that will get me the rank by unit sales of each product variant within its respective country?
I've use these statements to successfully get the product ID and country for each row within the visual.
VAR ProdSales = SUM(tblSales[Units])
VAR ProdCountry = SELECTEDVALUE(tblProducts[Country])
Any help is greatly appreciated. I assume I'm missing something with row context within the visual as a simple COUNTROWS on the table expressions above always returns "1". Being new to PowerBI and DAX I'm still trying to grasp the nuances of filter and row contexts.
The products table contains data like...
ProdID.....BaseID....Name...Country
7190...........7190.....xxxx.....US
150207190......7190.....XXXX....Panama
241807190......7190.....xxxx.....Spain
The sales table contains data like...
ProdID......SalesMonth......Units.....USD
7190........July 2010.......4563....$23491.00
150207190...July 2010.......2543....$16235.00
241807190...July 2010.......1263....$8125.00
There is a dates table as well that links the SalesMonth to the usual selection of date display formats. The two tables above are linked via ProdID, though the visuals are sliced by the BaseID. I'd attach a picture, but don't have permissions to do so.
With some more tinkering I found this solution.
Country Rank =
VAR ProdSales = SUM(tblSales[Units])
VAR ProdCountry = SELECTEDVALUE(tblProducts[Country])
VAR tblCountrySales =
SUMMARIZE (
FILTER(
ALLEXCEPT(tblProducts,tblProducts[BaseID]),
tblProducts[Country]=ProdCountry
),
tblProducts[ProdID],
"Total Sales", SUM ( tblSales[Units] )
)
RETURN
IF(ProdSales>0,COUNTROWS(FILTER(tblCountrySales,[Total Sales]>ProdSales))+1,BLANK())