Power BI: calculate value using data from related table - powerbi

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

Related

Find number of orders for customer first month

I am new to Power BI and learning how to perform cohort analysis with DAX in Power BI. Is there any way to find out how many times (or if easier if a customer buys more than a certain threshold) their first month?
If I have the table Customers:
ID | DateOfFirstRegistration
And the table of orders:
ID | customerId | orderDate
Let me know if any more information is helpful!
EDIT: If possible, is it also possible to plot it in a matrix with customers in each row and month 1 through 3 in the columns?
Thank you
You can do something like this measure in a table visualization together with customer ID:
First Month Purchases =
VAR _registration =
SELECTEDVALUE ( 'customers'[DateOfFirstRegistration] )
RETURN
CALCULATE (
COUNTROWS ( 'orders' ) ,
DATESINPERIOD (
'orders'[orderDate] ,
_registration ,
1 ,
MONTH
)
)

Measure returning same value for all columns in Matrix visual

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?

Sumx and Relatedtable in Power BI

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

PowerBI new column calculation based on columns from different tables

I am trying to calculate 'TimeSheet Team PV'[Time (h)] * 'Position History'[Salary]
Data model:
TimeSheet Team PV" table:
"Position History" table:
In the Position History table, I have some employees which were promoted and appear as having different periods during different periods in the company, with different salary. Can I join somehow these values based on Start Date, End Date (Position History table) and Work Date (TimeSheet Team PV tables) + Name?
I would like to generate new column in the TimeSheet Team PV table, with the calculation above.
You'll need to write some logic to make sure it uses the Work Date column. Something like this:
Cost =
VAR WorkDate = 'TimeSheet Team PV'[Work Date]
RETURN
'TimeSheet Team PV'[Time (h)]
* CALCULATE (
SELECTEDVALUE ( 'Position History'[Salary] ),
'Position History'[Start Date] <= WorkDate,
'Position History'[End Date] >= WorkDate
)

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.