Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Kishore_SR
Frequent Visitor

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 

ConsumerProductOverall_LikingTaste_LikingLiking of ProductLiking_Ratings
1Cookie9101
2Cookie8212
3Cookie7303
4Cookie641 4
5Cookie5505
6Cookie4615
7Cookie3704
8Cookie2813
9Cookie1902

 

Now I have Columns with key words "Liking" in it and values in Overall_Liking, Taste_Liking  are in the scale of 1 to 9, Liking of Product are in the scale of 0,1 and Liking_Ratings are in the scale of 1 to 5

 

What I want here is to capture the columns with Key Word "Liking" and values in the scale of 1 to 9 and remove the other columns, will it be possible to do this in Power Query!?

 

If so please help, Thanks in Advance!!

 

 

 

1 ACCEPTED SOLUTION
AntrikshSharma
Super User
Super User

@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

 

View solution in original post

4 REPLIES 4
wdx223_Daniel
Super User
Super User

= Table.SelectColumns(Source,List.Select(Table.ColumnNames(PreviousStepName),each not Text.Contains(_,"Liking") or List.RemoveItems(Table.Column(PreviousStepName,_),{1..9})={}))

Kishore_SR
Frequent Visitor

Hi @AntrikshSharma 

 

Your Method 2 works great for me, Thank you for your help.

 

One concern for me is, instead of Max = 9 can we set it as greater than 5..!! because sometimes there might not be no 9 ratings given

 

Is there a work around for this!?

 

 

 

@Kishore_SR Then you can do 

Check = if Min = 1 and Max > 5 then true else false
AntrikshSharma
Super User
Super User

@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

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.