Forum Discussion

Kishore_SR's avatar
Kishore_SR
Frequent Visitor
3 years ago
Solved

Capture Columns based on Key words in Column Headers and also the values with from 1 to 9

Hi all,   I have pasted sample data here, this is only for reference  Consumer Product Overall_Liking Taste_Liking Liking of Product Liking_Ratings 1 Cookie 9 1 0 1 2 Cookie 8...
  • AntrikshSharma's avatar
    3 years ago

    Kishore_SR Here are 2 methods:

    with List.Accumulate:

    let
        Source = Table.FromRows (
            Json.Document (
                Binary.Decompress (
                    Binary.FromText (
                        "Tc89DsAgCAXgqzTOHVDxb+4xjGOHpoP334r0xTC8YPzQQO/Ou9Ndc77PLYcmWRekdZzdBctVEtASlKPlIol4HZXZcpYwXh+snqwnhLQuzpYZX/jNxXLEAKSti6vlgPH9nq1Z9lie/s3GBw==",
                        BinaryEncoding.Base64
                    ),
                    Compression.Deflate
                )
            ),
            let
                _t = ( ( type nullable text ) meta [ Serialized.Text = true ] )
            in
                type table [
                    Consumer = _t,
                    Product = _t,
                    Overall_Liking = _t,
                    Taste_Liking = _t,
                    #"Liking of Product" = _t,
                    Liking_Ratings = _t
                ]
        ),
        ChangedType = Table.TransformColumnTypes (
            Source,
            {
                { "Consumer", Int64.Type },
                { "Product", type text },
                { "Overall_Liking", Int64.Type },
                { "Taste_Liking", Int64.Type },
                { "Liking of Product", Int64.Type },
                { "Liking_Ratings", Int64.Type }
            }
        ),
        ColumnNames = List.Select (
            Table.ColumnNames ( ChangedType ),
            each Text.Contains ( Text.Lower ( _ ), "liking" )
        ),
        Transformation = List.Accumulate (
            ColumnNames,
            ChangedType,
            ( State, Current ) =>
                let
                    Col = Table.Column ( State, Current ),
                    Min = List.Min ( Col ),
                    Max = List.Max ( Col ),
                    Check =
                        if Min = 1 and Max = 9 then
                            State
                        else
                            Table.RemoveColumns ( State, Current )
                in
                    Check
        )
    in
        Transformation

     Method 2:

    let
        Source = Table.FromRows (
            Json.Document (
                Binary.Decompress (
                    Binary.FromText (
                        "Tc89DsAgCAXgqzTOHVDxb+4xjGOHpoP334r0xTC8YPzQQO/Ou9Ndc77PLYcmWRekdZzdBctVEtASlKPlIol4HZXZcpYwXh+snqwnhLQuzpYZX/jNxXLEAKSti6vlgPH9nq1Z9lie/s3GBw==",
                        BinaryEncoding.Base64
                    ),
                    Compression.Deflate
                )
            ),
            let
                _t = ( ( type nullable text ) meta [ Serialized.Text = true ] )
            in
                type table [
                    Consumer = _t,
                    Product = _t,
                    Overall_Liking = _t,
                    Taste_Liking = _t,
                    #"Liking of Product" = _t,
                    Liking_Ratings = _t
                ]
        ),
        ChangedType = Table.TransformColumnTypes (
            Source,
            {
                { "Consumer", Int64.Type },
                { "Product", type text },
                { "Overall_Liking", Int64.Type },
                { "Taste_Liking", Int64.Type },
                { "Liking of Product", Int64.Type },
                { "Liking_Ratings", Int64.Type }
            }
        ),
        LikingsColumns = List.Select (
            Table.ColumnNames ( ChangedType ),
            each Text.Contains ( Text.Lower ( _ ), "liking" )
        ),
        NonLikingsColumns = List.Select (
            Table.ColumnNames ( ChangedType ),
            each not Text.Contains ( Text.Lower ( _ ), "liking" )
        ),
        ColumnsToKeep = List.Select (
            LikingsColumns,
            ( ColName ) =>
                let
                    ColList = Table.Column ( ChangedType, ColName ),
                    Min     = List.Min ( ColList ),
                    Max     = List.Max ( ColList ),
                    Check   = if Min = 1 and Max = 9 then true else false
                in
                    Check
        ),
        Result = Table.SelectColumns ( ChangedType, NonLikingsColumns & ColumnsToKeep )
    in
        Result