Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Custom column with ifs

I have another trouble with power query, the trouble is this i have 8 columns that display "OK", "REFRESH" or  "TEMPLATE" each, depending on an analysis they make, but i have to add another column th...
  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi Anonymous 

     

    In PQ you can do something like this:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8vdW0lEKcnULcg32ALJCXH0DfBxDXIFMsAyYwCIYqxONXRdCEF0rmgyMGxsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [PUESTO = _t, ID = _t, JI = _t, CT = _t, UP = _t, TAM = _t, REGION = _t, LOC = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"PUESTO", type text}, {"ID", type text}, {"JI", type text}, {"CT", type text}, {"UP", type text}, {"TAM", type text}, {"REGION", type text}, {"LOC", type text}}),
        
        Headers = List.Buffer(Table.ColumnNames(#"Changed Type")),
        fCombine = (L as list, Filter as text) => Text.Combine(List.Zip(List.Select(L, (n) => n{1} = Filter)){0}, ","),
        fFilter = (r)=> 
            let 
                List = List.Buffer(List.Zip({Headers} & {Record.ToList(r)})), 
                Ok = fCombine(List, "OK"), 
                Template = fCombine(List, "TEMPLATE"),
                Refresh = fCombine(List, "REFRESH"),
                Output = Table.FromColumns({{Ok}, {Template}, {Refresh}}, {"Ok", "Template", "Refresh"}) 
            in Output,
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each fFilter(_)),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Ok", "Template", "Refresh"}, {"Ok", "Template", "Refresh"})
    in
        #"Expanded Custom"

     

    Kind regards,

    JB

  • Mariusz's avatar
    Mariusz
    6 years ago

    Hi Anonymous 

     

    Both Greg_Deckler  and Anonymous  are very good working solutions, not trying to say my one will be any better but it is an alternative, please see the below.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8vdW0lEKcnULcg32ALJCXH0DfBxDXIFMsAyYwCIYqxONXRdCEF0rmgyMGxsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [PUESTO = _t, ID = _t, JI = _t, CT = _t, UP = _t, TAM = _t, REGION = _t, LOC = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"PUESTO", type text}, {"ID", type text}, {"JI", type text}, {"CT", type text}, {"UP", type text}, {"TAM", type text}, {"REGION", type text}, {"LOC", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 
        let 
            recordToTable = Record.ToTable( _ ),
            groupedRows = Table.Group( recordToTable, {"Value"}, {{"Columns", each _[Name], type list}}),
            extractedValues = Table.TransformColumns( groupedRows, {"Columns", each Text.Combine( List.Transform(_, Text.From), ", "), type text}),
            pivotedColumn = Table.Pivot( extractedValues, List.Distinct( extractedValues[Value] ), "Value", "Columns")
        in 
            pivotedColumn, type table ),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"OK", "REFRESH", "TEMPLATE"}, {"OK", "REFRESH", "TEMPLATE"})
    in
        #"Expanded Custom"

     

    Best Regards,
    Mariusz

    If this post helps, then please consider Accepting it as the solution.