Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Problem with DATESINPERIOD function

Hello,

 

I have a table of self reports of COVID symptoms which contatins user ID (AccountName) and the date of the report.

 

I'm trying to create a function which sums the unique reports over the last two weeks.

 

When I enter the dates manually using DATESBETWEEN I get the expected result

 

Reports_Lst2Wks_ALT = CALCULATE(DISTINCTCOUNT(selfreports[AccountName]),DATESBETWEEN(selfreports[Date],DATE(2020,08,14),DATE(2020,08,27)))
 
However I need the function to be adaptive to the current date.
 
I would expect the following function to give the same result as above:
 
Reports_Lst2Wks = CALCULATE(DISTINCTCOUNT(selfreports[AccountName]),DATESINPERIOD(selfreports[Date],[TODAY](),-7,DAY))
 
But instead it gives a result of 0.
 
Can anyone see what the problem might be?
 
Thanks!
  • Hey Anonymous ,

     

    you can use this DAX statement instead:

    Reports_Lst2Wks_ALT = 
    var _today = TODAY()
    var _7DaysBeforeToday = _today -7
    return
    CALCULATE(
    DISTINCTCOUNT(selfreports[AccountName])
    ,DATESBETWEEN(selfreports[Date] , _7DaysBeforeToday , _today)
    )

    DATESINPERIOD requires a dedicated date table without gaps, for this I recommend following the link Greg_Deckler provided in his post.

     

    Hopefully, this helps to tackle your challenge.

     

    Regards,

    Tom

     

4 Replies