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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
jfranco
Frequent Visitor

DAX Calculated Column - Value from a same date previous year

Hello and Good Morning.

 

I have another question from the knowledge base.

 

I have the following table:

 

IDLocationDateProduction
ANorth01/01/202410
BNorth01/01/20249
CNorth01/01/202411
ASouth01/01/202420
BSouth01/01/202418
CSouth01/01/202422

 

I'm trying to create a calculated column, that returns the Production Value for the same period (month) the previous year.

Example

for A : North : 01/01/2024  return the production for A : North : 01/01/2023 and so on

 

Thanks in advance

Roger

1 ACCEPTED SOLUTION

Thank you @v-jialongy-msft 

Thank you!

Almost but not quite.

I ended up creating two columns

M = Month(Table[Date])

PY = Year(Table[Date])

 

and then using a variation of your DAX code :

 

CALCULATE (SUM([Production)]),   
FILTER(ALL(Table),
Table[ID]=EARLIER(Table[ID])
&& Table[Location] = EARLIER(Table[Location])
&& Month(Table[Date]) = Earlier(Table[M])
&& Year(Table[Date]) = EARLIER(Table[PY]) )))

 

Gracias

Roger

View solution in original post

6 REPLIES 6
v-jialongy-msft
Community Support
Community Support

Hi @jfranco 

 

Please try the following dax:

PreviousYearProduction = 
CALCULATE(
    SUM('Table'[Production]),
    FILTER(
        ALL('Table'),
        'Table'[ID] = EARLIER('Table'[ID]) &&
        'Table'[Location] = EARLIER('Table'[Location]) &&
        'Table'[Date] = DATEADD('Table'[Date], -1, YEAR)
    )
)

 

 

 

 

 

Best Regards,

Jayleny

 

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

Thank you @v-jialongy-msft 

Thank you!

Almost but not quite.

I ended up creating two columns

M = Month(Table[Date])

PY = Year(Table[Date])

 

and then using a variation of your DAX code :

 

CALCULATE (SUM([Production)]),   
FILTER(ALL(Table),
Table[ID]=EARLIER(Table[ID])
&& Table[Location] = EARLIER(Table[Location])
&& Month(Table[Date]) = Earlier(Table[M])
&& Year(Table[Date]) = EARLIER(Table[PY]) )))

 

Gracias

Roger

jfranco
Frequent Visitor

Something else I've tried:

Added Two Columns:

LYM = MONTH (DATE)

LY = YEAR (DATE) -1

 

Then tried : 

CALCULATE (SUM([Production]),
MONTH([Date]) = [LYM],
year([Date]) = [LY]))
 
But still get blanks...any idea what I'm doing wrong?
 
jfranco
Frequent Visitor

Further.

 

This is the DAX formula I am using, but it returns blanks:

 

Prev Year Tot Prod  =  CALCULATE SUMTABLE [Production]), SAMEPERIODLASTYEAR([Date]))
belvoir99
Resolver III
Resolver III

LastYearDate = DATE(YEAR('Calendar'[Date])-1, MONTH('Calendar'[Date]), DAY('Calendar'[Date]))

in the above calculated column, 1 is subtracted from the Year.
see - https://learn.microsoft.com/en-us/dax/date-function-dax for more info.

the data type returned is a date/time where the time is midnight i.e. 00:00 - that should be fine for most purposes.

in the above example 'Calendar' is the name of my table - you need to replace that with yours. You don't need apostrophes unless it is a reserved word (like Calendar) or the table name has a space (etc) in the name.

Thanks @belvoir99 

I appreciate the formula to get the calculation for the same date last year, but what I am trying to accomplish is Return the Production (or sales or any other variable) for ID = A (or B or C), and Location = North (or South) for  DATE last year (so if Jan 15 2024 then Jan 15 2023)

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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