measure with limited hierarchy? - powerbi

I need to create a table which has invoice level information (invoiceno, customername, product, value, cost). In the same table i need to calculate the weighted average value/unit for each product but it needs to be at product level not at invoice customer level.
So far i have this measures :
SumQty =
var var1 = 'CustomerProfitability_PowerBi (3)'[StartDate]
var var2 = 'CustomerProfitability_PowerBi (3)'[EndDate]
return
sumx(summarize((calculatetable('CustomerProfitability_PowerBi (3)','CustomerProfitability_PowerBi (3)'[InvoiceDate] <= var2, 'CustomerProfitability_PowerBi (3)'[InvoiceDate] >= var1)),'CustomerProfitability_PowerBi (3)'[sku],"Qty",sum('CustomerProfitability_PowerBi (3)'[Quantity])),[Qty])
SumVal =
var var1 = 'CustomerProfitability_PowerBi (3)'[StartDate]
var var2 = 'CustomerProfitability_PowerBi (3)'[EndDate]
return
sumx(summarize((calculatetable('CustomerProfitability_PowerBi (3)','CustomerProfitability_PowerBi (3)'[InvoiceDate] <= var2, 'CustomerProfitability_PowerBi (3)'[InvoiceDate] >= var1)),'CustomerProfitability_PowerBi (3)'[sku],"Val",sum('CustomerProfitability_PowerBi (3)'[Val])),[Val])
AvgValue/Unit = 'CustomerProfitability_PowerBi (3)'[SumVal]/'CustomerProfitability_PowerBi (3)'[SumQty]
This works fine but the problem i have is when i put invoice, the measure then calculates per invoice but it needs to be by product within the daterange selected.
Regards

Related

DAX - SUMMARIZE based on SWITCH statement

I have SKUs data with some financial information. I have grouped the SKUs in 4 categories using a measure with SWITCH. Now I would like to summarise the information (e.g. total revenues of the products in each category) but I cannot get the summary to add up to the correct value.
Data model:
FactTable: fact table containing financial information (product code, revenue, profit, period, etc.)
DimSKU: mapping table with SKUs data (product code, brand, segment, etc.)
DimDates: date table (period, months, etc.)
Creating 4 product categories:
(using a measure and not a calculated column as the users can use a dropdown list and select different periods)
My Quadrants =
VAR
Quadrants =
SWITCH(
TRUE(),
AND([SKU_profit] >= [Total_profit], [SKU_growth] >= [Total_growth]), "Maintain",
AND([SKU_profit] < [Total_profit], [SKU_growth] < [Total_growth]), "Address",
AND([SKU_profit] >= [Total_profit], [SKU_growth] < [Total_growth]), "Grow",
AND([SKU_profit] < [Total_profit], [SKU_growth] >= [Total_growth]), "Investigate"
)
RETURN
Quadrants
Calculating YTD sales:
YTD revenue =
VAR
YTD_revenue = TOTALYTD(SUM(FactTable[Revenue]), DimDates[Date])
RETURN
YTD_revenue
Summarising data:
I can add the measure to a visual like a table and it works perfectly fine as long as I also display all the SKUs. Ideally, I would like a summary like the following:
MyQuadrants / Revenue
Maintain / 1,000
Address / 250
Grow / 500
Investigate / 350
I would be grateful if anyone could point me in the right direction.
Thanks all.
Edit
I have managed to make it work thanks to David's suggestion. See below for reference.
Creating configuration table
ConfigTable =
VAR
Total_profit = [Total_profit]
VAR
Total_growth = [Total_growth]
RETURN
{
("Maintain", Total_profit, 1, Total_growth, 1),
("Address", 0, Total_profit, 0, Total_growth),
("Grow", Total_profit, 1, 0, Total_growth),
("Investigate", 0, Total_profit, Total_growth, 1)
}
// 5 columns: quadrant, min profit, max profit, min growth, max growth
// Use table constructor to use variables
Creating measure
YTD revenue (segmented) =
CALCULATE(
[YTD revenue],
FILTER(
VALUES(DimSKU),
NOT ISEMPTY(
FILTER(
ConfigTable,
(
[SKU_profit] >= ConfigTable[min profit] &&
[SKU_profit] < ConfigTable[max profit] &&
[SKU_growth] >= ConfigTable[min growth] &&
[SKU_growth] < ConfigTable[min growth]
)
)
)
)

Calculating single values from cumulative data in Power BI

In my sales table I would like to change cumulative values into single values. Here is sample data from my table.
I created a measure that as far as I know should works for this.
sales_single_values = VAR current_sales = SUM('sales'[sales cumulative]) VAR prev_sales SUM('sales'[sales cumulative]) - CALCULATE( current_sales, 'sales'[period] = 'sales'[period] - 1) Return IF(ISBLANK(prev_sales), BLANK(), current_sales - prev_sales)
But unfortunately the final result on the chart is still the same as I used cumulative values, not single ones. Any ideas what should I change in my measure?
Expected values would be:
Period 1: 4
Period 2: 2
Period 3: 7
As a measure, something like:
sales_single_values =
var prev = max('sales'[period]) - 1
var prev_sales = CALCULATE( SUM('sales'[sales cumulative]), 'sales'[period] = prev)
Return
sum(sales[sales cumulative]) - prev_sales
But this seems like more of a modeling transformation, so do it in PQ, or with a calculated column, like
current sales =
var prev = calculate( max('sales'[period]) ) - 1
var prevSales = calculate( sum(sales[sales cumulative]), all(sales), sales[period] = prev)
return
sales[sales cumulative]-prevSales

Normalize measure to interval [0, 1] in Power BI

I have a dataset in 'Table1' as follows
sales
and calculate a measure of "Revenue per Customer" as follows
Revenue Per Customer =
DIVIDE(SUM('Table1'[Revenue (GBP)]), SUM('Table1'[Number Of Customers]))
As a final step, I try to normalize the measure above into interval [0,1] so that 1 would be max value as follows
Normalized Revenue Per Customer =
VAR Xi =
DIVIDE(SUM('Table1'[Revenue (GBP)]), SUM('Table1'[Number Of Customers]))
VAR MnX =
MINX('Table1',
DIVIDE(SUM('Table1'[Revenue (GBP)]), SUM('Table1'[Number Of Customers])))
VAR MxX =
MAXX('Table1', DIVIDE(SUM('Table1'[Revenue (GBP)]), SUM('Table1'[Number Of Customers])))
RETURN DIVIDE(Xi-MnX , MxX - MnX)
but does not work. In the end, I need to add the normalized measure to a visual and select via filters the year, so the normalized measure should be automatically calculated per any year combination, i.e. 2019 or 2019 and 2020 etc.
Any ideas how to make this work?
Normalized Revenue Per Customer =
VAR Xi = [Revenue Per Customer]
VAR MnX = MINX(
ALLSELECTED(Table1)
, [Revenue Per Customer]
)
VAR MxX = MAXX(
ALLSELECTED(Table1)
, [Revenue Per Customer]
)
RETURN DIVIDE(Xi-MnX , MxX - MnX)
Normalized Revenue Per Customer =
VAR Xi = [Revenue Per Customer]
VAR MnX = MINX(
ALL(Table1[Store]) -- ALLSELECTED(Table1[Store]) if you plan to filter by store.
,[Revenue Per Customer]
)
VAR MxX = MAXX(
ALL(Table1[Store]) -- ALLSELECTED(Table1[Store]) if you plan to filter by store.
,[Revenue Per Customer]
)
RETURN DIVIDE(Xi-MnX , MxX - MnX)
Your [Normalized Revenue Per Customer] should be like this:
Normalized Revenue Per Customer =
VAR Xi =
DIVIDE(SUM(Table1[Revenue(GBP)]), SUM('Table1'[Number Of Customers]))
VAR MnX =
MINX('Table1',
DIVIDE([Revenue(GBP)], [Number Of Customers]))
VAR MxX =
MAXX('Table1', DIVIDE([Revenue(GBP)],[Number Of Customers]))
RETURN DIVIDE(Xi-MnX , MxX - MnX)
If we test it with years comparison as slicer, It returns:
I hope This is the solution you are looking for.

DAX Row Total is Wrong For Sum Date Difference

I have table which includes start_date and end _date. I want to get date difference between these columns by minute. There is also date and time slicer and if max date slicer is smaller than end_date, I have to get date difference between start date and max date slicer + max time slicer.
I created a measure about this but row total is not true. How can I fix it?
this is my measure I wrote;
Zaman 3 =
var MaxDateTime =MAX(vwDimDate[Date]) + TIME(HOUR(MAX(vwDimTime[Time])), MINUTE(MAX(vwDimTime[Time])), SECOND(MAX(vwDimTime[Time])))
var sonuc1 = SUMX(vwFactATM_STATUS_LOG,DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],vwFactATM_STATUS_LOG[END_DATE],MINUTE))
var sonuc2 = SUMX(vwFactATM_STATUS_LOG,DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],MaxDateTime,MINUTE))
var deger = IF(MaxDateTime>MAX(vwFactATM_STATUS_LOG[END_DATE]),sonuc1,sonuc2)
//var a=MAX(vwFactATM_STATUS_LOG[END_DATE])
return deger
And table output;
As You see row total is not true. How can I fix it?
I solved the problem. We need calculate inside "if block" look I how I corrected
Var MaxDate=CALCULATE(MAX(vwDimDate[Date]) + TIME(HOUR(MAX(vwDimTime[Time])), MINUTE(MAX(vwDimTime[Time])), SECOND(MAX(vwDimTime[Time]))),ALLSELECTED(vwDimDate),ALLSELECTED(vwDimTime))
VAR Tarih=Sumx(
vwFactATM_STATUS_LOG, If(MaxDate<vwFactATM_STATUS_LOG[END_DATE], DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],MaxDate, MINUTE) , DATEDIFF(vwFactATM_STATUS_LOG[STARTING_DATE],vwFactATM_STATUS_LOG[END_DATE],MINUTE) ))
return Tarih

DAX to calculate cumulative sum column (year to date) for all individual products

I have 3 columns in a table: Date, Product, and Volume. Based on this is I want to calculate the cumulative sum (or YTD) column for each of the products.
I know that to calculate cumulative total we use:
YTD_Actual = CALCULATE(Sum(Combined[Actual Volume]),FILTER(Combined,Combined[Date] <= EARLIER(Combined[Date])))
But I want this to additionally filter individual products and do this calculation for that product.
The expected column is shown in the picture below:
As far as you manage to get the Month column in date format, the following works:
YTD =
VAR currProduct = Table1[Product]
VAR currMonth = Table1[Month]
VAR ytdTotal =
CALCULATE(
SUM(Table1[Volume]),
FILTER(
ALL(Table1),
Table1[Month] <= currMonth &&
Table1[Product] = currProduct
)
)
RETURN IF(NOT(ISBLANK(ytdTotal)), ytdTotal,0)
Result: