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.
Related
I have a measure that is comprised of other measures. I will post below pictures. I am calculating the forecast for the month per location. I created a measure for that and the total is correct for that. I created another measure that calculates the working days in a month and when I divide by this measure, I get the correct results per the location but the total is wrong.
This measure shows the measure I am using to calculate the total forecast for the month per location.
"MPP = (CALCULATE(SUM('TF'[ Forecast ]), DATESBETWEEN ('TF'[Date],[Month Start-pp], [Month end_pp] )))"
This measure shows the measure I am using to calculate the working days in a month.
"Days per location =
CALCULATE (
COUNTROWS ( 'TF' ),
FILTER (
'TF',
'TF'[Date].[MonthNo] = MONTH ( TODAY () )
)
)"
The pic is showing the totaling. The first column is the measure without the division and is correct totaling. The second column is where the issue is.
I am brand new to Power BI, and I could use a bit of help. I have this sample data:
I then want to get the [Average Total Billed Per Profession], which I got using:
Then, ideally, I would [Total Billed]/[Average Total Billed Per Profession]. But when I drop it in a table, it breaks. On the left, are the values I would want in the calculation. On the right, the values I get:
How would I set up the calculation correctly? Any help is appreciated. Thanks.
To have the average per profession in a table where also the Name is present, it's necessary to remove the filter context over Name, so a possible solution is
Average Total Bille Per Profession =
CALCULATE(
AVERAGEX(
SUMMARIZE( StaticData, StaticData[Profession], StaticData[Name] ),
CALCULATE( SUM( StaticData[Price] ) )
),
ALLEXCEPT( StaticData, StaticData[Profession] )
)
The CALCULATE inside AVERAGEX is required to trigger a context transition to transform the row context on the StaticData columns to a the corresponding filter context, that's needed to filer the StaticData[PriceColumn] rows to be used in the SUM.
It looks like profession + name doesn't make each row unique. If you just want "Average Total Billed Per Profession", then you can use something like the following.
Average Total Bille Per Profession =
AVERAGEX(
VALUES( StaticData[Profession] ),
SUM( StaticData[Price] )
)
It's just making sure you're considering unique profession for the metric you're calculating.
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.
I need help calculating the percentage of each category in a column (based on the grand total) in DAX but for one specific category.
This is how the data is structured. Each row is an individual transaction with an ID column and item column.
I need to get the % of transactions that are for bagels only. This is my sql code I currently use.
`Select 100 - CAST(count([Items])
- count(case [Items] when 'Bagel' then 1 else null end) AS FLOAT)
/ count([Items]) * 100 as Percent_Bagel
from Items_Table where Items != 'Coffee'
and Items != 'Muffin'`
I need this to be converted to a DAX formula to use in a Power BI measure if possible, but I am new to DAX and don't know where to begin.
Thank you.
The "right" implementation for you always depends on the context. You can achieve your goal through different approaches.
I have used the following:
Measure =
DIVIDE(
-- Numerator: Filter all Bagel's transaction and count them
CALCULATE(
COUNT('Table'[Transaction ID]),
FILTER('Table', 'Table'[Item] = "Bagel")
),
-- Denominator: Remove any filter - essentially fixing the full table - and count all transactions we have
CALCULATE(
COUNT('Table'[Transaction ID]),
ALL('Table'[Item])
),
-- If something goes wrong with the DIVIDE, go for 0
0
)
You may also use the filters to rule out all measures that are not blank.
Without measure filter
With measure filter (Other categories are gone)
Hope this is what you are looking for!
I have two tables, one has a column gross profit and the other has total sales and category. I want to divide gross profit by total sales where category = SGA.
How do I get there?
Create measure:
Gross Margin =
CALCULATE (
DIVIDE ( SUM ( Table1[Gross Profit] ), SUM ( Table2[Total Sales] ) ),
Table2[Category] = "SGA"
)
Note: it will only work if you slice it by shared dimensions. If you run into issues, post your data model snapshot - DAX formulas are highly dependent on the model relations.