Forum Discussion
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.
| ImpYear | ReportedDate | Id | Amount |
| 2017 | 01/01/2017 | 1 | 10 |
| 2017 | 01/01/2017 | 2 | 7 |
| 2017 | 01/01/2017 | 3 | 9 |
| 2018 | 01/01/2018 | 1 | 23 |
| 2018 | 01/01/2018 | 2 | 15 |
| 2018 | 01/01/2018 | 3 | 19 |
| 2019 | 01/01/2019 | 1 | 12 |
| 2019 | 01/01/2019 | 2 | 17 |
| 2019 | 01/01/2019 | 3 | 18 |
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:
| Id | Current Year Total | Past Year Total |
| 1 | 12 | 23 |
| 2 | 17 | 15 |
| 3 | 18 | 19 |
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.
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])-1RETURN CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[ImpYear]=_previousYear))Regards,
Nemanja Andic
10 Replies
- wdx223_DanielCommunity Champion
- AnonymousNot applicable
Hello wdx223_Daniel The above measure that you have created is Past Year Total. How would I calculate Current Year Total?
- wdx223_DanielCommunity Champion
sum(table[amount]
- nandicResident 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])-1RETURN CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[ImpYear]=_previousYear))Regards,
Nemanja Andic- AnonymousNot 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))+0Could you please let me know how can this query be rectified?
- nandicResident 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])RETURNCALCULATE([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])-1RETURN CALCULATE(SUM('Table'[Amount]),FILTER('Table','Table'[ImpYear]=_previousYear))disconnected_Year over Year = [disconnected_Current Year]-[disconnected_Past Year]Regards,
Nemanja Andic- AnonymousNot 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)+0Or may be is there any other efficient way to find the Last YTD?- nandicResident 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])RETURNCALCULATE([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 ) + 1Also, i added more data in data source so that you can check it.Regards,
Nemanja Andic