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

Get 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

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
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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