Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreGet certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now
In the below image, i have 3 columns, Country from 1 table, Year from Calendar table and [Total Ratings] is a measure. I wanted to calculate the previous year ratings for each country and there are gap between years. There are random years for each country and have gap period. I want to Calculate the Total Ratings for each country based on previous year if there is no previous year then compare with the above row. For eg. Take Country Argentina, I want to compare Total Ratings of 2020 with 2019 but 2019 is not present so it should compare it with year 2017.
Solved! Go to Solution.
Try the measure below
Previous Year =
VAR PrevYear =
//get the year before current
CALCULATE (
MAX ( 'table'[year] ),
FILTER (
ALLEXCEPT ( 'table', 'table'[country] ),
'table'[year] < MAX ( 'table'[year] )
)
)
VAR PrevYearValue =
//get the value equivalent to PrevYear
CALCULATE (
[Total Ratings],
FILTER ( ALLEXCEPT ( 'table', 'table'[country] ), 'table'[year] = PrevYear )
)
RETURN
//difference
IF ( NOT ( ISBLANK ( PrevYear ) ), [Total Ratings] - PrevYearValue )
You should get a similar result as below:
Moving forward, please provide a sample data that we can easily copy-paste.
Proud to be a Super User!
VAR in DAX means variable. Instead of writing a formula everytime it is needed, it is assigned to a variable. The concept is similar to other programming languages. This makes the code shorter and easier to understand.
The variable below calculates the year before current for each country using ALLEXCEPT. It removes context filters over a table except for columns that are specified in subsequent arguments. In other words, the aggregation (which is MAX in this case) is calcualted at each country (column specified) in the table.
VAR PrevYear =
//get the year before current
CALCULATE (
MAX ( 'table'[year] ),
FILTER (
ALLEXCEPT ( 'table', 'table'[country] ),
'table'[year] < MAX ( 'table'[year] )
)
)
The variable below calculates the Total Ratings for each country in the table which year is equivalent to variable PrevYear.
VAR PrevYearValue =
//get the value equivalent to PrevYear
CALCULATE (
[Total Ratings],
FILTER ( ALLEXCEPT ( 'table', 'table'[country] ), 'table'[year] = PrevYear )
)
RETURN is used for the result you want which can be any of the variables above or another calculation or an external measure. It is used only if VAR is used. The below result could have been assigned to VAR Result and instead of wrign down the formula below Result is returned instead.
The IF condition is added so the result is shown only if the previous year is not blank otherwise it will have returned the current [Total Ratings] as there is nothing to subract from it.
RETURN
//difference
IF ( NOT ( ISBLANK ( PrevYear ) ), [Total Ratings] - PrevYearValue )
Proud to be a Super User!
Try the measure below
Previous Year =
VAR PrevYear =
//get the year before current
CALCULATE (
MAX ( 'table'[year] ),
FILTER (
ALLEXCEPT ( 'table', 'table'[country] ),
'table'[year] < MAX ( 'table'[year] )
)
)
VAR PrevYearValue =
//get the value equivalent to PrevYear
CALCULATE (
[Total Ratings],
FILTER ( ALLEXCEPT ( 'table', 'table'[country] ), 'table'[year] = PrevYear )
)
RETURN
//difference
IF ( NOT ( ISBLANK ( PrevYear ) ), [Total Ratings] - PrevYearValue )
You should get a similar result as below:
Moving forward, please provide a sample data that we can easily copy-paste.
Proud to be a Super User!
Thank you!! @danextian The Solution is giving results, if possible can you explain this? how logic has been implemented?
VAR in DAX means variable. Instead of writing a formula everytime it is needed, it is assigned to a variable. The concept is similar to other programming languages. This makes the code shorter and easier to understand.
The variable below calculates the year before current for each country using ALLEXCEPT. It removes context filters over a table except for columns that are specified in subsequent arguments. In other words, the aggregation (which is MAX in this case) is calcualted at each country (column specified) in the table.
VAR PrevYear =
//get the year before current
CALCULATE (
MAX ( 'table'[year] ),
FILTER (
ALLEXCEPT ( 'table', 'table'[country] ),
'table'[year] < MAX ( 'table'[year] )
)
)
The variable below calculates the Total Ratings for each country in the table which year is equivalent to variable PrevYear.
VAR PrevYearValue =
//get the value equivalent to PrevYear
CALCULATE (
[Total Ratings],
FILTER ( ALLEXCEPT ( 'table', 'table'[country] ), 'table'[year] = PrevYear )
)
RETURN is used for the result you want which can be any of the variables above or another calculation or an external measure. It is used only if VAR is used. The below result could have been assigned to VAR Result and instead of wrign down the formula below Result is returned instead.
The IF condition is added so the result is shown only if the previous year is not blank otherwise it will have returned the current [Total Ratings] as there is nothing to subract from it.
RETURN
//difference
IF ( NOT ( ISBLANK ( PrevYear ) ), [Total Ratings] - PrevYearValue )
Proud to be a Super User!
Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
User | Count |
---|---|
87 | |
87 | |
87 | |
67 | |
49 |
User | Count |
---|---|
135 | |
113 | |
100 | |
68 | |
67 |