I've been trying to calculate stores' first 10 years of annual sales since their unique inception date.
My Yr1 Sales measure is working correctly
[results for Yr1 Sales][1]
and its code:
Yr1 Sales =
CALCULATE(
SUM(Actuals[Lawson SALES]),
DATESINPERIOD (
'Date'[Date],
MIN('STORE DATABASE'[STORE_OPEN_Date]),
12,
MONTH))
My Code for a measure [Yr2 Sales] (error):
**Yr2 Sales =
VAR Yr2StartDate = DATEADD('STORE DATABASE'[STORE_OPEN_Date],12,MONTH)
VAR SalesOfYear2 = CALCULATE(
SUM(Actuals[Lawson SALES]),
DATESINPERIOD (
'Date'[Date],
Yr2StartDate,
12,
MONTH))
RETURN
SalesOfYear2**
error message when I drag the measure to the same table: "MdxScript(Model)(24, 9) Calculation error in measure 'Actuals'[Yr2 Sales]: A table of multiple values was supplied where a single value was expected.
[1]: https://i.stack.imgur.com/XrlrD.png
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 am trying to wrote logic to show the Year to Date values on a P&L alongside in month values. The logic below works on the monthly calculations but an error is being received when adding in logic to attempt to show the YTD values at the end of the statement.
Selected YTD Actuals =
VAR CURRENTITEM = SELECTEDVALUE( 'P&L Template'[Items (Normalized)])
return
SWITCH( TRUE(),
CURRENTITEM = "Total Staff Costs", [YTD Total Staff Costs],
CURRENTITEM = "Total Other Overheads", [Total Other Overheads YTD],
CURRENTITEM = "Total Spend", [Total Spend YTD],
CALCULATE(CALCULATE([YTD Cost], FILTER( 'Transaction Data', 'Transaction Data'[CATEGORY] = CURRENTITEM), CALCULATE ([Total Cost], FILTER('Transaction Data', DATESYTD (Dates[Date]))))))
Any idea on how to resolve the calculate expression error above?
Appreciate any help,
Bruce
My Dataset
am trying create a report based on the selected quarter
What I want is sum of sales by quarter grouped by product in one column and sum of sales for the year until selected quarter for that year.
Example: (this is what i got ... not right though)
Model:
DAX:
ByProductforselectedquarter =
VAR vTable =
SUMMARIZE (
sales,
Sales[Product], Sales[Sales] )
VAR Result =
SUMX ( vTable, Sales[Sales] )
RETURN
Result
how to get sales for the year until selected quarter for that year.
You can try something as below-
ByProductUptoselectedquarter =
var selected_max_date = max(Date[Date])
var selected_year = Year(selected_max_date)
return
calculate(
sum(Sales[Sales]),
filter(
all(Sales),
Year(Sales[Date]) = selected_year
&& Sales[Date] <= selected_max_date
)
)
I have the below measure which calculates the headcount. I would like to create a new measure which calculates the running total for the past 12 months.
TIA
Employee Count =
VAR selectedDate = MAX('Date'[Date])
RETURN
SUMX('Table1',
VAR employeeStartDate = [DATE_OF_EMPLOYMENT]
VAR employeeEndDate = [DATE_OF_LEAVING]
RETURN IF(employeeStartDate <= selectedDate &&
OR(employeeEndDate >= selectedDate, employeeEndDate=BLANK()
),1,0)
)
If I get you right, you want something that is called Moving Annual Total (MAT). Assuming that you have a Date table (Calendar) you can use the following pattern. Replace the [Sales Amount] measure with your favorite one.
Sales MAT :=
CALCULATE(
[Sales Amount],
DATESINPERIOD(
'Date'[Date],
MAX( 'Date'[Date] ),
-1,
YEAR
)
)
Check for MAT here: https://www.daxpatterns.com/standard-time-related-calculations/
I need help creating a measure that will count the total merch booked from a previous working day.
I currently have the following:
dimDate table
A. This table contains following:
i. Date Column
ii. Dayofweek column: 1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday
iii. Working Day column: indicates whether it is a "Working" or "Non-Working" day based on the combination of "Dayofweek" and "Holiday" Column.
Total Merch Booked measure
Here are the conditions that this previous day measure should follow:
Weekday (TODAY()) = 2 (Monday), then it should look whether the Friday before was a working day, if so, then it should calculate Total Merch booked on that day, otherwise keep repeating to the previous day to it until it finds a working day and then calculate Total Merch Booked.
Weekday(TODAY()) =1 OR Weekday (TODAY()) =7 (Saturday or Sunday), then skip and do not calculate the Total Merch booked.
Weekday(TODAY()) = any other number besides 1, 2 or 7 (Tuesday thru Friday), then it should look at the previous day to see if it is a Working day, if so, then it should calculate Total Merch booked on that day, otherwise going in backwards until it finds a working day and then calculate Total Merch booked.
I tried to use the following, but i also need to count 'holidays' in to the mix:
IF(
WEEKDAY(TODAY()) = 2,
CALCULATE(
[Total Merch Booked],
'dimDate'[Date]= (TODAY()-3)
),
IF(
WEEKDAY(TODAY()) = 1,
BLANK(),
IF(
WEEKDAY(TODAY()) = 7,
BLANK(),
CALCULATE(
[Total Merch Booked],
'dimDate' [Date] = (TODAY()-1)
)
)
)
)
This is really difficult to suggest without sample data and expected output. But you can you try with this below measure-
total_merch_booked =
VAR previous_working_day =
CALCULATE(
MAX('dimDate'[Date]),
FILTER(
ALL('dimDate'),
'dimDate'[Date] < TODAY()
&& 'dimDate'[Working Day] = "Working"
)
)
RETURN
IF(
WEEKDAY(TODAY()) IN { 1, 7 },
BLANK(),
CALCULATE(
[Total Merch Booked],
FILTER(
ALL('dimDate'),
'dimDate'[Date]= previous_working_day
)
)
)