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
PPalkowski
Helper II
Helper II

Trying to get decade from year

I am new to power query, couldn't find an easy solution

I am trying to get the decade from the year,  slowly built this

 

Added column: This takes a year and converts it to text

= Table.AddColumn(#"Renamed Columns", "Y1", each Number.ToText ([Year]))

 

Added column: This take the first two characters at this point, 19 or 20

= Table.AddColumn(#"Added Custom", "Y2", each Text.Start([Y1], 2))

 

Added column: takes the 3rd character, and adds an 0s

= Table.AddColumn(#"Added Custom2", "Y4", each [Y2] & Text.At ([Y1], 2)&"0s")

 

The result is 1920s, 1930s, 1940s, etc…

Is there a way to do this in one step?

1 ACCEPTED SOLUTION
solexecution
Frequent Visitor

Hi, try pasting below to your blank Advanced Query editor and see if it does what you are after, line below the comment is I believe what you are after.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMlCK1QExDE1hDBMYAyZlYAxmGFpaWkIZFqZQhgmQEQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}}),
    // Below is all in one line, hope that helps
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Start(Text.From([Year]),3)&"0s")
in
    #"Added Custom"

View solution in original post

5 REPLIES 5
solexecution
Frequent Visitor

Hi, try pasting below to your blank Advanced Query editor and see if it does what you are after, line below the comment is I believe what you are after.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMlCK1QExDE1hDBMYAyZlYAxmGFpaWkIZFqZQhgmQEQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}}),
    // Below is all in one line, hope that helps
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Start(Text.From([Year]),3)&"0s")
in
    #"Added Custom"

Prefect... now I need to break this down and see what you did so I can learn from it

@PPalkowski  you could also use mod10 for probably less pqengine effort:
 [Year] - Number.Mod( [Year], 10 )




Feel free to connect with me:
LinkedIn

camargos88
Community Champion
Community Champion

Hi @PPalkowski .

 

Try this one:

Text.Start(Date.ToText([Column1], "yyyy"), 3) & "0s"

 

Capture.PNG



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



I get an error, I beleive because my year is a number

 

PPalkowski_0-1593630505920.png

 

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