Calculating Percent of Total in a Hierarchy - powerbi

Using the Contoso database, I have a report that looks as follows:
The measure for Percent of Total is:
Percent of Total =
VAR SelectedSalesTotal = CALCULATE(
[Sales Amount],
ALLSELECTED(Products[Brand Name]),
ALLSELECTED(Customers[Country])
)
RETURN
DIVIDE([Sales Amount], SelectedSalesTotal)
Now I want to change this so that the percentage for each country is not the percentage based on total sales, but based on brand name sales. So, for example, for the brand A. Datum and Canada, instead of 4.48%, it should be 26.96% (7,011,941.77 / 26,007,534.55). The Percent of Total values for each brand shouldn't change. So, A. Datum should remain at 16.62%.
I have been unable to figure out how to do this.
The data model looks like this:

Related

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?

Using RANKX with a YTD Measure does not rank correctly

I am trying to calculate the Cumulative Purchases by YTD. The first step is to rank the items by Cost Amount, but when I try to rank by the [_YTD Cost] measure, the numbers I get do not make sense (skipped numbers, duplicated).
[]
I had 3 slicers: Month, Year and to select Month/YTD measures. Since with the Month calculation I have no problems, I removed the interaction with the Month/YTD slicer and I placed only YTD measures on the table:
Total Purchase Cost = SUM ( Purchases[Amount] )
_YTD Cost = TOTALYTD([Total Purchase Cost], 'dim-calendar'[Date])
_RANK YTD = RANKX(ALLSELECTED(Purchases), [_YTD Cost])
Notes:
I pulled the item from the Item table
The Purchase table is linked to the Date table by Purchase Date
Not 100 % sure on your data model, but try changing your RANKX(ALLSELECTED(***) to reference the Item-column. Like this:
RANKX(ALLSELECTED('Item'[Item]), [_YTD Cost])

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.

Count of products where sum of sales for particular product is higher than 2% of total sales - Power BI - DAX Measure

I have facts tables with sales
I would like to create measure that counts number of products where sum of sales for particular product is higher than 2% of total sales.
For example:
1. sum of sales for 'ProductKey' 310 is 5000.
2. sum of sales for 'ProductKey' 346 is 2000.
3. 2% of sum of total sales is 3000.
Product 310 would be included in count product 346 wouldn't be included in count.
How would I write such measure?
I've tried to created something like this:
Big Sales =
var SalesTotal = CALCULATE(SUM(fact_InternetSales[SalesAmount]))
var twoperceSalesAmount =
CALCULATE (SUM(fact_InternetSales[SalesAmount]), ALL( fact_InternetSales )) * 0.02
return
CALCULATE (COUNT(fact_InternetSales[ProductKey]),
FILTER (fact_InternetSales, SalesTotal - twoperceSalesAmount > 0))
Thansk
Kind regards,
Robert
You were almost there.
The pseudoalgorithm is:
1. Calculate threshold
2. Consider only product with sales above threshold
3. Count number of products
Big Sales:=
var _threshold = CALCULATE(SUM(fact_InternetSales[SalesAmount]),ALL(fact_InternetSales))*0.02
var _productlist =
FILTER(ADDCOLUMNS(
SUMMARIZE(
fact_InternetSales,
fact_InternetSales[ProductKey),
"productsales",CALCULATE(SUM(fact_InternetSales[SalesAmount]))),
[productsales]>_threshold)
RETURN
Countrows(_productlist)
If it is very slow web can optimize it.
Also solution with SUMX works:
Big Sales SUMX =
SUMX(VALUES(fact_InternetSales[ProductKey]),
IF(CALCULATE(SUM(fact_InternetSales[SalesAmount]))>
CALCULATE(SUM(fact_InternetSales[SalesAmount]),ALL(fact_InternetSales))*0.02,1))

CALCULATE within the lower granularity

I have a matrix that is using the fields "Product Group" and "Product" in rows.
I want to calculate the average price for the products that had sales last year. Sales LY is a calculated measure:
Sales LY = CALCULATE(SUM('Table'[Qty]);SAMEPERIODLASTYEAR('Calendar'[Date]))
And this is the measure I'm trying to find:
AvgPrice= CALCULATE(DIVIDE([Turnover];[Quantity]);FILTER('Table'; [Sales LY]>0))
This works fine on the lower granularity (Product), but on the higher granularity (Product group) the calculation is wrong because the Product group is adding ALL the products within that Product group and I only want the ones that had sales last year.
How do I tell DAX: "Use the lower granularity"?
One option for situations like this is to make the higher granularities a sum of the lowest granularity values. Something like this:
GroupAvgPrice = CALCULATE( SUMX( VALUES( 'Table'[Product] ), [AvgPrice] ) )
When there is only one product in the evaluation context, this simply reduces to [AvgPrice], but should work at higher granularities as well.