Forum Discussion

NCPATZER's avatar
NCPATZER
Frequent Visitor
2 years ago
Solved

Transpose dates column dynamically using specific column names

I get an output daily from an SQL query with varying dates (not dependent on current date). I'd like to transpose it like the sample below, and have a dynamic naming, that the oldest date would be a ...
  • dufoq3's avatar
    2 years ago

    Hello NCPATZER,

     

    You can choose output column names in 3rd step Variant (choose day or date😞

     

    Result - Variant day (see column names)

     

    Result - Variant date (see column names)

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcktNUjDVUTAyMDJR0lFyBGJDA6VYHYiEGaqEKVzCHFXCCC6BMMoJVcIMRcISi0kgcTMsBjkDsTkWc0DiRhZYDAJLAN0aCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Item = _t, Amount = _t]),
        ChangedType = Table.TransformColumns(Source, {{"Date", each Date.FromText(_, [Format="MMM %d, yyyy", Culture="en-US"]), type date}, {"Amount", each Number.From(_), type number}}),
        // Use "day" or "date". This will infulence final result.
        Variant = "day",
        StepBack = ChangedType,
        #"Grouped Rows" = Table.Group(StepBack, {"Item"}, {{"All", each _, type table}}),
        Ad_FinalTable = Table.AddColumn(#"Grouped Rows", "Final Table", each
            List.Accumulate(
                {0..Table.RowCount([All])-1},
                #table(type table[Item=text], {{[Item]}}),
                (s,c)=> 
                    if Variant = "day" then Table.AddColumn(s, "Day " & Text.From(c+1), (x)=> [All]{c}[Amount], type number)
                    else Table.AddColumn(s, Date.ToText([All]{c}[Date], "MMM %d", "en-US"), (x)=> [All]{c}[Amount], type number)
            ), type table
        ),
        CombinedTables = Table.Combine(Ad_FinalTable[Final Table])
    in
        CombinedTables