Forum Discussion
Date calculation based on date slicer
- Anonymous2 years ago
Hi Infidti ,
Since you didn't provide sample data, I created my own test set:I also created an additional date sheet to create the slicer:
Since I don't know whether you are averaging the YTD values by the number of data records or by the total number of days, I'll provide you with two DAXs for each of these two averages:
YTD_PerData = CALCULATE( AVERAGE('Table'[test data]), FILTER( ALL('Table'), 'Table'[date] <= MAX('Slicer'[Date]) ) )YTD=(10000+12000+20000+2000+31000+9000+8500+13000)/8=13187.5YTD_PerDay = VAR Days_count = DATEDIFF(DATE(2022,1,1), MAX('Slicer'[Date]), DAY) + 1 VAR Date_count = CALCULATE( SUM('Table'[test data]), FILTER( ALL('Table'), 'Table'[date] <= MAX('Slicer'[Date]) ) ) RETURN Date_count / Days_countYTD=(10000+12000+20000+2000+31000+9000+8500+13000)/90(Total days between 2022.1.1 and 2022.3.31)=1172.2
And about QTR, -5 year and -10 year, could you please provide me with the sample data and the formula so that I can know how to use the DAX function to do the calculation?
Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
The measures would all follow a similar structure:
Qtr =
CALCULATE(AVERAGE('YourTable'[YourColumn]), FILTER(ALL('YourTable'),
YEAR('YourTable'[Date]) = YEAR(SELECTEDVALUE('YourTable'[Date])
&& QUARTER('YourTable'[Date]) = QUARTER(SELECTEDVALUE('YourTable'[Date]))
)
This is assuming that your slicer is only allowing users to select a single value, if you wanted them to be able to select multiple you'd want to use an AVERAGEX over a table that was aggregated by the quarter (let me know if this is the case and I will think about it).
Last 5 Years =
CALCULATE(AVERAGE('YourTable'[YourColumn]), FILTER(ALL('YourTable'),
YEAR('YourTable'[Date]) >= YEAR(SELECTEDVALUE('YourTable'[Date]) - 5
&& YEAR('YourTable'[Date]) < YEAR(SELECTEDVALUE('YourTable'[Date])
)
The above would return an average over the last 5 years, not including the current year, you can play around with the interval and the greater than and less than signs to get the specific period that you want. Hope this helps, let me know if you have any issues or further questions.