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
thalitakadlec
Frequent Visitor

Calculated column to show current year unless it's january (then show last year)

Hi there,

Last year (2019) I created a dashboard which uses a calculated column to obtain the current year, with this formula:

 

= Date.Year(DateTime.LocalNow())

 

It was working fine until we turned from 2019 to 2020, but in January we are still reporting last December's metrics.

Therefore, I need to find a way what this formula shows me what year it is, unless we're in january, in which case I need it to show last year.

 

I imagine I could subtract 1 month from the current date but I am not sure how to formulate that.

Could someone help?

Thanks and happy new year!

1 ACCEPTED SOLUTION

oh, sorry @thalitakadlec  my bad

try

= if Date.Month(DateTime.LocalNow()) = 1 then Date.Year(DateTime.LocalNow())-1 else Date.Year(DateTime.LocalNow())

 

do not hesitate to give a kudo to useful posts and mark solutions as solution


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

View solution in original post

5 REPLIES 5
az38
Community Champion
Community Champion

Hi @thalitakadlec 

if you mean Custom column (power qery editor), not calculated then try

 

= if Date.Month = 1 then Date.Year(DateTime.LocalNow())-1 else Date.Year(DateTime.LocalNow())

 

do not hesitate to give a kudo to useful posts and mark solutions as solution


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Yes custom column!

I have tried your suggestion, PowerBI accepted the formula, however I'm still getting 2020 on the Year column.

Any idea why this didn't work?

 

powerbi.jpg

 
 

@thalitakadlec -

You're not providing the Date.Month ( [ ?] ) .






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



oh, sorry @thalitakadlec  my bad

try

= if Date.Month(DateTime.LocalNow()) = 1 then Date.Year(DateTime.LocalNow())-1 else Date.Year(DateTime.LocalNow())

 

do not hesitate to give a kudo to useful posts and mark solutions as solution


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Awesome it worked, thank you!

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.