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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
kalkhudary
Helper IV
Helper IV

Column index based on year

Hello Community, 

 

I am trying to create an index column that starts from 1, 2, ... based on my date column where the index column will show 1 for all dates in 2017  and 2 for all dates in 2018, etc... Can someone help guide me step by step how to reach to this result?

 

Currently I can created an index column in the transform and it shows each row as a number but need now to show all dates in 2017 as 1 and all in 2018 as 2 and all in 2019 as 3 etc...

Column Index Group by Year.PNG

1 ACCEPTED SOLUTION
HotChilli
Community Champion
Community Champion

Get the year from the Date column using Date.Year()  and subtract 2016

View solution in original post

5 REPLIES 5
kalkhudary
Helper IV
Helper IV

@jbwtp @HotChilli @PhilipTreacy  Thanks all for this tremendous support. All syntax worked.

PhilipTreacy
Super User
Super User

Hi @kalkhudary 

 

Just chipping in my 2c as I can do it in less steps 🙂

 

As @HotChilli describes you just need the year - 2016.  Whilst @jbwtp 's code gives the result you want, you just need to add a single custom column

 

#"Added Custom" = Table.AddColumn(#"Changed Type", "Index", each Date.Year([Date]) - 2016)

 

 

Full code here 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc7bCcQwEEPRXvwdkGb8iF2LSf9tLBvL8XweBBfNmRzMcNqdnmsmA+tRJtgOfYA9sMMYeMM8sIIv+2IDS2BVWSwqi1ll0VUWqfJYNJXHt7ZD25/F/Vncn0V9dn4sgWWV/3x+", 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 Custom" = Table.AddColumn(#"Changed Type", "Index", each Date.Year([Date]) - 2016)
in
    #"Added Custom"

 

 

yearindex.png

 

I'd feel bad if you mark this as the solution, as I'm just expanding on Hot Chili's answer. 

 

Regards

 

Phil

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


kalkhudary
Helper IV
Helper IV

@HotChilli Can you help show and tell the steps? 

HotChilli
Community Champion
Community Champion

Get the year from the Date column using Date.Year()  and subtract 2016

Or more generalised:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc7bCcQwEEPRXvwdkGb8iF2LSf9tLBvL8XweBBfNmRzMcNqdnmsmA+tRJtgOfYA9sMMYeMM8sIIv+2IDS2BVWSwqi1ll0VUWqfJYNJXHt7ZD25/F/Vncn0V9dn4sgWWV/3x+", 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}}),
    #"Inserted Year" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Date]), Int64.Type),
    #"Grouped Rows" = Table.Group(#"Inserted Year", {"Year"}, {{"Data", each _, Value.Type(#"Changed Type")}}),
    #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1, Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Added Index",{"Year"}),
    #"Expanded Data" = Table.ExpandTableColumn(#"Removed Columns", "Data", {"Date"}, {"Date"})
in
    #"Expanded Data"

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.