We are trying to create a model in Excel/Power BI (using Power Query or Power Pivot or anything that would work) in order to classify a customer by its best product (based on a ranking system).
The first approach we applied was to count the customers per minimum ranking (or per best product brand). (inspired from the blog https://stackoverflow.com/questions/15742186/powerpivot-dax-dynamic-ranking-per-group-min-per-group)
Below the steps we did exactly:
- In PowerPivot Model, we created Classification and Customers table like in the example further below.
In the same model, we added a calculated column with the following formula to obtain the minimum rank per customer.
=MINX (
FILTER ( ALLSELECTED ( Customers ); [Customer_ID] = EARLIEST ( [Customer_ID] ) );
[Ranking]
)
Within a pivot table in Excel, we’ve put the calculated column in rows.
Then, we’ve used a Count distinct aggregation of the customers in the pivot table values.
This gave me the first desired result. (below example Pivot_Table.Selection1)
Now, the issue comes when we want to add more analysis axis.
For example, besides the product brand, we want to have the Product type in columns, and we want our measure to be recalculated every time I add/delete an axis.
In other words, we want to have a distinct customer count per best product and per Product Type.
In addition, we want the second attribute (axis) to be variable and the grouping or the distinct count per group to be dynamic.
Example:
Let’s suppose we have the tables Classification and Customers in our Model:
In the first approach we tried, we got the following table: Pivot_Table.Selection1:
Now when we add the analysis axis, we would like to have the following example: Pivot_Table.Selection2:
But we are having this:
As you can see, there should be one customer for the Group “Mercedes” and one for “Renault”, since depending on the product type, the top Truck for customer A is Renault and its top Car is “Mercedes”.
However, in the pivot table, the Mercedes group is shown as Truck (which doesn’t even exist in our dataset).
Edit
I'm open for any suggestion, not only Power Pivot, but also Power Query (M functions) or Power BI or whatever could work.
Finally I think I understood your problem, a customer can have different Product_Brand values, you want to count only those Product_Brand which its ranking is the minimum.
In that case, this is a possible solution:
Create a calculated column called Minimum Rank in the Customer table.
=
CALCULATE (
MIN ( [Ranking] );
FILTER ( Customer; [Customer_ID] = EARLIER ( Customer[Customer_ID] ) )
)
Then create a measure, lets say Customer ID Distinct Count to count those rows where the Rank is equal to the minimum for that customer.
Customer ID Distinct Count :=
CALCULATE (
DISTINCTCOUNT ( Customer[Customer_ID] );
FILTER ( Customer; [Ranking] = [Minimum Rank] )
)
You will get something like this:
Related
I have a table that just contains all articles.
Then I have a transactional table that contains a value that I would like to use for the rank. THe same tables has the department information.
The structure is like:
Now I would like to get the Ranking based in Value, in the end represented in a matrix.
This function shows me the ranking based on the value:
RANKX(ALLSELECTED(Article),[Value])
If the matrix just contains Article in the rows, then it works.
But when I add department, then the Rank is calculated for each department.
How can I get the RANK for each Article regardles of the Department?
I'm assuming that you are creating a measure and that you are putting the results in a "table", rather than a "matrix". If so, then the following DAX works for me.
Rank = RANKX(ALLSELECTED(Table1), Calculate(Max(Table1[Value])))
Note that this is when all of the data comes from a single "Table1". You may need to make updates to the measure above based on your table structure and names.
I have loaded 2 tables into Power Pivot which are related by a key. One table contains sales for a product and the other contains it's cost. What I am trying to do is multiply the total sales by the cost of that item. How do I write a calculated measure in a pivot table that would calculate this?
= SUM(ProductSales[Sales])*RELATED(MarketValue[Value])
The error I am getting is that This formula is invalid or incomplete: 'The column 'MarketValue[Value]' either doesn't exist or doesn't have a relationship to any table available in the current context.'
I understand that this is wrong but i'm not sure how to modify it to suit my needs. I tried using SUM in front of the related function but it wouldn't accept the related function.
Thanks in advance.
If there is no relationship between the tables, you should have a common field at least, i.e. ProductSales[ProductID] and MarketValue[ProductID]. Using the common field you can join both tables using FILTER function.
=SUMX (
ProductSales,
ProductSales[Sales]
* CALCULATE (
VALUES ( MarketValue[Value] ),
FILTER ( MarketValue, MarketValue[ProductID] = ProductSales[ProductID] )
)
)
UPDATE: If you managed to create the relationship in your model, you have to create three measures:
Cost measure in the MarketValue.
= SUM(MarketValue[Value])
Sales measure in the ProductSales
=SUM(ProductSales[Sales])
Then just use the two previous measures.
= [Sales]*[Cost]
Let me know if this helps.
I am Counting on Distinct ID's in a column - this is leading to the sum of the subtotals not equalling the grand total as follows:
What I want to do is rank the Payment Dates in cronological order and select ONLY the highest date to display. In the example above the Grand Total won't change, but the Townville row will not show a Distinct Student Count.
This is a very specific requirement and I'm assuming there's an easy way to do it in DAX - I've tried playing around with both RANKX and MAX but am no closer to solving this.
One last thing - the Rank must be contextual to the Time Filter selected by the user (so if they select 2015 it'd give the second record Rank 1 and the top record wouldn't show. If they select May 2015 it'd give the top record Rank 1 and the second record wouldn't show)
I think this is what you are looking for - I added a calculated column to the PowerPivot model that provides a rank based on the Last Payment Date and the Name of the Student. It will rank the earliest payment for any student as a 1.
The code for the column is as follows:
=RANKX(FILTER(Table1, [Student Name] = EARLIER([Student Name])), [Last Payment Date])
... assuming your table is named "Table1"!
The FILTER is the key that limits the ranking to dates belonging to students with that name only.
Update for Multiple tables
To set up relationships between the tables, go to the "Diagram View" of the model, available in the Home tab of the Power Pivot window.
You can drag fields from one table to the other to create relationships. This will only work if at least one of the fields is unique - it's a good idea to think of the model as a dimensional model, with a tables that acts like a fact and other tables around it that act like dimensions.
From the comment, I would try to get the Payments to act like the fact, and have it link to the Community and Student tables. in this case, you could then have the following code:
=RANKX(FILTER(Table1, Related('Students'[Student Name]) = EARLIER('Students'[Student Name])), [Last Payment Date])
This calculated column would be on your Payments Fact table, and it uses a lookup to a related field.
Note that in this specific case, it would be easier to just run the filter over your Student ID field that is used to lookup the Student name.
I'm not overly experienced with DAX, and my boss has asked me for some metrics which seem past my capability. Specifically, she wants to know in what percentage of the stores any item is being sold at.
We have a Stores table which is in a one to many relationship with the measures table relating by [STORE_ID]. The items table is also related 1-to-M to the measures table by [ITEM_ID]. the measures table contains which indicates among other things, the weekly [Sold] of an item.
My current logic has been to separate this problem into two more simple parts, (stores selling product)/ (total stores). finding the total stores which is an easy distinctcount in the stores table, the The next is more difficult. I tried Stores_Selling_Product = countrows(filter(filter(Measures, earlier(measures[ITEM_ID]) = measures[ITEM_ID]), EARLIER(Measures[STORE_ID]) <> Measures[STORE_ID])), but I found that only excluded the stores that matched the current store ID. Is there a way to exclude stores that have already been counted?
if it helps there is also a binary cell [SoldInStore] which is 1 if the item is sold in that store else 0. I'm pretty certain I'm going to have to use something other than countrows, but I don't know where to look. Any advice would super nice. Thanks in advance,
-Mudkip.
AllStores:=
COUNTROWS( ALL( 'Stores' ) )
StoresWithSales:=
COUNTROWS(
CALCULATETABLE(
'Stores'
,'Measures'
)
)
%StoresSelling:=
DIVIDE( [StoresWithSales], [AllStores] )
The first measure will always return the total number of stores in the 'Stores' table, regardless of any filter context from the pivot table.
The second utilizes the magic of cross table filtering to filter 'Stores' to only those rows that have at least one corresponding row in 'Measures' based on whatever filter context currently exist on 'Measures'. Think of it as essentially the same as the following in SQL:
SELECT
COUNT(1)
FROM Stores
WHERE EXISTS (SELECT
COUNT(1)
FROM Measures
WHERE Measures.StoreKey = Stores.StoreKey
AND <whatever filter context exists on Measures>
);
I am using Excel Powerpivot with data in two separate tables. Table 1 has ITEM level data with a BRAND characteristic. Table 2 has BRAND level data. The two tables are linked by the BRAND key. The measure I am using is non addable. i.e. the sum of the ITEMS does not equal the BRAND. The pivot is set up with ITEMS nested under BRANDS in the rows and the Measure in the column.
Excel assumes that I want to summarize ITEM to a BRAND level by applying SUM, MAX, MIN, AVG, etc. I would like to return the actual values from the appropriate ITEM or BRAND level table and not apply any calculations to the values. Is this possible?
If what you are effectively trying to do is produce a different result for the Brand rows (e.g. blank()) then the answer is to write a further measure that does a logic check to determine whether or not the row in question is an ITEM or a BRAND.
= IF (HASONEVALUE(table1[Item]), [Measure], Blank() )
Bear in mind that this will work for your current pivot but may not be adaptable to all pivots.
This assumes that you have explicitly created a measure called [Measure] and you are not just dragging the numeric column into the values box. If not you can create the initial [Measure] something like this:
= Sum(table1[Value])
Where Value is the column you want to use in the measure. Although you have used a sum, if it relates to a single item which has a single row in the table it will give the desired result.