Forum Discussion

bideveloper555's avatar
5 years ago
Solved

SamePeriodLastYear for DateTime

hi

 i am trying to get value for sameperiodlastyear using datetime column in date table.

 

i have column Datetime in table date and where the data is located transcationdate,no of transcation.

i want to drill down year,month,date,hours too.

Previous Year = CALCULATE(SUM('TransTable'[no.of trans]),SAMEPERIODLASTYEAR('DateTime'[DateTime]) )

this is not working as there are duplicates dates due to hours.

below is the same data and desired output.

DateTime table   Transcation Table   Desired output 
DateTimeDate IDTransDateno. of trans   
01/01/2020 01:0001/01/2020 101/01/2020 01:0010  CurrentyearLast year
01/01/2020 02:0001/01/2020 201/01/2020 02:0020 01/01/2020 01:001015
01/01/2020 03:0001/01/2020 301/01/2020 03:0030 01/01/2020 02:002030
01/01/2019 01:0001/01/2019 401/01/2019 01:0015 01/01/2020 03:003040
01/01/2019 02:0001/01/2019 501/01/2019 02:0030    
01/01/2019 03:0001/01/2019 601/01/2019 03:0040    
          
          
  • Hi bideveloper555 ,

     

    Time intelligence didn't work for datetime type, you can create a calculated column for lastyeardatetime and convert it to datetime type:

     

    lastyeardate = var _date = DATE(YEAR(Transcation[TransDate])-1,MONTH(Transcation[TransDate]),DAY(Transcation[TransDate])) var time =FORMAT(Transcation[TransDate],"hh:mm:ss") return _date&" "&time 

     

     

    Then create the following measure:

    Currentyear = SUM(Transcation[no. of trans])
    
    Lastyear = CALCULATE([Currentyear],FILTER(ALL(Transcation),Transcation[TransDate] = MAX(Transcation[lastyeardate])))

     

    For more details, please refer to the pbix file: https://qiuyunus-my.sharepoint.com/:u:/g/personal/pbipro_qiuyunus_onmicrosoft_com/EW3eGcCbj21LjU_e0HdgMWgBF-IT_SqqnAOukonDFg0GKQ?e=KZ2svI

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

    Best Regards,

    Dedmon Dai

3 Replies

  • bideveloper555 , this will not work with date time, You can only use date

    Create a date column

    Date = [DateTime].date
    or
    Date = date(year([DateTime ]),month([DateTime ]),day([DateTime ]))

     

    Join date with date of date table and then try

    To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

    • bideveloper555's avatar
      bideveloper555
      Helper IV

      hi,

      i created a date table and created date column in fact table.joined both date columns but still i cant drill down to HH.I can drill YY>Q>MMM>D but Hours.

      when i marked Date table as Date,drill down doesnt work at all.

  • v-deddai1-msft's avatar
    v-deddai1-msft
    Community Support

    Hi bideveloper555 ,

     

    Time intelligence didn't work for datetime type, you can create a calculated column for lastyeardatetime and convert it to datetime type:

     

    lastyeardate = var _date = DATE(YEAR(Transcation[TransDate])-1,MONTH(Transcation[TransDate]),DAY(Transcation[TransDate])) var time =FORMAT(Transcation[TransDate],"hh:mm:ss") return _date&" "&time 

     

     

    Then create the following measure:

    Currentyear = SUM(Transcation[no. of trans])
    
    Lastyear = CALCULATE([Currentyear],FILTER(ALL(Transcation),Transcation[TransDate] = MAX(Transcation[lastyeardate])))

     

    For more details, please refer to the pbix file: https://qiuyunus-my.sharepoint.com/:u:/g/personal/pbipro_qiuyunus_onmicrosoft_com/EW3eGcCbj21LjU_e0HdgMWgBF-IT_SqqnAOukonDFg0GKQ?e=KZ2svI

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

    Best Regards,

    Dedmon Dai