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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
HamidBee
Power Participant
Power Participant

How can I create a calculated column using SAMEPERIODLASTYEAR/DATEADD + FILTER?

Hi All,

 

I'll kindlly like to ask how I can calculate the value for the same period last year for each category. So  values for A,B,C & D. I think I need to be applying filters here but I'm not too sure. Any help would be greatly appreciated.

 

This is what I have so far but I'm sure I'm way off. 

 

Dateadd.jpg

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @HamidBee ,

I have create a simple sample. Please have a try.

Create a column first.

 

year = YEAR('Table'[Date])

 

Then create a measure.

 

Measure =
VAR this_year =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[year] = SELECTEDVALUE ( 'Table'[year] )
                && 'Table'[Attribute] = SELECTEDVALUE ( 'Table'[Attribute] )
        )
    )
VAR previous_year =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[year]
                = SELECTEDVALUE ( 'Table'[year] ) - 1
                && 'Table'[Attribute] = SELECTEDVALUE ( 'Table'[Attribute] )
        )
    )
RETURN
    this_year - previous_year

 

 I have also create a column.

 

Column =
VAR _beforeyear =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            'Table',
            'Table'[year]
                = EARLIER ( 'Table'[year] ) - 1
                && 'Table'[Attribute] = EARLIER ( 'Table'[Attribute] )
        )
    )
VAR thisyear =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            'Table',
            'Table'[Attribute] = EARLIER ( 'Table'[Attribute] )
                && 'Table'[year] = EARLIER ( 'Table'[year] )
        )
    )
RETURN
    thisyear - _beforeyear

 

vpollymsft_0-1643772603692.png

Please refer to it to see if it helps you.

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

View solution in original post

15 REPLIES 15
Anonymous
Not applicable

Hi @HamidBee ,

I have create a simple sample. Please have a try.

Create a column first.

 

year = YEAR('Table'[Date])

 

Then create a measure.

 

Measure =
VAR this_year =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[year] = SELECTEDVALUE ( 'Table'[year] )
                && 'Table'[Attribute] = SELECTEDVALUE ( 'Table'[Attribute] )
        )
    )
VAR previous_year =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[year]
                = SELECTEDVALUE ( 'Table'[year] ) - 1
                && 'Table'[Attribute] = SELECTEDVALUE ( 'Table'[Attribute] )
        )
    )
RETURN
    this_year - previous_year

 

 I have also create a column.

 

Column =
VAR _beforeyear =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            'Table',
            'Table'[year]
                = EARLIER ( 'Table'[year] ) - 1
                && 'Table'[Attribute] = EARLIER ( 'Table'[Attribute] )
        )
    )
VAR thisyear =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            'Table',
            'Table'[Attribute] = EARLIER ( 'Table'[Attribute] )
                && 'Table'[year] = EARLIER ( 'Table'[year] )
        )
    )
RETURN
    thisyear - _beforeyear

 

vpollymsft_0-1643772603692.png

Please refer to it to see if it helps you.

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

VahidDM
Super User
Super User

HI @HamidBee 

Try this:
New Column = 

Var _DT = Summary Table[Date]
Var _Y = Year(_DT)
Var _M = Month(_DT)
Var _D = Day(_DT)
Var _LYD = Date(_Y-1,_M,_D)
Var _S = sum(Summary Table[value])
Var _LY = calcultae(_S,filter(all(Summary Table),Summary Table[Date]=_LYD&&Summary Table[Attribute]=Earlier(Summary Table[Attribute]))
return
_S-_LY

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

 

Error.jpg

 

This is what I get when I try to type the formula in.

Try this:

 

New Column =
VAR _DT = 'Summary Table'[Date]
VAR _Y =
    YEAR ( _DT )
VAR _M =
    MONTH ( _DT )
VAR _D =
    DAY ( _DT )
VAR _LYD =
    DATE ( _Y - 1, _M, _D )
VAR _S =
    SUM ( 'Summary Table'[value] )
VAR _LY =
    CALCULATE (
        SUM ( 'Summary Table'[value] ),
        FILTER (
            ALL ( 'Summary Table' ),
            'Summary Table'[Date] = _LYD
                && 'Summary Table'[Attribute] = EARLIER ( 'Summary Table'[Attribute] )
        )
    )
RETURN
    _S - _LY

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

I received the following error when I pasted this in:


error2.jpg

😁Sorry mate, try this:

New Column =
VAR _DT = 'Summary Table'[Date]
VAR _Y =
    YEAR ( _DT )
VAR _M =
    MONTH ( _DT )
VAR _D =
    DAY ( _DT )
VAR _LYD =
    DATE ( (_Y - 1), _M, _D )
VAR _S =
    SUM ( 'Summary Table'[value] )
VAR _LY =
    CALCULATE (
        SUM ( 'Summary Table'[value] ),
        FILTER (
            ALL ( 'Summary Table' ),
            'Summary Table'[Date] = _LYD
                && 'Summary Table'[Attribute] = EARLIER ( 'Summary Table'[Attribute] )
        )
    )
RETURN
    _S - _LY

 

Make sure Type of your Date column is DATE,

 


Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

I checked the data type and it's set to 'date'. That column has a relationship with a date table that is part of the model I'm not sure if that has anything to do with it. I'm currently getting the same error when I paste the above code.

 

An argument of function 'DATE' has the wrong data type or the result is too large or too small.

@HamidBee Can you share your PBIX file?

 

Upload that on https://wetransfer.com/

 


Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

No problem. Please find attached the example file here below:

https://www.mediafire.com/file/eptjd7i8636ba4q/Example_File.pbix/file

I'm only interested in the years spanning 2020 and 2021. Their is data for 2022 which I am not calculating for but I wouldn't want the data to be removed. 

Thanks alot for your help

HI @HamidBee 

 

Just add ' before and after the table name, or remove table name and select that again.

should be: 'Summary Table'

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

 

 

Apologies I should have noticed that. I've entered the code now and this is the new error I'm getting:

 

error2.jpg

New Column =
Var _DT = 'Summary Table'[Date]
Var _Y = Year(_DT)
Var _M = Month(_DT)
Var _D = Day(_DT)
Var _LYD = Date(_Y-1,_M,_D)
Var _S = sum('Summary Table'[value])
Var _LY = CALCULATE(_S,filter(all('Summary Table'),[Date]=_LYD&&'Summary Table'[Attribute]=Earlier('Summary Table'[Attribute]))
return
_S-_LY
Daryl-Lynch-Bzy
Resident Rockstar
Resident Rockstar

@HamidBee - What is the "value" referenced in the second part of the formula.  Should be SUM ( 'Summary Table'[VALUE] as well?.    Try using PARALLELPERIOD ( 'Calendar'[Date] , -1 , YEAR )  PARALLELPERIOD – DAX Guide

I tried using the following formula:

 

Column 2 = Calculate(sum('Summary Table'[Value]))-calculate(sum('Summary Table'[Value]),PARALLELPERIOD('Calendar'[Date],-1,YEAR))
 
but it returns the exact same values as those already in the current value column.
ALLUREAN
Solution Sage
Solution Sage

Hi, @HamidBee 

 

Can you try this:

 

Column =


SUM('Summary Table'[Value]) 

-

CALCULATE(
SUM('Summary Table'[Value]),
ALLEXCEPT('Summary Table', 'Summary Table'[Attribute]),
SAMEPERIODLASTYEAR('Calendar'[Date])
)

 

Did I answer your question? Please Like and Mark my post as a solution if it solves your issue. Thanks.

Appreciate your Kudos !!!

https://allure-analytics.com/

https://www.youtube.com/channel/UCndD_QZVNB_JWYLEmP6KrpA

https://www.linkedin.com/company/77757292/




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


https://allure-analytics.com/
https://www.youtube.com/channel/UCndD_QZVNB_JWYLEmP6KrpA
https://www.linkedin.com/company/77757292/

Proud to be a Super User!




I just tried the formula and got one value for all the rows.

one value.jpg

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.