Forum Discussion
Rename Columns based on end of month using M code
- 6 years ago
Try Below Code. One Important thing, dynamic value "FY" will be always picked based on the first row of Source.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci4tKkrNS65U0lFyiwQSXol5QYklqUqxOtFKocEuYGFDSyBloGdoZGxiamZuYakUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t]), #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]), #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Currency", type text}, {"JanRate", type number}}), FY = Text.End(#"Changed Type"{0}[FY],2), Custom1 = Date.ToText(Date.EndOfMonth(#date(Int64.From(Text.Combine({"20" & FY})),1,1)),"YYYY-MM-DD"), Custom2 = Table.RenameColumns(#"Changed Type",{"JanRate",Custom1}) in Custom2
Worked great, thank you.
I was also able to do it by adding a decimal format parameter in but obviously involves manual intervention come the year change
#"Replaced Value" = Table.ReplaceValue(Sheet1_Sheet,"JanRate",Date.ToText(Date.EndOfMonth(#date(Year, 1, 1)),"YYYY-MM-DD"),Replacer.ReplaceValue,{"Column4"})Great, Awesome. Thanks for this question, had a good learning :)
- lgs19836 years agoHelper I
AnkitBI sorry to be a pain but how would I structure the code to do it for Feb and Mar too for example?
- AnkitBI6 years agoSolution Sage
Share sample input and expected result . The code I shared has Initial ColumnName and Month 01 in #date HardCoded. We will need to make it dynamic.
- lgs19836 years agoHelper I
So is similar as before really,
EXISTING
Currency FY JanRate FebRate MarRate AprRate
USD FY19 0.123456789 0.123456789 0.123456789 0.123456789
DESIRED OUTCOME
Currency FY 2019-01-31 2019-02-28 2019-03-31 2019-04-30
USD FY19 0.123456789 0.123456789 0.123456789 0.123456789