Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Conditional Column to have value '1' or '0' as per the condition given below

 

MonthYearAggregate
October201341
October201345
October201333
October201325
November201350
November201322
November201353
November201344
December201341
December201339
December201335
December201341
January201456
January201459
January201441
January201440
February201471
February201437
February201433
February201464

 

Hi all,

Above is the small part of the main table I am working on. I need a new column which gives me a '1' if [Aggregate] column's average for that month and year is greater than the [Aggregate] value of that row, else a '0'.

Could anyone please help me solve this problem?

 

  • Group by Month and Year taking the average over [Aggregate], merge this back with the original table, then write a custom column to compare the [Aggregate] and the average.

    Here's a full sample query you can paste into the Advanced Editor of a new blank query:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fdCxCoAgEMbxd3FuSO9M2qOhoR4gGiwcK5AKevssp8KvSfj/9JDre9FN2zo6LzKhcknhYCmGLNV1uhOlu4r32/Vw8wt0DkAp9IIAMD9QuekLEgCVCPT/qMYuu/Vn7Hx/qgC9THc0h+M6ajf6NxgJgAwCAlCEPQ0X", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Year = _t, Aggregate = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", type text}, {"Year", Int64.Type}, {"Aggregate", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Month", "Year"}, {{"Avg", each List.Average([Aggregate]), type nullable number}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Month", "Year"}, #"Grouped Rows", {"Month", "Year"}, "Grouped Rows", JoinKind.LeftOuter),
        #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Avg"}, {"Avg"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Grouped Rows", "Custom", each if [Avg] > [Aggregate] then 1 else 0, Int64.Type)
    in
        #"Added Custom"

4 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      Done that. Thanks

  • Group by Month and Year taking the average over [Aggregate], merge this back with the original table, then write a custom column to compare the [Aggregate] and the average.

    Here's a full sample query you can paste into the Advanced Editor of a new blank query:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fdCxCoAgEMbxd3FuSO9M2qOhoR4gGiwcK5AKevssp8KvSfj/9JDre9FN2zo6LzKhcknhYCmGLNV1uhOlu4r32/Vw8wt0DkAp9IIAMD9QuekLEgCVCPT/qMYuu/Vn7Hx/qgC9THc0h+M6ajf6NxgJgAwCAlCEPQ0X", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Year = _t, Aggregate = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", type text}, {"Year", Int64.Type}, {"Aggregate", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Month", "Year"}, {{"Avg", each List.Average([Aggregate]), type nullable number}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Month", "Year"}, #"Grouped Rows", {"Month", "Year"}, "Grouped Rows", JoinKind.LeftOuter),
        #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Avg"}, {"Avg"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Grouped Rows", "Custom", each if [Avg] > [Aggregate] then 1 else 0, Int64.Type)
    in
        #"Added Custom"
    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you Alexis. It worked.