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.)?

Thanks!

John

  • 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/

5 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Unfortunately, I do not.

    • tackytechtom's avatar
      tackytechtom
      Icon for Most Valuable Professional rankMost Valuable Professional

      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/

  • v-henryk-mstf's avatar
    v-henryk-mstf
    Icon for Community Support rankCommunity Support

    Hi Anonymous ,

     

    Whether the advice given by tackytechtom  has solved your confusion, if the problem has been solved you can mark the reply for the standard answer to help the other members find it more quickly. If not, please point it out.


    Looking forward to your feedback.


    Best Regards,
    Henry

  • How would i amend this to continue on for additional years (eg count to 13 14 and so on for jan, feb the following year)