Forum Discussion
Syndicate_Admin
3 years agoAdministrator
Lable YTD Rows in Calendar
I'm trying to create a way to label all YTD comparable rows in a calendar table. I got close but it doesn't handle leap years (see screenshot) Can anyone assist in a proper formula that would re...
- 3 years ago
ShawnKeeneTF
The formula will return true for each of those dates.
Regards
The following M code generates all dates from 1st Jan 2020 to 31st Dec 2023 and uses the formula to mark each date as in YTD or not so you can check if it does what you want. The screenshot in your question was not readable so maybe there's some other subtlety in the question that it would reveal.
let
Source = List.Dates(#date(2020,1,1), Duration.TotalDays(#date(2023,12,31) - #date(2020,1,1)) + 1 , #duration(1,0,0,0)),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", type date}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Column1", "Date"}}),
#"Add in Year" = Table.AddColumn(
#"Renamed Columns",
"Error Free Version",
each Date.IsInYearToDate(
Date.AddYears( [Date], Date.Year(DateTime.LocalNow()) - Date.Year([Date]) )
),
type logical
)
in
#"Add in Year"
collinsg
3 years agoSolution Sage
Good day ShawnKeeneTF,
An idea would be to use AddYears to add years to “Date”. AddYears takes care of leap years sensibly.
- The number of years to add is year(this year)-year(date).
- Then check if that date is in the current year to date.
Here is M code, with the formula which gives the error and then the formula using AddYears.
Hope this helps.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtc30zcyUIrViVYystQ3grENDUDixkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
#"Added Error Version" = Table.AddColumn(
#"Changed Type",
"Errored Version",
each Date.IsInYearToDate(
#date(Date.Year(DateTime.Date(DateTime.LocalNow())),Date.Month([Date]), Date.Day([Date]))
),
type logical
),
#"Added Error-free Version" = Table.AddColumn(
#"Added Error Version",
"Error Free Version",
each Date.IsInYearToDate(
Date.AddYears( [Date], Date.Year(DateTime.LocalNow()) - Date.Year([Date]) )
),
type logical
)
in
#"Added Error-free Version"
which gives the result