Forum Discussion
Power BI Logic Help
- Anonymous2 years ago
Hi Anonymous ,Hello PijushRoy ,
Thank you for your prompt reply.
Based on my understanding, you want to calculate difference between dates as days.
Per my test, we can create an index column from 1 in power query for sorting your Date column:
Then create a measure using the following code to meet your requirement:
DateDiff = VAR currentIndex = SELECTEDVALUE('Table'[Index]) VAR currentDate=CALCULATE(SELECTEDVALUE('Table'[Date]),FILTER(ALL('Table'),'Table'[Index]=currentIndex)) VAR preDate=IF(currentIndex=1,CALCULATE(SELECTEDVALUE('Table'[Date]),FILTER(ALL('Table'),'Table'[Index]=1)), CALCULATE(SELECTEDVALUE('Table'[Date]),FILTER(ALL('Table'),'Table'[Index]=currentIndex-1))) RETURN DATEDIFF(preDate,currentDate,DAY)Result for your reference:
I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.
Best regards,
Joyce
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi Anonymous ,Hello PijushRoy ,
Thank you for your prompt reply.
Based on my understanding, you want to calculate difference between dates as days.
Per my test, we can create an index column from 1 in power query for sorting your Date column:
Then create a measure using the following code to meet your requirement:
DateDiff = VAR currentIndex = SELECTEDVALUE('Table'[Index])
VAR currentDate=CALCULATE(SELECTEDVALUE('Table'[Date]),FILTER(ALL('Table'),'Table'[Index]=currentIndex))
VAR preDate=IF(currentIndex=1,CALCULATE(SELECTEDVALUE('Table'[Date]),FILTER(ALL('Table'),'Table'[Index]=1)),
CALCULATE(SELECTEDVALUE('Table'[Date]),FILTER(ALL('Table'),'Table'[Index]=currentIndex-1)))
RETURN
DATEDIFF(preDate,currentDate,DAY)
Result for your reference:
I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.
Best regards,
Joyce
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.