Forum Discussion
Need help on Calculate function
- 5 years ago
Understanding how CALCULATE works is fundamental in DAX. CALCULATE allows you to remove the filter context - if you need to - and apply further filters. The function brekas down into "two sections":
CALCULATE( expression, filters)
Expression is what you are calculating; filters is the rows you want the calculation to be carried out (using filters or table references),
In you case, your measure is:
Work orders count = CALCULATE(
COUNTROWS('Work orders'),
'Timesheet'
)So the expression in red is what you want to calculate, and the table reference is the filter you want the calculation to be applied to (in other words, which rows to make the calculation on).
In your model, the table 'Work orders" is a dimension table. If you simply counted the rows of this table, you would get the toal number of rows in the table for all the years, since it is unrleated to the Date table. By adding the "Timesheet" filter expression in the CALCULATE function, you are saying you want the COUNTROWS to be filtered according to the filters applied to the Timesheet table (which is filtered by the Year field.
To see the difference I've used a sample dataset similar to your structure. This is the model:
Now compare these two measures:
Countrows Dim Items = COUNTROWS('Dim Item')and (similar to your measure)
Calc Countrows w DataTable = CALCULATE(COUNTROWS('Dim Item'), 'DataTable')In a table you get this:
The first measure (your example), is counting the rows filtered by the Year field.
Just so that you can see the DISTINCTCOUNT also:
I hope that helps a bit. It is very important that you understand how CALCULATE works!
To get the values for the previous year, use PREVIOUSYEAR(Date[Date]) instead of SAMEPERIODLASTYEAR
PY Calc Countrows = CALCULATE([Calc Countrows w DataTable], PREVIOUSYEAR('Calendar Table'[Date]))
Edit:
In my sample, SAMEPERIODLASTYEAR also works:
SPLY Calc Countrows = CALCULATE([Calc Countrows w DataTable], SAMEPERIODLASTYEAR('Calendar Table'[Date]))
Does your date table have continuous dates covering the range of dates in the model?
Hi,
Thank you very much for your kind explanation. Now I have better understanding. And yes, after using PREVIOUSYEAR(), it is correct now.
Mind for last question ? (really it is my last question), 😁 why SAMEPERIODLASTYEAR is not working ?
As I tried to not using too many function, it seems too many similar function but each has its own tricks.
Thanks.
- PaulDBrown5 years agoCommunity Champion
From the official DAX reference guide:
so one returns all the dates from the previous year; the other the values within the range of dates selected. I'm not sure why SAMEPERIODLASTYEAR doesn't work in your case, since the date range cover whole years anyway (filtering by year). In the example I posted, both work however.
can you post a screenshot of your date table?
- admin_xlsior5 years agoPost Prodigy
Hi,
Just found out, my Date table is not continues after all. So this is the cause obviously.
Many thanks for your guidance on this. Much appreciated.
Thanks