Forum Discussion
Need Help with DAX Measure Please !
- 4 years ago
Hi,
Like many people described, I also want to suggest using a proper calendar table.
But, check the below picture and the attached pbix file, which I did not create a calendar table.
All measures are in the attached pbix file.
avg of last 7 days : =
VAR currentdate =
MAX ( Data[Date] )
VAR countdates =
CALCULATE (
COUNTROWS ( VALUES ( Data[Date] ) ),
FILTER ( ALL ( Data[Date] ), Data[Date] <= currentdate )
)
VAR sevendaysperiod =
DATESBETWEEN ( Data[Date], currentdate - 6, currentdate )
VAR result =
AVERAGEX ( sevendaysperiod, [Sales measure :] )
RETURN
IF ( countdates < 7, BLANK (), result )
Hi Anonymous ,
If you have a calendar table and it's properly related to your fact table, then a measure like this should work:
_avgSales7Day =
CALCULATE(
AVERAGE(yourTable[Sales]),
DATESINPERIOD(
calendar[Date],
LASTDATE(calendar[Date]),
-7,
DAY
)
)
If you don't have a calendar table set up, then I highly recommend you get one (examples are easily found online).
Many DAX functions rely on a proper calendar table to work, thus making your life significantly easier.
Pete