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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Havard
Frequent Visitor

Find if a date from API is YTD or LYTD

I'm really new to this. I can't find how to do what's marked with BOLD. I would be very happy if anyone could help med with this:

 

if [postingDate] >= #date(ThisYear, 1, 1) and [postingDate] <= #date(ThisYear,Thismonth, ThisDay) 

then ‘’YTD’’

else if [postingDate] >= #date(ThisYear -1, 1, 1) and [postingDate] <= #date(ThisYear -1,ThisMonth, ThisDay) 

then ‘’LYTD’ 

else ‘’BLANK’’

1 ACCEPTED SOLUTION

That is how you do it. The clean code would be something like this @Havard 

let 
   varToday = Date.From(DateTimeZone.FixedUtcNow())
in

if [postingDate] >= Date.StartOfYear(varToday) and [postingDate] <= varToday

then "YTD"

else if [postingDate] >= Date.AddYears(varToday,-1) and [postingDate] <= Date.AddYears(varToday,-1)

then "LYTD"



 



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!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

5 REPLIES 5
Havard
Frequent Visitor

I Ifinally solved it, but it's bit of .."clumsy". I have seen a code with Let, but don't know how to go from there to if...

 

if [postingDate] >= Date.StartOfYear(Date.From(DateTimeZone.FixedUtcNow())) and [postingDate] <= Date.From(DateTimeZone.FixedUtcNow())

then "YTD"

else if [postingDate] >= Date.AddYears(Date.From(DateTimeZone.FixedUtcNow()),-1) and [postingDate] <= Date.From(Date.AddYears(Date.From(DateTimeZone.FixedUtcNow()),-1))

then "LYTD"

else "BLANK"

That is how you do it. The clean code would be something like this @Havard 

let 
   varToday = Date.From(DateTimeZone.FixedUtcNow())
in

if [postingDate] >= Date.StartOfYear(varToday) and [postingDate] <= varToday

then "YTD"

else if [postingDate] >= Date.AddYears(varToday,-1) and [postingDate] <= Date.AddYears(varToday,-1)

then "LYTD"



 



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!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Havard
Frequent Visitor

Thx a lot! It's easier to see the "language" now. 

Havard
Frequent Visitor

I thought this worke, but does not. Can somoen pls help me? Im NO good to this;)

 

if [postingDate] >= Date.StartOfYear(Date.From(DateTimeZone.FixedUtcNow())) and [postingDate] <= Date.From(DateTimeZone.FixedUtcNow())
then "YTD"
else if [postingDate] >= Date.AddYears(Date.From(DateTimeZone.FixedUtcNow()),-1) and [postingDate] <= Date.From(Date.AddYears(Date.From(DateTimeZone.FixedUtcNow()),-1))
then "LYTD"
else "BLANK"

Excellent @Havard - you definitely have the concepts down. Once I figured out how to use variables like this my code got much smaller and easier to edit/read.



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!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.

Top Solution Authors
Top Kudoed Authors