I would like display the Product Current Cost measure for each Store_id in each Store_city in a matrix table using Power BI.
Here is my DAX:
Product current cost (test) =
CALCULATE(
SUMX(
'Product Lookup',
'Product Lookup'[current_cost]
),
RELATEDTABLE(
'Store Lookup'
)
)
Output:
Field pane:
The Product Lookup table does not have relationship with Store Lookup and Sales by store tables. Thus, I am using RELATEDTABLE function.
I am expecting the current cost (from Product Lookup table) value should not be the same for each store_city in each store_id.
Anything wrong in my DAX?
Updated:
Product Lookup:
Store Lookup:
You can acheive result by following measure
Product current cost (test) =
SUMX(
RELATEDTABLE('Store Lookup'), 'Product Lookup'[current_cost]
)
Related
I am trying to create two columns for each product, one that reflects current year sales and another that shows prior year sales.
My underlying data already aggregates all sales by year. In this example I have three tables. One that contains the main sales. And the other is just a measure table where I store all my measures related to sales in Denver. So Denver Sales[Sales] is essentially just a measure which filters the 'Main Sales' table for Denver data only. This measure if defined as follows:
Sales = CALCULATE(SUM('Main Sales'[Sales]),
FILTER('Main Sales', 'Main Sales[City] = "Denver")
)
The third table is the Product table, which only contain two columns Product Name and Product ID. It has a relationship with Main Sales based on the Product ID.
So in order to get the PY sales, I tried to create a measure with the following DAX code:
PY Sales =
VAR py_sales =
CALCULATE(
'Denver Sales'[Sales],
FILTER ( ALLSELECTED('Main Sales'), 'Main Sales'[Year] = MAX ( 'Main Sales'[Year]) -1 )
)
RETURN
py_sales
However, as you can see below. The PY Sales column is taking the sum of all 3 products' PY sales for each year, rather than just the individual product.
Can anyone help me understand why my code is doing this?
I have 2 tables: dimProduct and factSales
Dim table has productid, name, category
Fact table has salesid, productid, status, amount, paiddate
I have a table visual that shows the status from the fact table. Against each status I want to show the count of products and the count of distinct category.
CountProducts=DISTINCTCOUNT(FACTSALES[PRODUCTID])
CountDistinctCategory=DISTINCTCOUNT(dimProduct[category])
How to correct this?
Set the cross-filter direction for your relationship to 'Both', so that filtering can also propagate from the fact table to the dimension table.
Alternatively, using DAX:
CountDistinctCategory =
CALCULATE(
DISTINCTCOUNT( dimProduct[category] ),
TREATAS(
VALUES( factSales[productid] ),
dimProduct[productid]
)
)
I have a situation below (Power BI - DAX) in which I am trying to SUMMARIZE a table called Product with a SUM aggregation to get the total cost of all products in each Category; but then I have to change the column name of one or two columns in the summarized result set.
I have written the below code to develop a calculated table:
ProductCategoryCostCT = SELECTCOLUMNS (
SUMMARIZE(
Product,
Product[Category],
"TotalCostOfAllProductsInThisCategory", SUM(Product[ProductCost])
),
"CategoryName", Product[Category],
"Cost", Product[TotalCostOfAllProductsInThisCategory]
)
The above code throws an error. Can someone help me correct this ? This may be pedestrian to many of you!
(The source Product table has ProductCost column at the individual Product level, with Category as another column
in the same table)
ProductCategoryCostCT = SUMMARIZE(
SELECTCOLUMNS(
Product,
"CategoryName", Product[Category],
"ProductCost", Product[ProductCost]
),
[CategoryName],
"Cost", SUM(Product[ProductCost])
)
I'm trying to calculate the value of item by multiple quantity by unit price from related table.
SALES REPORT
ITEMS
QTY
A
12
B
30
B
45
UNIT PRICE
ITEMS
PRICE
A
$5
B
$9
If you have a relationship set up between the Sales table and the Unit Price table, you can use the RELATED function to retrieve the price.
Add a DAX calculated column to the SALES REPORT table:
Value = Sales[QTY] * RELATED(Products[PRICE])
You can use LOOKUPVALUE to get the result from the other table if you don't have a relationship defined between the tables. As the link says, RELATED is more efficient.
Value = Sales[QTY] * LOOKUPVALUE(Products[PRICE],Products[ITEMS],Sales[ITEMS])
If your schema is like this
Use this measure
Measure =
SUMX ( RELATEDTABLE ( 'SALES REPORT' ), 'SALES REPORT'[QTY] )
* SUM ( 'UNIT PRICE'[PRICE] )
In this measure - I'm creating a temp table to group by customer ID and return the year of the min order_date for each customer. I then want to count the # of customers that show up for a given year (basically just a row count).
What I'm struggling to understand is - this formula doesn't seem to look at the SUMMARIZE table within it. If I put year and this measure in a matrix, it counts based on the raw table, not the grouped version built by SUMMARIZE in the formula. Any ideas why it's evaluating this way?
COUNTROWS(
SUMMARIZE(
Orders,
Orders[Customer ID],
"min_order_date_year",
MIN(Orders[Order Date].[Year])))
The formula is OK as per logic you provided to it. You have to clear about both your requirement and what your are writing in your code. Your formula is returning exactly what you are instructing it.
If I understand correct, you simply need the Customer count for the Minimum Year. For say if you have 6 unique customer for Year 2019 and 11 unique customer for Year 2020, you are looking here for value 6 to return by your measure.
Now, what your summarize table actually returning? if you create a separate custom table for the Summarize code only as below, you can see the table will actually hold all your customer name/id in first column and the second column will hole the MIN year available for that customer.
orders_summarize =
SUMMARIZE(
Orders,
Orders[customer id],
"min_order_date_year",MIN(Orders[Order Date].[Year])
)
So basically you have list of all customer in your summarize table. And now your are counting rows of your summarize table which is actually returning the total number of unique customers.
Finally, if you wants customer count for a specific Year (like MIN year), follow these below steps-
Step-1: Create a custom summarized table as below-
store_summarized_table =
SUMMARIZE(
store,
store[Customer ID],
"mindate",MIN(store[Order Date])
)
Step-2: create a measure as-
count_cust_id = COUNT('store_summarized_table'[Customer ID])
Step-3: Now configure your Matrix visuals as shown in the below image. You can also get the output in the image-
To avoid the Physical Table, you can do this below-
Step-1: Create a Custom Column as below-
is_min_year =
// -- keep current row's customer id to a variable
VAR current_cust_id = store[Customer ID]
// -- keep current row's YEAR value to a variable
VAR current_year = store[Order Date].[Year]
// -- find the MIN YEAR from order date for the current row customer id
VAR min_year_current_custommer_id =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
store,
store[Customer ID] = current_cust_id
)
)
// -- check the current row's year is the MIN year of order date for the customer as well or not.
RETURN IF(current_year = min_year_current_custommer_id, 1,0)
OR create a measure as-
is_min_year_measure =
VAR min_order_year_for_current_customer =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
ALL(store),
store[Customer ID] = MIN(store[Customer ID])
)
)
RETURN
IF ( MIN(store[Order Date].[Year]) = min_order_year_for_current_customer, 1,0)
Step-2: Create a Measure as below-
For created Custom Column
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
store[is_min_year] = 1
)
)
For Created Measure
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
[is_min_year_measure] = 1
)
)
Now add "Order Date" and measure "count_cust_for_min_year" to your Matrix. The out put will same as below-