Forum Discussion

metricwise's avatar
metricwise
Regular Visitor
6 years ago
Solved

Create two columns together for MTD/YTD use case

Hello,   I am trying to create two columns - "Dates", "Tag" such that "Dates" contains all the dates that fall in MTD and then the corresponding rows to that should be tagged as "MTD".   I tried ...
  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi metricwise ,

    You could Add a Custom column using like this:

    The full code in Advanced Editor:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VdDLCcAwDAPQXXIuyHL+s4Tsv0ZTSrF6fMgG2WulDsLNLe1rpQEPNGRNiqIGKpomPVAwNJmBDFqIoHRwUEpM8GnB+U4aWIRnswYH2EIN7DIKjtA5euoFbtrN/x9xberfT/YN", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type date}}),
        #"Added Custom1" = Table.AddColumn(#"Changed Type", "QTD", each if Date.IsInCurrentQuarter([Value]) then "QTD" else null),
        #"Added Custom2" = Table.AddColumn(#"Added Custom1", "YTD", each if Date.IsInCurrentYear([Value]) then "YTD" else null),
        #"Added Custom" = Table.AddColumn(#"Added Custom2", "MTD", each if Date.IsInCurrentMonth([Value]) then "MTD" else null),
        #"Sorted Rows" = Table.Sort(#"Added Custom",{{"Value", Order.Descending}}),
        #"Reordered Columns" = Table.ReorderColumns(#"Sorted Rows",{"Value", "MTD", "QTD", "YTD"})
    in
        #"Reordered Columns"

    Then the final table will look like this:

    Best regards,

    Eyelyn Qin