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.
Related
I have below table structure:
enter image description here
here I want to put a date slicer in Power BI to filter on dates and return the count of total rows as total in the card as shown below:
enter image description here
simple, the only twist is that I want to have the total of hybrid car added at all times.
i.e.
Brands before 5/25/2020 = 4 Hybrid + 1 Electric = 5
Brands before 12/5/2020 = 4 Hybrid + 3 Electric = 7
I have found a solution, which is creating a view in my database, which jus holds the number count of hybrid car (select count(*) from table where cartype = 'hybrid') and using it to sum with filter rows in power bi - but I am looking for a solution completely in Power BI DAX query.
any measure I have tried to create in power bi is filtered by date slicer and so doesn't work.
Create these measure:
TOTALROWS = COUNT('cars'[brand])
ELECTRIC_NUM = CALCULATE([TotalRows],('cars'),'cars'[cartype]="ELECTRIC")
HYBRID_NUM = CALCULATE([TOTALROWS],ALL('cars'),'cars'[cartype]="HYBRID")
TOTALBYBUSINESSLOGIC = CALCULATE([ELECTRIC_NUM]+[HYBRID_NUM])
Now use the last measure (i.e. TOTALBYBUSINESSLOGIC) to be used in your Card to display the total, Notice the expression diffrence between ELECTRIC_NUM and HYBRID_NUM
(In HYBRID_NUM I have used ALL, All will have it bypass the Date Slicer filter) whereas ELECTRIC_NUM will only proivde sum of rows falling in the active date sliver range.
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 a requirement where i have to summarise the sales data by department and then create a calculated column to show the percentage of how much each department had contributed towards total sales.
Here % should be the calculated column in Direct Query Mode
Since you did not provide much information or what you have already tried, I can't really guess your model, but I hope this gives you a starting point.
Create a new measure:
(replace the table name where needed)
% Sales Sub Category = DIVIDE(
SUM('Sales'[Sales]),
CALCULATE(SUM('Sales'[Sales]), ALL('Sales'[Sub Category])
)
I am fairly new to PowerBI DAX and I want to filter out the top 20 product ids in a measure.
I came up with this formula but it does not seem to be working and I was hoping to get some help here.
$ Amount Parcel =
CALCULATE(
SUM(Data[$ Amount Parcel]),
FILTER (Data, NOT (Data[idProduct], SUM(Data[NetSales])) IN TOPN(20, SUMMARIZE(Data, Data[idProduct], "NetSales", SUM(Data[NetSales]))))
)
I want to show sales per PID for all products except for our 20 best sellers.
Thank you !!
I would suggest an easier approach adding a dimension column.
First of all, you need to have Product dimension table separated from Sales fact table. Make sure to create one-to-many relationship between Product and Sales with "Single" cross filter direction.
Then you can create a calculated column on Product table, which can be used to filter out top selling products.
Sales Rank = RANKX('Product', CALCULATE(SUM(Sales[SalesAmount])))
Now drag and drop Sales Rank field into the Filters pane of your visualization, and set the filter condition so that top selling products will not be shown.
I have four tables which I have tried related and unrelated:
Store (column "Store Number")
Calendar (column "Sales Date")
SKU (column "SKU Code")
Sales (columns "Store Number", "Sales Date",
"SKU Code" and "Sales Quantity")
I have slicers on the Calendar and SKU tables
I need to list all stores with total "Sales Quantity" for each store and at the same time to limit the sales quantity to the two slicers mentioned above. Basically, I need to list these columns:
Store Number - from the Store table (no filtering from Slicers)
Store Name - from the Store table (no filtering from Slicers)
Total Quantity of Sales for the Store - calculated measure filtered by Calendar and SKU slicers
So my question is, what DAX required to create the calculated measure?
Please note I must list ALL stores regardless of whether they have sales in the stipulated period.
I've tried various DAX functions such as TREATAS, SUMMARIZE, ETC.
I've tried with and without active relationships and with no relationships.
The closest I've got is the code below, but it excludes stores with zero sales. I need all stores regardless of their sales.
Qty by Store = CALCULATE(
sum(Sales[Sales Qty])
,USERELATIONSHIP(
Sales[Store Number]
,Store[Store Number]
)
)
The problem with the output I've managed is that stores without sales are excluded from the list. I need to have them included.
Keep the relationship active, and change the DAX formula to
Qty by Store =
VAR res = sum(Sales[Sales Quantity])
RETURN IF (ISBLANK(res), 0, res)
There is no need for USERELATIONSHIP(). Relationship Store - Sales is already active. The reason why the number of stores changes in the table visual is because when there is no sale for a particular store Qty by store measure returns BLANK and those BLANKs get filtered out by the table.
Result:
An easy way to make a blank return zero instead is to simply append +0 to your measure formula.
Qty by Store = SUM ( Sales[Sates Quantity] ) + 0
This works because DAX calculates BLANK() + 0 = 0.