Forum Discussion
Measure giving wrong answer
- 2 years ago
Try this:
TotalUsers_6M = VAR _LATEST_MONTH = MAX('Date'[Fiscal Date]) Var _END_DATE = EOMONTH(_LATEST_MONTH,0) VAR _START_DATE = EOMONTH(_END_DATE,-6)+1 VAR _MAXDATE = MAX(Sheet1[order_date]) VAR _VALUE = CALCULATE( SUM(Sheet1[Net Revenue GBP]),DATESBETWEEN('Date'[Fiscal Date],_START_DATE,_END_DATE)) VAR _RESULT = IF( EOMONTH(_MAXDATE,0) >= _LATEST_MONTH, _VALUE) RETURN _RESULT - 2 years ago
It can work with DATESINPERIOD if you protect your date by wrapping it around EOMONTH.
Basically you need to make it clear to the code that needs to take the full period of the month.
I re wrote your logic below.
Debug = VAR LATEST_MONTH = MAX(Sheet1[order_date]) Var _END_DATE = EOMONTH(LATEST_MONTH,0) var final_date = if( ISBLANK( year(_END_DATE) ) ,BLANK(), _END_DATE ) VAR DATES_IN_PERIOD_TABLE_PY = DATESINPERIOD ('Date'[Fiscal Date], final_date, -6, MONTH) VAR _Result = CALCULATE( SUM(Sheet1[Net Revenue GBP]),DATES_IN_PERIOD_TABLE_PY) RETURN _Result
Try this:
TotalUsers_6M =
VAR _LATEST_MONTH = MAX('Date'[Fiscal Date])
Var _END_DATE = EOMONTH(_LATEST_MONTH,0)
VAR _START_DATE = EOMONTH(_END_DATE,-6)+1
VAR _MAXDATE = MAX(Sheet1[order_date])
VAR _VALUE =
CALCULATE(
SUM(Sheet1[Net Revenue GBP]),DATESBETWEEN('Date'[Fiscal Date],_START_DATE,_END_DATE))
VAR _RESULT =
IF( EOMONTH(_MAXDATE,0) >= _LATEST_MONTH, _VALUE)
RETURN _RESULT- Anonymous2 years agoNot applicable
Omg, its working now.
You are genious.
I will accept this as a solution.
But before that can you please explain me the calculation ?
VAR _START_DATE = EOMONTH(_END_DATE,-6)+1Why are you adding 1 in this variable.
Ideally, datesinperiod dax should work but this id the first time I am facing this type of concern
May I know from where you learnt this all ?
- Alex872 years agoSolution Sage
So, for this variable: "VAR _START_DATE = EOMONTH(_END_DATE,-6)+1"
I am using EOMONTH, which is tranforming my end date to the last day of the month, six month previous.
For June it will therefore be 31/12 and I want it to be 01/01. So I am adding a day (+1). My Start Date Date will always be the first day of the specifc month.
The logic behind the debugging is to cut your logic in smaller parts, use variables, check them if they are correct and easily building the logic until reaching the expected result.
I am glad it worked! Best regards
- Anonymous2 years agoNot applicable
Wait a minute, I have one doubt. Like you said for Jun, calculation will start from 31/12 and it will move to 01/01if I use VAR _START_DATE = EOMONTH(_END_DATE,-6)+1. but for jun calculation is already starting from 01/01
- Anonymous2 years agoNot applicable
But datesinperiod dax should work here. Why it is not working??
Ideally it should work for rolling 6 months calculation