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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Trying to Return Current Date

Hi Everyone,

 

I'm trying to return the current year and last year in a new column. However, when I try creating a DAX formula, it keeps either giving me an error or giving me a random year:

 

ThisYear/LastYear =
var lastyear = (YEAR(TODAY()) - 1)
var thisyear = (YEAR(TODAY()))
return
IF('Date'[Year] = thisyear, FORMAT(thisyear, "yyyy"), "NA"
)
 
antoineleduke_0-1643299422716.png

 

I tried using SWITCH but that doesn't work. Any advice would be greatly appreciated.

 

Thanks!

2 ACCEPTED SOLUTIONS
ValtteriN
Super User
Super User

Hi,

Include the bolded DAX in your FORMAT and you are good to go:

ThisYear =
var _ty = YEAR(TODAY())
return
if('Calendar'[Year]=_ty,FORMAT(date(_ty,1,1),"YYYY"),"NA")

I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!






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

Proud to be a Super User!




View solution in original post

For both use this kind of structure:

ThisYear + LY =
var _ty = YEAR(TODAY())
var _ly = _ty-1
return
SWITCH(TRUE(),'Calendar'[Year]=_ty,FORMAT(date(_ty,1,1),"YYYY"),
'Calendar'[Year]=_Ly,FORMAT(date(_Ly,1,1),"YYYY"),
"NA")

SWITCH + TRUE is a useful combo





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

Proud to be a Super User!




View solution in original post

6 REPLIES 6
ValtteriN
Super User
Super User

Hi,

Include the bolded DAX in your FORMAT and you are good to go:

ThisYear =
var _ty = YEAR(TODAY())
return
if('Calendar'[Year]=_ty,FORMAT(date(_ty,1,1),"YYYY"),"NA")

I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!






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

Proud to be a Super User!




Anonymous
Not applicable

Thank you! Can you explan what the bolded portion is doing just so I understand?

@Anonymous 

So basically FORMAT recognizes your "YYYY" as date formatstring and for this to work properly you need to pass a "date" to it. Now we create a proper date in this case 1.1.2022 and take the year portion of this with FORMAT to our column.





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

Proud to be a Super User!




Anonymous
Not applicable

And would you know why this isn't working when I change it to switch? I'd like it to display values for This Year and Last Year

For both use this kind of structure:

ThisYear + LY =
var _ty = YEAR(TODAY())
var _ly = _ty-1
return
SWITCH(TRUE(),'Calendar'[Year]=_ty,FORMAT(date(_ty,1,1),"YYYY"),
'Calendar'[Year]=_Ly,FORMAT(date(_Ly,1,1),"YYYY"),
"NA")

SWITCH + TRUE is a useful combo





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

Proud to be a Super User!




Anonymous
Not applicable

Thank you so much!

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.