Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

"Month Number" Formula

Hi: In Power Query Editor, I have a column showing the names of months.   How do I create a separate column showing the number for each month (i.e., "1" for "January", "2" for February, etc.)? Tha...
  • tackytechtom's avatar
    tackytechtom
    4 years ago

    Hi Anonymous ,

     

    That's a pity... since there is no M Power Query function to reverse MonthNames to Numbers... There is always the possibility for twelve if clauses I suppose, but here another one that you might consider sufficient:

     

    Create a new custom column with the following code:

     

    Date.Month ( Date.FromText ( "1 " & [MonthName] & " 2022" ) )

     

    Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMK00sqlSK1YlWcktNKoJzfBOLkjPALMeCoswcqBhEzqs0LxXKyIGIOJamlxaXgJnBqQUlqblJqUVgnn9yST6M7ZdfhpBwSU2GcmIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [MonthName = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"MonthName", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "MonthNumber", each Date.Month ( Date.FromText ( "1 " & [MonthName] & " 2022" ) ))
    in
        #"Added Custom"

     

    Does this work for you? 🙂

     

    /Tom
    https://www.tackytech.blog/
    https://www.instagram.com/tackytechtom/