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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Havard
Frequent Visitor

Find if posting date is YTD or LYTD, The formula does not work... Hope for help

if [postingDate] >= Date.StartOfYear(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"

 

The result is "BLANK"

 

Had one formula for this before, but it's gone....

6 REPLIES 6
Havard
Frequent Visitor

Thx, there is some missing, fex jan 14 2022 and feb 10th 2022 should be LYTD, but can't manage that... Later than today has no intereset, so nor problem there. Hope You can adjust, I have tried "everything" the last 3 hrs..., but I don't have the skills to manage...

Assuming your year runs from Jan-Dec, can't you just compare the current year to the posting date year?

 

let
    Source = 
        Table.FromColumns({
            {#date(2022,12,30), 
             #date(2023,2,1), 
             #date(2024,1,10), 
             #date(2020,1,1),
             #date(2022,1,14),
             #date(2022,2,10)}             
             },
            type table[postingDate=date]
        ),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each 
            let 
                #"This Year" = Date.Year(DateTime.FixedLocalNow()),
                #"Posting Year" = Date.Year([postingDate])
            in 
                if #"Posting Year" >= #"This Year"  then "YTD"
                else if #"This Year" - #"Posting Year" = 1 then "LYTD"
                else "BLANK",
            type text)
in
    #"Added Custom"

ronrsnfld_0-1676405718218.png

 

 

 

Thx again, that worked! ...but i don't know how to make that in the original form... Could You pls..?

I don't understand what you mean. Just use the formula that shows in the #"Added Custom" step when you click on that Applied Step.

jbwtp
Memorable Member
Memorable Member

Hi @Havard,

 

This is quite strange... It does show up as LYTD on my side ( do yo umind checking the conditions/formula and the dates format [maybe?]):

jbwtp_0-1676405514573.png

 

 

Cheers,

John

jbwtp
Memorable Member
Memorable Member

Hi @Havard,

 

you can use something like this:

Table.AddColumn(#"Renamed Columns", "Custom", each let daysDiff = Number.From([postingDate] - Date.StartOfYear(Date.From(DateTimeZone.FixedUtcNow()))), res = if daysDiff >= 0 then "YTD" else if daysDiff > -365 then "LYTD" else "BLANK" in res, type text)

Please note that future year will be "YTD" in this case:

jbwtp_0-1676328798548.png

 

Cheers,

John

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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