Forum Discussion

kubouch's avatar
kubouch
New Member
3 years ago
Solved

Custom column - Difference between columns in the current and another query

I have a simple problem: I need to create a custom column that is calculated as a difference between a column from another query and a current query.   In the Custom Column menu formula, I can do ...
  • jbwtp's avatar
    jbwtp
    3 years ago

    This is not all that gloom :), you can use a function to template repeating actions.

    Something like this:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlKK1YlWMgaTJmDSFEyaKcXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [foo = _t]),
        main = Table.TransformColumnTypes(Source,{{"foo", type text}}),
        a1 = Table.TransformColumnTypes(Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmDSBEyaKsXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [score = _t]),{{"score", Int64.Type}}),
        a2 = Table.TransformColumnTypes(Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmDSBEyagkkzpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [score = _t]),{{"score", Int64.Type}}),
        a3 = Table.TransformColumnTypes(Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmDSRCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [score = _t]),{{"score", Int64.Type}}),
        
        tablesToAdd = {a1, a2, a3},
        process = List.Accumulate(tablesToAdd, Table.ToColumns(main), (a, n)=> a & {n[score]}),
        columnNames = Table.ColumnNames(main) & List.Transform({1..List.Count(tablesToAdd)}, each Number.ToText(_, "score#")),
        Custom1 = Table.FromColumns(process, columnNames)
    
    in
        Custom1

     

    Just add your tables to the list (tablesToAdd) and sbstitie main with the "foo" table.

     

    Cheers,

    John