Using related in DAX calculated measures - excel

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.

Related

DAX Measure - Specific Ranking using RANKX

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.

How to use the unique items from in a column from several tables as the row criteria of a pivot table in excel data model using Dax?

I have 3 different tables with the customer name and there are duplicates as well as unique customers in the 3 tables and I need to get the unique for all 3 to be used as the rows criteria in the pivot table.
I've been finding a way to do so but I cannot seem to figure it out.
The measure I tried is: Customers:=DISTINCT(UNION(VALUES('Test1 - Invoice'[CustomerID]),VALUES('Test2 - Invoice'[CustomerID]),VALUES('Test3 - Invoice'[CustomerID])))
But I get the error below:
Semantic Error: Too many arguments were passed to the VALUES function.
The maximum argument count for the function is 1.
I am quite new to DAX and have no idea how to do it. I believe it is because measures are only for values if i'm not mistaken
I read that to place on other fields of the pivot table, it has to be a calculated column although I do not see how it can be a calculated column as well.
One approach is to create a separate table to store the Customer Name dimension - then create relationships between that Customer dimension table and your 3 fact tables. This would be most effective at the Power Query stage, but can be done using DAX.
An alternative is to merge your 3 fact tables - again, this would be best done with Power Query, but is possible with DAX.

Dynamic Grouping in Power BI/PowerPivot Model based on a calculated field

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:

Avoid DISTINCTCOUNT in PowerPivot

Due to performance issues I need to remove a few distinct counts on my DAX. However, I have a particular scenario and I can't figure out how to do it.
As example, let's say one or more restaurants can be hired at one or more feasts and prepare one or more menus (see data below).
I want a PowerPivot table that shows in how many feasts each restaurant was present (see table below). I achieved this by using distinctcount.
Why not precalculating this on Power Query? The real data I have is a bit more complex (more ID columns) and in order to be able to pivot the data I would have to calculate thousands of possible combinations.
I tried adding to my model a Feast dimensional table (on the example this would only be 1 column of 2 rows). I was hoping to use that relationship to be able to make a straight count, but I haven't been able to come up with the right DAX to do so.
You could use COUNTROWS() combined with VALUES().
Specifically, COUNTROWS() will give you the count of rows in a table. That means COUNTROWS is expecting a table is input. Here's the magic part: VALUES() will return a table as results, and the table it returns are the distinct values in the table/column that you provide as the argument for VALUES().
I'm not sure if I'm explaining it well, so for the sample data you provided, the measure would look like this (assuming the table is named Table1):
Unique Feasts:=COUNTROWS(VALUES('Table1'[Feast Id]))
You can then create a pivot table from Powerpivot, and drag Restaurant Id into Rows, and drag the measure above into Values. Same result as DISTINCTCOUNT, but with less performance overhead (I think).

how to calculate unique stores selling products in powerpivot

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>
);

Resources