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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
bleow
Frequent Visitor

Generating date table by year

I need to generate a date table like the following:

calendar year startcalendar year end
1/1/202112/12/2021
1/1/202012/12/2020
1/1/201912/12/2019

 

The number of rows is referencing another field:

NumYearsBack

4

 

How do I dynamically generate this date table, accounting for leap years?

 

I have got this so far:

 

= Table.FromList(
        List.Dates(#date(Date.Year(DateTime.LocalNow()), 1, 1), List.First(#"1 Date Start/End"[NumYearsBack])+1, #duration(
            -(   if Number.Mod(Date.Year(DateTime.LocalNow())-1, 4) = 0 and Number.Mod(Date.Year(DateTime.LocalNow())-1, 100) <> 0 then 366 
            else if Number.Mod(Date.Year(DateTime.LocalNow())-1, 4) = 0 and Number.Mod(Date.Year(DateTime.LocalNow())-1, 100) = 0 and Number.Mod(Date.Year(DateTime.LocalNow())-1, 400) = 0 then 366 else 365)
            ,0,0,0)
        )
        , Splitter.SplitByNothing(), null, null, ExtraValues.Error
    )

 

 But the leap year check is wrong because I'm always referencing this year, not the current row's year. Hence I get

bleow_0-1624010447189.png

 

How do I reference the current row's year? Or, is there a better way of generating this table? Thanks in advance.

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @bleow,

Did you mean to use the current date and the range to generate a table with the start date and end date? 

If that is the case, you can take a look at the following formula:

let
    offset = 4,
    Source =
        List.Transform(
            List.Numbers(
                Date.Year(DateTime.LocalNow())
                - offset
                + 1,
                offset
            ),
            each
                Text.From(_)
                & ","
                & Text.From(#date(_, 1, 1))
                & ","
                & Text.From(#date(_, 12, 31))
        ),
    #"Converted to Table" =
        Table.FromList(
            Source,
            Splitter.SplitTextByDelimiter(","),
            null,
            null,
            ExtraValues.Error
        ),
    #"Renamed Columns" =
        Table.RenameColumns(
            #"Converted to Table",
            {
                {
                    "Column1",
                    "Year"
                },
                {
                    "Column2",
                    "Start"
                },
                {
                    "Column3",
                    "End"
                }
            }
        ),
    #"Changed Type" =
        Table.TransformColumnTypes(
            #"Renamed Columns",
            {
                {
                    "Year",
                    Int64.Type
                },
                {
                    "Start",
                    type date
                },
                {
                    "End",
                    type date
                }
            }
        )
in
    #"Changed Type"

4.png

Regards,
Xiaoxin Sheng

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @bleow,

Did you mean to use the current date and the range to generate a table with the start date and end date? 

If that is the case, you can take a look at the following formula:

let
    offset = 4,
    Source =
        List.Transform(
            List.Numbers(
                Date.Year(DateTime.LocalNow())
                - offset
                + 1,
                offset
            ),
            each
                Text.From(_)
                & ","
                & Text.From(#date(_, 1, 1))
                & ","
                & Text.From(#date(_, 12, 31))
        ),
    #"Converted to Table" =
        Table.FromList(
            Source,
            Splitter.SplitTextByDelimiter(","),
            null,
            null,
            ExtraValues.Error
        ),
    #"Renamed Columns" =
        Table.RenameColumns(
            #"Converted to Table",
            {
                {
                    "Column1",
                    "Year"
                },
                {
                    "Column2",
                    "Start"
                },
                {
                    "Column3",
                    "End"
                }
            }
        ),
    #"Changed Type" =
        Table.TransformColumnTypes(
            #"Renamed Columns",
            {
                {
                    "Year",
                    Int64.Type
                },
                {
                    "Start",
                    type date
                },
                {
                    "End",
                    type date
                }
            }
        )
in
    #"Changed Type"

4.png

Regards,
Xiaoxin Sheng

amitchandak
Super User
Super User

@bleow , add Date.EndOfYear , or have a column with that, that should give you end date.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

Please explain exactly the logic behind the calculations.

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.