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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
elJukes
Helper I
Helper I

Help creating table in Power Query

Hello

 

I have a table of student data including the following columns:

 

 

StudentID Enrolement Start Date Enrolment End Date Current/Last Year Group

000126 September 200721 July 20126
000212 September 200821 July 20125
000318 September 200721 July 20126

 

What I need to do is create a separate table that lists for every StudentID, which Year Group they were in for each year they were enroled similar to:

 

 

StudentID Year Year Group

000120126
000120115
000120104
000120093
000120082
000120071
000220125
000220114
000220103
000220092
000220081
000320126
000320115
000320104
000320093
000320082
000320071

 

I've tried googling, and going over the forums but am completely stuck. From what I've picked up I'm thinking this may be a Power Query scenario? I'm not overly familiar with Power Query however and would appreciate any help anyone could offer.

1 ACCEPTED SOLUTION
Stachu
Community Champion
Community Champion

try this code, you will need to change the first step to reference your original table (red code)

the main trick was using the list generator - e.g. {2..5} generates list from 2 to 5 (blue code)

let
    Source = Table1,
    #"Added Custom1" = Table.AddColumn(Source, "Year", each {Date.Year([Enrolement Start Date])..Date.Year([Enrolment End Date])}),
    #"Expanded Year" = Table.ExpandListColumn(#"Added Custom1", "Year"),
    #"Added Custom" = Table.AddColumn(#"Expanded Year", "Year Group", each [Year]-Date.Year([Enrolement Start Date])+1),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"StudentID", "Year", "Year Group"}),
    #"Sorted Rows" = Table.Sort(#"Removed Other Columns",{{"StudentID", Order.Ascending}, {"Year", Order.Descending}})
in
    #"Sorted Rows"


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

View solution in original post

2 REPLIES 2
Stachu
Community Champion
Community Champion

try this code, you will need to change the first step to reference your original table (red code)

the main trick was using the list generator - e.g. {2..5} generates list from 2 to 5 (blue code)

let
    Source = Table1,
    #"Added Custom1" = Table.AddColumn(Source, "Year", each {Date.Year([Enrolement Start Date])..Date.Year([Enrolment End Date])}),
    #"Expanded Year" = Table.ExpandListColumn(#"Added Custom1", "Year"),
    #"Added Custom" = Table.AddColumn(#"Expanded Year", "Year Group", each [Year]-Date.Year([Enrolement Start Date])+1),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"StudentID", "Year", "Year Group"}),
    #"Sorted Rows" = Table.Sort(#"Removed Other Columns",{{"StudentID", Order.Ascending}, {"Year", Order.Descending}})
in
    #"Sorted Rows"


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Hi Stachu

 

Thank you very much indeed! That code works excellently and taught me an awful lot about Power Query.

 

Regards

 

Sam

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

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.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.