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.
Related
Hello community and DAX gurus!
I am trying to create a measure that calculates the total product sales for a specfic month only for does products that has been sold the same period last year.
Below is an illustration of what I want to achieve:
The first thing I did was to create a measure to calculate the Sales Amount for previous year:
Sales Amount PY =
CALCULATE(
[Sales Amount],
SAMEPERIODLASTYEAR(DimDate[Datekey])
)
The second thing I did was to create a Comparable range flag as measure:
ComparableRange = IF(FactSales[Sales Amount] = BLANK() || FactSales[Sales Amount PY] = BLANK(),0,1)
Third step I created a measure to calculate the total product sales:
Total Product Sales =
CALCULATE(
FactSales[Sales Amount],ALL(DimProduct)
)
The final step I want to calculate the total product sales only for does products being comparable.
I tried this solution but not getting it to work, it is only returning blank:
Total Product Sales Comparable =
var CompRangeTable = ADDCOLUMNS(FactSales,"#CompRange",[ComparableRange])
var FilteredTable = FILTER(CompRangeTable,[#CompRange] = 1)
return
CALCULATE(FactSales[Sales Amount],ALL(DimProduct),FilteredTable)
I also tried this solution but still getting blanks:
Total Product Sales Comparable =
var FilteredTable = FILTER(FactSales, [Sales Amount PY]*[Sales Amount]+0<>0)
return
CALCULATE([Sales Amount],ALL(DimProduct),FilteredTable)
I wonder if the issue is that the Comparable range flag doesn't evaluate during context in the measure and potentially only returning 0 and if that is the case how would you go about to solve this problem.
To demonstrate my problem I have used the ContosoRetailDW sample database with a simple star scheme consisting in the tables "FactSales", "DimDate" and "DimProduct"
This expression
ADDCOLUMNS(FactSales,"#CompRange",[ComparableRange])
is equal to
CALCULATETABLE(
ADDCOLUMNS(FactSales,"#CompRange",[ComparableRange])
,Calendar[CalendarMonth]=2000805
,DimProduct[Brand]="The Phone Company"
)
so :
1 - FactSales is cutted by context
2 - ADDCOLUMNS applies a row context to [ComparableRange] measure
to EACH row.
For example you have a row with FactSales[date]="01/01/2022"; FactSales[product]="iPhone"; FactSales[customer]="Bill Gates" ; FactSales[price]=200 ; FactSales[qty]=10 Your [Sales Amount PY] in [ComparableRange] will search SAMEPERIODLASTYEAR() on a day level, for the sample it is - "01/01/2021" most probably you have no sales for customer "Bill Gates" that date, so [ComparableRange] will return you - 0
Try this one, it's not optimized, so just check if it works.
Total Product Sales Comparable=
VAR CurrentCalendarMonth = SELECTEDVALUE(Calendar[CalendarMonth])
VAR allProducts =
CALCULATETABLE(
VALUES(DimProduct[ProductName])
,ALL() -- remove all filters and crossffilters from your data model
)
VAR totalSalesAndCompRng =
ADDCOLUMNS(
allProducts
,"#totalAmount
,CALCULATE(
[Sales Amount]
,Calendar[CalendarMonth] = CurrentCalendarMonth
)
,"#CompRange"
,CALCULATE(
[ComparableRange]
,Calendar[CalendarMonth] = CurrentCalendarMonth
)
)
VAR onlyCompRng =
FILTER(
totalSalesAndCompRng
,[#CompRange]=1
)
RETURN
SUMX(onlyCompRng,[#CompRange])
Your second measure:
Total Product Sales Comparable =
var FilteredTable =
FILTER(
FactSales
,[Sales Amount PY]*[Sales Amount]+0<>0 -- returns false
-- the reason is the same
-- as above
-- and FilteredTable is empty
)
RETURN
CALCULATE(
[Sales Amount]
,ALL(DimProduct)
,FilteredTable
)
You can try smth like this:
Total Product Sales Comparable =
var FilteredTable =
FILTER(
All(DimProduct)
,NOT [Sales Amount PY]*[Sales Amount]=0
)
VAR CurrentCalendarMonth = SELECTEDVALUE(Calendar[CalendarMonth])
RETURN
SUMX(
FilteredTable
,CALCULATE(
[Sales Amount]
,Calendar[CalendarMonth]=CurrentCalendarMonth
)
)
I got a table with a field called PERCENT. it holds the % value for each item category.
Then, I have another calculation parameter to calculate the percentage of QTy of "SD" out of Total Qty.
Now I need to add a new column to show the difference between PERCENT - the percentage of QTy.
I tried below DAX,
NEW2 =
VAR V1 =
CALCULATE(
SUM(PO_POOPEND[ORDER_QTY]),
FILTER(
AP_APCATGRY,
AP_APCATGRY[APCATGRY CODE] = "LC"
)
)
VAR V2 =
CALCULATE(
SUM(PO_POOPEND[ORDER_QTY])
)
VAR v3 = calculate (V1/V2 *100)
var v4 = SUMX(AP_MATCAT,AP_MATCAT[PERCENT] - [NEW])
VAR RESULT = v4
RETURN
RESULT
is it correct? Can someone help me with this
Your DAX is unnecessarily complicated. Since you're not making use of any of the temporary variables v1, v2 or v3 it can be simplified to:
NEW2 =
SUMX (
AP_MATCAT,
AP_MATCAT[PERCENT] - [NEW]
)
But where does measure [NEW] come from?
I have created the following code in Power BI to give me LY sales figures for any selected range.
Total Net Sales LY =
VAR MINSequentialDayNumber = MIN('Dim_Date'[SequentialDayNumber-LY])
VAR MAXSequentialDayNumber = MAX('Dim_Date'[SequentialDayNumber-LY])
RETURN
SUMX(
FILTER(
ALL(Dim_Date),
'Dim_Date'[SequentialDayNumber]>=MINSequentialDayNumber && 'Dim_Date'[SequentialDayNumber]<=MAXSequentialDayNumber),[Total Net Sales])
I am trying to do the same for a Count of Distinct Location IDs but DISTINCTCOUNTX is not usable.
Is there a way to do this with DAX, otherwise I will have to create a separate table of distinct counts and feed in which is getting messy.
Location Count LY =
VAR MINSequentialDayNumber = MIN('Dim_Date'[SequentialDayNumber-LY])
VAR MAXSequentialDayNumber = MAX('Dim_Date'[SequentialDayNumber-LY])
RETURN
DISTINCTCOUNTX(
FILTER(
ALL(Dim_Date),
'Dim_Date'[SequentialDayNumber]>=MINSequentialDayNumber && 'Dim_Date'[SequentialDayNumber]<=MAXSequentialDayNumber),[DistinctTransaction_ID])
OK I think I have worked it out...
Location Count LY =
VAR MINSequentialDayNumber = MIN('Dim_Date'[SequentialDayNumber-LY])
VAR MAXSequentialDayNumber = MAX('Dim_Date'[SequentialDayNumber-LY])
RETURN
CALCULATE
(DISTINCTCOUNT('Fact_Sales'[DistinctTransaction_ID]),
FILTER(ALL(Dim_Date),
'Dim_Date'[SequentialDayNumber]>=MINSequentialDayNumber && 'Dim_Date'[SequentialDayNumber]<=MAXSequentialDayNumber)
)
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
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: