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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
asantos12
Frequent Visitor

Sum values in the two previous years and show in the actual year column in a graph

Hi everyone,
I am trying to create a measure to show in a graph the volume of Sales in the two previous years. I wanna show in the 2019 column the sum of Sales from 2018 and 2017. In the data base I Just have the Sales per year individually.
1 ACCEPTED SOLUTION
TomMartens
Super User
Super User

Hey,

 

assuming your table looks like this:

image.png

 

you can create a measure like this:

 

Measure = 
    var _currentYear = MAX('Table1'[Year])
    var _prevYear = _currentYear - 1
    var _prevprevYear = _currentYear -2
    return
    CALCULATE(
        SUM(Table1[Sales])
        , 'Table1'[Year] IN {_prevprevYear , _prevYear}
    )

Using the measure you can visualize the table like this:

 

image.png

You might want to suppress the calculation for the total line, then just use this:

Measure = 
    var _currentYear = MAX('Table1'[Year])
    var _prevYear = _currentYear - 1
    var _prevprevYear = _currentYear -2
    return
    IF(HASONEVALUE(Table1[Year])
        ,CALCULATE(
            SUM(Table1[Sales])
            --, ALL(Table1[Year])
            , 'Table1'[Year] IN {_prevprevYear , _prevYear}
        )
        , BLANK()
    )

Hopefully this provides what you are looking for.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

3 REPLIES 3
TomMartens
Super User
Super User

Hey,

 

assuming your table looks like this:

image.png

 

you can create a measure like this:

 

Measure = 
    var _currentYear = MAX('Table1'[Year])
    var _prevYear = _currentYear - 1
    var _prevprevYear = _currentYear -2
    return
    CALCULATE(
        SUM(Table1[Sales])
        , 'Table1'[Year] IN {_prevprevYear , _prevYear}
    )

Using the measure you can visualize the table like this:

 

image.png

You might want to suppress the calculation for the total line, then just use this:

Measure = 
    var _currentYear = MAX('Table1'[Year])
    var _prevYear = _currentYear - 1
    var _prevprevYear = _currentYear -2
    return
    IF(HASONEVALUE(Table1[Year])
        ,CALCULATE(
            SUM(Table1[Sales])
            --, ALL(Table1[Year])
            , 'Table1'[Year] IN {_prevprevYear , _prevYear}
        )
        , BLANK()
    )

Hopefully this provides what you are looking for.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Hi Tom, 

 

It worked perfectly,I really apreciate your help. My biggest difficult is to get familiar with HASONEVALUE function, I've tried something very similar with the answer that you gave me, but I just could show de value in a visual card and not in a graph.

 

Thank you very much,

Arthur Santos

Anonymous
Not applicable

Hello-

 

   Create a DAX measure.

 

Total = calculate(sum(this year)) + calculate(sum(last year))

Jared

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.