cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
null
Helper V
Helper V

add a column use M code to get the month

Hi, I am a fresh man use M code and I want to create a column based on this column, the current month number is 05, so when the value is 202205 and I want get a new column with value "M", the M should be current month, and previous month in the exist column is 202204,  I want to get a value"M-1" in the new column,  and if it has other month the value will be null, so how can I get a new column with M code?

Thank you.

null_0-1653019489758.png

 

1 ACCEPTED SOLUTION
Vijay_A_Verma
Community Champion
Community Champion

In a custom column use following formula where [Data] column has dates

= if Date.ToText(Date.From(DateTime.FixedLocalNow()),"yyyyMM")=Text.From([Data]) then "M"
else if Date.ToText(Date.AddMonths(Date.From(DateTime.FixedLocalNow()),-1),"yyyyMM")=Text.From([Data]) then "M-1" else null

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMjIwVorVgTJNEExTBNMMwTRHMC2UYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Data = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if Date.ToText(Date.From(DateTime.FixedLocalNow()),"yyyyMM")=Text.From([Data]) then "M"
else if Date.ToText(Date.AddMonths(Date.From(DateTime.FixedLocalNow()),-1),"yyyyMM")=Text.From([Data]) then "M-1" else null)
in
    #"Added Custom"

View solution in original post

6 REPLIES 6
Vijay_A_Verma
Community Champion
Community Champion

Difficult to understand your requirement. Can you please post sample output which you need?

today=20220520, and column A has value like 202203, 202205, 202204..

I want get a new column that based current month and column A, if column A=202205 then new column="M"

if column A=202204 then new column="M-1";

and if this month is june, 

if column A=202206 then new column="M"

if column A=202205 then new column="M-1";

I have problem with how to month 5 into "05" to match with column A and I want to learn if  your have any good ieads, Thank you

Vijay_A_Verma
Community Champion
Community Champion

Thanks for clarity. 

So if this is May, then what would be value for 202203? Will it be M-2?

What will be value for 202207 - Will it be M+2?

logicallly it is right, but I just need this month and previous month, so there is only two actual value M and M-1, others could set null

Vijay_A_Verma
Community Champion
Community Champion

In a custom column use following formula where [Data] column has dates

= if Date.ToText(Date.From(DateTime.FixedLocalNow()),"yyyyMM")=Text.From([Data]) then "M"
else if Date.ToText(Date.AddMonths(Date.From(DateTime.FixedLocalNow()),-1),"yyyyMM")=Text.From([Data]) then "M-1" else null

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMjIwVorVgTJNEExTBNMMwTRHMC2UYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Data = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if Date.ToText(Date.From(DateTime.FixedLocalNow()),"yyyyMM")=Text.From([Data]) then "M"
else if Date.ToText(Date.AddMonths(Date.From(DateTime.FixedLocalNow()),-1),"yyyyMM")=Text.From([Data]) then "M-1" else null)
in
    #"Added Custom"

Very nice, Thank you.

Helpful resources

Announcements
August 2022 update 768x460.jpg

Check it Out!

Click here to learn more about the August 2022 updates!

August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors