RANKX function gives duplicates across all values it is ranking on - powerbi

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)

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

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.

Ranking a Measure Value (as opposed to a column value) in DAX?

Short version of question: how can I create a rank based on a calculated measure?
Question background:
I have a table/query that yields many rows for each employee, with each row having a time taken value.
I'd like to calculate the average time taken for each employee, and then present a table with a row for each employee, showing their name, average time taken, and rank (based on time taken).
To do the first part, I created a measure called AverageTimeLength and set it equal to:
AverageTimeLength = Average(Table_name[Column_name])
Then I coded the following:
AverageTimeLength_employee = CALCULATE([AverageTimeLength], GROUPBY(Table_name, Table_name[EmployeeName]))
These two are measures; I was able to insert the second into the new table chart I'm creating, but unfortunately, I can't use RANKX() on it because the measure values don't come from a column. If I try to create a column derived from the measure (i.e., column_name = [AverageTimeLength_employee]) I just get an error accusing column_name of circular reasoning.
What I want to do seems like it should be simple; does anyone know how I can create a simple rank parameter, to rank the measure values?
You can create the Average measure and use it in the Rank measure as follows:
Average = AVERAGE([Time taken])
Rank = IF (
HASONEVALUE ( Table_Name[Name] ),
RANKX ( ALL ( Table_Name[Name] ), [Average])
)
Hope it helps.

Combine real and planned data in PowerBI KPI visualizations

I've got two primary datasets:
Real data with all sales transactions.
For example RealData:
Date;Sales
16-01-2017;1200
20-01-2017;1500
05-02-2017;800
08-02-2017;1100
13-02-2017;300
Etc.
A plan with total sales I want to achieve, with totals at the last day of each month.
For example EndOfMonthTargets:
Date;Sales
31-01-2017;3000
28-02-2017;2500
Etc.
In my real data I (a) also have data from the years before 2017, and obviously I have the targets for each month in 2017. The RealData is always live / up to date.
What I'm trying to do is show KPI visualizations: how is the real data vs the plan doing. However, I can't figure out how to relate the two datasets, because the cannot really be joined together.
What is the idiomatic way to do this in PowerBI?
I've tried:
adding a column to each source with its respective status ("real" vs "plan")
creating a union (i.e. "Append Queries as New")
adding a column Month = MONTH([Date])
Giving me this data:
But that's weird, because then the KPI visualization will include the "Plan" data either at the start or worse at the end where it will be plotted as the "current" number.
The expected output is a KPI visualization:
showing the absolute number of sales summed by year (or month, if my slicers are set that way!)
with the Target goals retrieved from the plan data
with a trend based on previous years (not included in my sample data)
How do I achieve that?
There are a few ways to join tables like this. What you have are 2 tables with different granularities: a sales table that goes to the day, and a target table that goes down to the month. It's a common request to model them against each other, and it boils down to having a lookup table that both fact tables can join with.
What I would recommend:
Have a table of your real data
Have a table of your plan/target data, using the last day of the month is fine
Do not relate these 2 fact tables together directly
Rather, have a separate date lookup table, to which both tables are joined. This date dimension should, at a minimum, have a month & year column (e.g. March 2017).
Join both fact tables to your date table
Hide the date fields in your 2 fact tables, so that you aren't tempted to use them in visuals (you have to use the date fields from the lookup table instead)
You can then create measures to SUM your actual and targets. And then additional measures that subtract those 2 measures against each other (or divide one into the other). All those measures will then be sliceable by the month & year on your date table. (You could slice them by date too, but because targets are assigned to the last day of the month, that granularity will be less than helpful.)
There's a lot of good information & examples here: http://www.daxpatterns.com/handling-different-granularities/
The way I would do it:
Add a calculated column to both real and targets tables:
Month = Date(Actual[Date].[Year],Actual[Date].[MonthNo],1)
Month = Date(Target[Date].[Year],Target[Date].[MonthNo],1)
Create relashionships between these
Plot SUM(RealData[Sales]) and Target[Sales] against Target[Month]
You can use PowerQuery to transform data.
"Real" table:
let
Source = Table.FromRecords({[Date="16.01.2017", Sales = 1200], [Date="20.01.2017", Sales=1500], [Date="05.02.2017", Sales = 800], [Date="08.02.2017", Sales = 1100], [Date="13.02.2017", Sales = 300], [Date="10.01.2016", Sales = 1400], [Date="02.02.2016", Sales = 1800]}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Sales", Int64.Type}})
in
#"Changed Type"
"Plan" table:
let
Source = Table.FromRecords({[Date="31.01.2017", Sales = 3000], [Date="28.02.2017", Sales=2500], [Date="31.01.2016", Sales = 2800], [Date="29.02.2016", Sales = 2700]}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Sales", Int64.Type}})
in
#"Changed Type"
Your query for getting data, let's name it GetData:
let
t1 = Table.AddColumn(Real, "Source", each "Real"),
t2 = Table.AddColumn(Plan, "Source", each "Plan"),
MergedTable = Table.Combine({t1, t2}),
AddYear = Table.AddColumn(MergedTable, "Year", each Date.Year([Date])),
AddMonth = Table.AddColumn(AddYear, "Month", each Date.Month([Date])),
Group = Table.Group(AddMonth, {"Year", "Month", "Source"}, {{"Sales", each List.Sum([Sales]), type number}})
in
Group
You may avoid using last step by grouping results at report level, but this will result in users being able to view amount of each sale. If this is expected behavior, just remove Group step, or replace it with Table.RemoveColumn to remove [Date].
If you need separate columns for Plan and Real values, then pivot Source column.
Then just use GetData query in clustered column chart.
You will have to create a measure to calculate the Actual total and measure to calculate the Planned by each month or end of month. Then create a measure to compare the difference between Planned total and Actual total.
Actual =
CALCULATE (
SUM ( RealDate[Sales] ),
FILTER (
ALL ( RealDate ),
MONTH ( [Date] ) = MONTH ( MAX ( EndOfMonthTargets[Date] ) )
&& YEAR ( [Date] ) = YEAR ( MAX ( EndOfMonthTargets[Date] ) )
)
)
Planned = SUM(EndOfMonthTargets[Sales])
ActualVSPlanned = [Actual] - [Planned]
Now use that measures and the Date in eEndOfMonthTargets table to build a matrix:
For the ActualVSPlanned measure use conditional formating as follows:
You will get something like this: