How to get first n numbers in Power Query

chuck DM 81 Reputation points
2021-03-17T03:21:01.717+00:00

I have a column that has values like 202012, 202011,202101, etc. I want to get 1st 4 numbers of each value. The output would be something like 2020,2020,2021 etc,. How to write in Power Query?

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,764 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lz._ 8,991 Reputation points
    2021-03-17T05:10:44.147+00:00

    Hi @chuck DM

    let  
        // Table for demo.  
        Source = Table.FromRecords(  
            { [Value = 202012], [Value = 202102], [Value = 201908], [Value = 202003] },  
            type table [Value = Int64.Type]  
        ),  
        //  
        FirstFourDig = Table.AddColumn(Source, "First 4 digits",  
            each Number.From(  
                    Text.Start(  
                        Number.ToText([Value]),  
                        4  
                    )  
                 ), Int64.Type  
        )  
    in  
        FirstFourDig  
    
    0 comments No comments