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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
zfrank67
Frequent Visitor

Determining Generation

Hey all!

I've got a table that has employee information, including Date of Birth. Then I also have a reference table that lists each generation with a start year and end year.

zfrank67_0-1667338172884.png

 

I'd like to take the value of Date.Year([Date Of Birth) in Table1 and pull back which generation it is in. That is, see if the year value of the first table is between the upper and lower limits columns in the second table and return associated information from another column in the second table.

1 ACCEPTED SOLUTION
edhans
Super User
Super User

Hi @zfrank67 ,

 

You will either want to do this in DAX where it is very fast to filter tables between ranges, or you need to expand this table for the "lookup" so all values are listed. I started with this mockup:

edhans_0-1667341503840.png

I then added a new Year Range column with the following formula:

={[Start Year]..[End Year]}

That adds a new column where every year from start to finish is listed. Expand that to new rows:

edhans_1-1667341628522.png

You will get this, one record for every year:

edhans_2-1667341687596.png

Now just use the Merge feature of Power Query. Merge the birth year to this column, then expand the Generation column you want to display.

 

This table would only be used as a lookup. I would not bring this table into a data model without knowing more about what you are doing.

Full code here:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wck/NM1TSUTK0tDQHUkYGRkZKsTpgYSOwsAVU1gwmbAzmm5lCZA1gwiZgvglE2MxEKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Generation = _t, #"Start Year" = _t, #"End Year" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Year", Int64.Type}, {"End Year", Int64.Type}}),
    #"Added Year Range" = Table.AddColumn(#"Changed Type", "Year Range", each {[Start Year]..[End Year]}),
    #"Expanded Year Range" = Table.ExpandListColumn(#"Added Year Range", "Year Range")
in
    #"Expanded Year Range"

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

3 REPLIES 3
zfrank67
Frequent Visitor

@edhans Took me a few days to be able to test, but this worked perfectly. Thank you!

Great @zfrank67 - glad I was able to assist.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
edhans
Super User
Super User

Hi @zfrank67 ,

 

You will either want to do this in DAX where it is very fast to filter tables between ranges, or you need to expand this table for the "lookup" so all values are listed. I started with this mockup:

edhans_0-1667341503840.png

I then added a new Year Range column with the following formula:

={[Start Year]..[End Year]}

That adds a new column where every year from start to finish is listed. Expand that to new rows:

edhans_1-1667341628522.png

You will get this, one record for every year:

edhans_2-1667341687596.png

Now just use the Merge feature of Power Query. Merge the birth year to this column, then expand the Generation column you want to display.

 

This table would only be used as a lookup. I would not bring this table into a data model without knowing more about what you are doing.

Full code here:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wck/NM1TSUTK0tDQHUkYGRkZKsTpgYSOwsAVU1gwmbAzmm5lCZA1gwiZgvglE2MxEKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Generation = _t, #"Start Year" = _t, #"End Year" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Year", Int64.Type}, {"End Year", Int64.Type}}),
    #"Added Year Range" = Table.AddColumn(#"Changed Type", "Year Range", each {[Start Year]..[End Year]}),
    #"Expanded Year Range" = Table.ExpandListColumn(#"Added Year Range", "Year Range")
in
    #"Expanded Year Range"

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors