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
)
)
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 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 have 2 tables
1 table is date table with following fields: date, month, quarter, year, yearmonth,yearnumber
2nd table is Employee table with following fields: EmpID, Hired Date, Termination Date,
employees that are active have a blank termination date.
I am trying to do employee head count by month and year. Wrote a dax measure using forums but it is showing me blank results:
Would be gratefull for any help here.
HeadCount =
var endperiod=MAX('date'[Date])
var startperiod=MIN('date'[Date])
return
CALCULATE(DISTINCTCOUNT('shared User'[EmployeeId]),FILTER(ALL('shared User'),('shared User'[HireDate]<=endperiod && 'shared User'[TerminationDate]>=startperiod)))
You can use the following to accomplish your task (I am assuming you are slicing based on your calendar[date] column, and employee_ID is unique):
EmployeeCount =
Var __SDate = MIN( 'Calendar'[Date] )
VAR __EDate = MAX ( 'Calendar'[Date] )
REturn
COUNTX(
FILTER(
EmployeeTable,
EmployeeTable[HIRE_DATE] >= __SDate
&& EmployeeTable[END_DATE] <= __EDate
) , [EM_ID]
)
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:
I want to calculate the sum of MTD sales per day for the same period of last year, only until a specific day - the day of the last sales transaction.
So for example, if the filter is year= 2019 and month= 2, I need the MTD sales for february 2018 until the 5th, calculated day by day:
MTDSales=
VAR MyDay = 5
RETURN
CALCULATE(
TOTALMTD(SUM(Sales); Calendar[Date]);
SAMEPERIODLASTYEAR(Calendar[Date]);
//here I need another filter to stop on the 5th!
)
Edit:
Please have a look at this link to see the sample data.
The measures I'm trying to build are:
Sales MTD CY
Sales MTD LY
Sales MTD CY*
Sales MTD LY*
Sales MTD CY**
Sales MTD LY**
Thanks for helping!
I'm assuming that you are using 5 since today is February 5th.
You can get MTDSales for the current month like this:
MTDSales =
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(TODAY(), - 1) + 1, TODAY() )
RETURN CALCULATE( SUM( Sales ), DateRange )
To match that up with the previous year, just use SAMEPERIODLASTYEAR.
LastYearMTDSales =
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(TODAY(), - 1) + 1, TODAY() )
RETURN CALCULATE( SUM( Sales ), SAMEPERIODLASTYEAR(DateRange) )
If you want to use a different date than TODAY, just specify that date as a variable and pass it into the DateRange variable where TODAY appears.
If you want to find the MTDSales up to the 5th day of the month (assuming you have the month in your filter context), try this
MTDSales =
Var MyDay = 5
VAR MyDate = MIN( Calendar[Date] ) + MyDay - 1
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(MyDate, -1) + 1, MyDate )
RETURN CALCULATE( [Sum of Sales], DateRange )
Then for the previous year, you can reuse that measure, but shifted:
PrevYearMTDSales =
CALCULATE( [MTDSales], ALL( Calendar ), SAMEPERIODLASTYEAR( Calendar[Date] ) )
Edit: After looking at your PBIX, I realized that I had made the wrong assumption about your filter context. Since you are looking to write a measure at the date level, try this instead:
Sales MTD CY =
VAR MyDay = 5
VAR CurrentDate = MAX ( 'Calendar'[Date] )
VAR MyDate = EOMONTH ( CurrentDate, -1 ) + MIN ( MyDay, DAY ( CurrentDate ) )
RETURN
CALCULATE (
TOTALMTD ( SUM ( Sales[Sales] ), 'Calendar'[Date] ),
FILTER ( 'Calendar', 'Calendar'[Date] <= MyDate )
)
The previous year measure can still be done referencing this measure and shifting.
Replace my column names for yours and it should work
The formula remains the same:
MTDSales =
VAR MyDay = 5
RETURN
CALCULATE(
TOTALMTD([total sales], 'Calendar'[DateKey]),
SAMEPERIODLASTYEAR('Calendar'[DateKey]),
FILTER(
ALL(Sales),
Sales[DateKey] >= STARTOFMONTH('Calendar'[DateKey]) && Sales[DateKey] <= DATEADD(STARTOFMONTH(Sales[DateKey]),MyDay-1,DAY)
)
)