Forum Discussion

lgs1983's avatar
lgs1983
Helper I
5 years ago
Solved

M Code - Custom Column Headache

Hi All,   What I can't get my head around is this particular problem. Child Parent Sweden Ind Full Structure Ind Amazon UK Amazon.com 0 1 Amazon Sweden Amazon.com 1 1 Amazon P...
  • Jimmy801's avatar
    5 years ago

    Hello lgs1983 

     

    you didn't describe the logic why Full Ind is 1 when in the column are also 0. I tried to reproduce your table and the logic is to group by Parent, and then add a new column to the grouped table, that is using the first item in the Ind-column other than 0.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsxNrMrPUwj1VtKBsvWS83OBHAOlWB24dHB5akpqHroSQ2QlAYlFiemliZVYzIkFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Child = _t, Parent = _t, #"Sweden Ind" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Child", type text}, {"Parent", type text}, {"Sweden Ind", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Parent"}, {{"AllRows", each _, type table [Child=text, Parent=text, Sweden Ind=number]}}),
        FullID = Table.TransformColumns
        (
            #"Grouped Rows",
            {
                {
                    "AllRows",
                    (tbl)=> Table.AddColumn(tbl, "FullStructure", (add)=> try List.Select(tbl[Sweden Ind], each _ <> 0){0} otherwise null)
                }
            }
        ),
        #"Expanded AllRows" = Table.ExpandTableColumn(FullID, "AllRows", {"Child", "Sweden Ind", "FullStructure"}, {"Child", "Sweden Ind", "FullStructure"})
    in
        #"Expanded AllRows"

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy