Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
pathare_amey
New Member

Compare the values with years that has gap

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.Screenshot_7.jpg

 

 

2 ACCEPTED SOLUTIONS
danextian
Super User
Super User

Hi @pathare_amey 

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:

danextian_0-1693131461018.png

 

Moving forward, please provide a sample data that we can easily copy-paste.










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

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 )

 

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

3 REPLIES 3
danextian
Super User
Super User

Hi @pathare_amey 

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:

danextian_0-1693131461018.png

 

Moving forward, please provide a sample data that we can easily copy-paste.










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

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 )

 

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.