Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Filter amounts by year

Hello Community,

 

I have a sample dataset given below, where, I want to filter by year and calculate the total amount for each id for current year and past year. 

 

ImpYearReportedDateIdAmount
201701/01/2017110
201701/01/201727
201701/01/201739
201801/01/2018123
201801/01/2018215
201801/01/2018319
201901/01/2019112
201901/01/2019217
201901/01/2019318

 

The final output should have 'Year' present in the filter so that I can select any year from my data. Below is my expected output if I have filtered out Year to 2019:

IdCurrent Year TotalPast Year Total
11223
21715
31819

 

Also please note that the 'ReportedDate' column is actually a hierarchy column with day, month, quarter and year where I have consirdered only the 'year' part.

Below is a snippet of my DAX query which does not give me the numbers. 
Past Year Total= CALCULATE(sum(Table[Amount]),FILTER(Table,Table[ImpYear]=Table[ReportedDate].[Year]-1))+0
 
  • Hi Anonymous ,
    There is option to create disconnected table.
    Example: create new table which will have values 2017, 2018, 2019. This table must not have any connection to existing table where you have measures.

    In slicer use Year from this new disconnected table.
    Now create these 2 measures:

    Current Year =
    var _currentYear = SELECTEDVALUE('Year'[Year])
    RETURN CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[ImpYear]=_currentYear))
    Past Year =
    var _previousYear = SELECTEDVALUE('Year'[Year])-1
    RETURN CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[ImpYear]=_previousYear))
     

    Regards,
    Nemanja Andic

     

10 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello wdx223_Daniel The above measure that you have created is Past Year Total. How would I calculate Current Year Total?

       

  • nandic's avatar
    nandic
    Resident Rockstar

    Hi Anonymous ,
    There is option to create disconnected table.
    Example: create new table which will have values 2017, 2018, 2019. This table must not have any connection to existing table where you have measures.

    In slicer use Year from this new disconnected table.
    Now create these 2 measures:

    Current Year =
    var _currentYear = SELECTEDVALUE('Year'[Year])
    RETURN CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[ImpYear]=_currentYear))
    Past Year =
    var _previousYear = SELECTEDVALUE('Year'[Year])-1
    RETURN CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[ImpYear]=_previousYear))
     

    Regards,
    Nemanja Andic

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello nandic ,

       

      Thank you for your response! I also wanted to calculate the 'Change YTD', which also be based on the year that will be chosen from the filter.  My current DAX query looks like:

      [Current Year]-[Past YTD], where,
      [Current Year] will contain the formula that you have mentioned in the previous post, whereas, [Past YTD] =
      CALCULATE(sum(Table[Amount]),MONTH(Table[ReportedDate])<=MONTH(TODAY())-1,FILTER(Table,Table[ImpYear]=Year(today())-1))+0
      Could you please let me know how can this query be rectified?
  • nandic's avatar
    nandic
    Resident Rockstar

    Hi Anonymous , attached power bi file with both scenarios.
    One scenario (on the right) which is simpler, it doesn't use disconnected table so everything is based on original (regular) table.

     

    regular_Current Year = SUM('Table'[Amount])
     
    regular_Past Year =
    var _currentYear = MAX('Table'[ImpYear])
    RETURN
    CALCULATE([regular_Current Year],'Table'[ImpYear]=_currentYear-1)
     
    regular_Year over Year = [regular_Current Year]-[regular_Past Year]

     


    Second scenario (on the left) if you are using disconnected table.

    disconnected_Current Year =
    var _currentYear = SELECTEDVALUE('Year'[Year])
    RETURN CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[ImpYear]=_currentYear))
    disconnected_Past Year =
    var _previousYear = SELECTEDVALUE('Year'[Year])-1
    RETURN CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[ImpYear]=_previousYear))
     
    disconnected_Year over Year = [disconnected_Current Year]-[disconnected_Past Year]
     
     

    Regards,
    Nemanja Andic

    • Anonymous's avatar
      Anonymous
      Not applicable

      nandic I want a Last YTD total which is different from Last Year total. So in order to do that, I tried this query which is not giving any value:

      Last YTD = 

      var _cy = MAX('Table'[ImpYear]) RETURN CALCULATE([regular_Current Year], MONTH(Table[ReportedDate])<=MONTH(TODAY())-1, 'Table'[ImpYear]=_cy)+0
       
      Or may be is there any other efficient way to find the Last YTD?

       

      • nandic's avatar
        nandic
        Resident Rockstar

        Anonymous , attached new version of the file, i missed that you wrote last year "ytd".
        New version of the file is focused on regular measures (i deleted disconnected table and measures).

        Past YTD =
        var _currentYear = MAX('Table'[ImpYear])
        RETURN
        CALCULATE([Current Year],'Table'[ImpYear]=_currentYear-1, 'Table'[Day of Year]<=DAY(TODAY()))
        For this purpose i added new column in table which calculates day number of the year.
        Day of Year =
        DATEDIFF ( DATE ( YEAR ( 'Table'[ReportedDate] ), 1, 1 ), 'Table'[ReportedDate], DAY ) + 1
         
        Also, i added more data in data source so that you can check it.
         
        Regards,
        Nemanja Andic