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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Strange Filter Situation Power Query

I admit I'm a little lost with where to begin with something I'm trying to do in power query.

 

I made up some dummy data to imitate the problem I'm trying to solve. 

 

My boss wants me to only keep rows like the following:

- Insect Description is Mantis

- Insect description is Moth

- Insect Description is Caterpillar AND Caterpillar is the only Insect Description for that Insect ID AND there is a date in the Finish column on that row (for example, see Insect ID 5, 8, 15, etc)

 

To make it more clear which rows my boss would want me to keep, I have made a new column "Want to Keep This Row" and manually put an x in the rows my boss would want. However, the original data set is hundreds of rows long and I'm wondering if there would be a more feasible way than for me to manually add an x.

 

I know how to write m code to filter for the Moth and Mantis Insect Description, but I'm struggling with isolating the Caterpillar rows. This is because my boss only wants the caterpillar rows where there is a finish date and also where caterpillar is the only insect description for the insect ID. For example he wouldn't want the caterpillar row for insect id 9 because that same insect ID also has a cocoon row, nor would he want the caterpillar rows on insect IDs that also have moth rows--but he does want all of the moth rows!

 

Does this description make sense? What would you advise?

 

 

 

Insect IDInsect DescriptionStartFinishInsect ValueWant to Keep this Row
1Caterpillar04/06/202104/13/2021 $        2,692.00 
1Cocoon04/13/202104/13/2021 $        2,692.00 
1Moth04/13/202104/13/2021 $        2,692.00x
2Caterpillar02/04/202102/06/2021 $      17,127.00 
2Cocoon02/06/202102/06/2021 $      17,127.00 
2Moth02/06/202102/06/2021 $      17,127.00x
3Mantis10/26/202111/01/2021 $          800.00x
4Mantis9/1/202110/15/2021 $         900.00x
5Caterpillar06/08/202106/09/2021 $      15,474.00x
6Caterpillar06/15/2021  $      28,760.00 
7Caterpillar03/09/202103/11/2021 $        1,426.00x
8Caterpillar06/24/202106/30/2021 $        4,063.00x
9Caterpillar06/16/202106/20/2021 $        5,617.00 
9Cocoon06/20/2021  $        5,617.00 
10Mantis3/5/20215/1/2021 $      1,000.00x
11Mantis4/28/20215/15/2021 $    11,000.00x
12Mantis6/14/20217/30/2021 $          50.00x
13Mantis8/21/202110/1/2021 $          80.00x
14Mantis7/29/20218/14/2021 $      1,000.00x
15Caterpillar04/05/202104/08/2021 $        1,726.00x
16Caterpillar01/25/202102/01/2021 $      29,966.00 
16Cocoon02/01/202102/01/2021 $      29,966.00 
16Moth02/01/2021  $      29,966.00
17Mantis2/15/20213/4/2021 $         900.00x
18Caterpillar01/22/202101/27/2021 $      18,532.00x
19Caterpillar03/23/202103/24/2021 $        2,800.00 
19Cocoon03/24/202103/24/2021 $        2,800.00 
19Moth03/24/202103/24/2021 $        2,800.00x
20Caterpillar08/14/202108/21/2021 $      15,654.00 
20Cocoon08/21/202108/21/2021 $      15,654.00 
20Moth08/21/202108/21/2021 $      15,654.00x
1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZVNagMxDIWvMgxdmlqS/9dd9wQhi1AKDYSkpFnk+FWaGSPFbsHNKgp8z3rys7LZzDib+WV3eT9/7g+H3Zkr8BaiJSC8F+jWYnqalg+ZWOgZ4PbjvDWLzOntdDo+QiMKr6fLxzh//eGpNUKW4VWGhKtVBpNBSrILUj4kMyCw2hjC7y7cDd8dL/sv/oJgqTKIFrCdwzRlAKngpUKxFWExDB2+aDy0Y4wWcrXBRWlsBOOTlyqxpyKOFzBlkyLIEaYWduJULrAzCDSeouwh93ogL5w4aGW8geikTOlaiUKGOjLBRFS5KCpYCvobRJAX6mwdYrDNFNCAvk1ECbP3LOiHNGBLk6TZdR1e6s6Ou9e8SjMfrcLYz7LiVZaTpZqBLHr53Xwny7zbqu1bkXtBSjpI2Ekzdx/k027cUDElRnWR8XG34H8UxHLB7nsS2HXhkpwjiZt3thljuxKw85L4aKrdc5Ga28gmOLWgsfOSeLE78a6p0w2ZuuAmoVPnKKkhiXWQYwLLnw20XmQmQYZdrMkYvPqzAGVFQiMKq5Mhno1svwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Insect ID" = _t, #"Insect Description" = _t, Start = _t, Finish = _t, #"Insect Value" = _t, #"Want to Keep this Row" = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Single", each let s=Table.SelectRows(Source,(k)=>[Insect ID]=k[Insect ID]) in Table.RowCount(s)=1 and s[Insect Description]{0}="Caterpillar"),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each [Insect Description] <> "Cocoon" and [Finish] <> " " and ([Insect Description] <> "Caterpillar" or [Single] = true))
in
    #"Filtered Rows"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZVNagMxDIWvMgxdmlqS/9dd9wQhi1AKDYSkpFnk+FWaGSPFbsHNKgp8z3rys7LZzDib+WV3eT9/7g+H3Zkr8BaiJSC8F+jWYnqalg+ZWOgZ4PbjvDWLzOntdDo+QiMKr6fLxzh//eGpNUKW4VWGhKtVBpNBSrILUj4kMyCw2hjC7y7cDd8dL/sv/oJgqTKIFrCdwzRlAKngpUKxFWExDB2+aDy0Y4wWcrXBRWlsBOOTlyqxpyKOFzBlkyLIEaYWduJULrAzCDSeouwh93ogL5w4aGW8geikTOlaiUKGOjLBRFS5KCpYCvobRJAX6mwdYrDNFNCAvk1ECbP3LOiHNGBLk6TZdR1e6s6Ou9e8SjMfrcLYz7LiVZaTpZqBLHr53Xwny7zbqu1bkXtBSjpI2Ekzdx/k027cUDElRnWR8XG34H8UxHLB7nsS2HXhkpwjiZt3thljuxKw85L4aKrdc5Ga28gmOLWgsfOSeLE78a6p0w2ZuuAmoVPnKKkhiXWQYwLLnw20XmQmQYZdrMkYvPqzAGVFQiMKq5Mhno1svwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Insect ID" = _t, #"Insect Description" = _t, Start = _t, Finish = _t, #"Insect Value" = _t, #"Want to Keep this Row" = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Single", each let s=Table.SelectRows(Source,(k)=>[Insect ID]=k[Insect ID]) in Table.RowCount(s)=1 and s[Insect Description]{0}="Caterpillar"),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each [Insect Description] <> "Cocoon" and [Finish] <> " " and ([Insect Description] <> "Caterpillar" or [Single] = true))
in
    #"Filtered Rows"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

Anonymous
Not applicable

Thank you so much for your fast response! In my case, I am using power query with an Excel workbook and it looks like you are referencing a JSON object. Would I add your code like below if I wanted to use it with my Excel table?

 

let
    Source = 
          Table.FromRows(
               Excel.CurrentWorkbook(){[Name="Table1"]}[Content], 
               let _t = ((type nullable text) meta [Serialized.Text = true]) in type table 
              [#"Insect ID" = _t, 
                    #"Insect Description" = _t, 
                    Start = _t, 
                    Finish = _t, 
                    #"Insect Value" = _t, 
                    #"Want to Keep this Row" = _t]
          ),
    #"Added Custom" = 
        Table.AddColumn(
            Source, 
            "Single", 
            each 
                let s=Table.SelectRows(
                    Source,
                    (k)=>[Insect ID]=k[Insect ID]
                ) in Table.RowCount(s)=1 
                    and s[Insect Description]{0}="Caterpillar"
            ),
    #"Filtered Rows" = 
        Table.SelectRows(
            #"Added Custom", 
                each 
                    [Insect Description] <> "Cocoon" and 
                        [Finish] <> " " and 
                        ([Insect Description] <> "Caterpillar" 
                            or [Single] = true
                        )
            )
in
    #"Filtered Rows"

 

I did notice that I got the below error when I used this code. What would you recommend?

"Expression.Error: We cannot convert a value of type Table to type List.
Details:
Value=[Table]
Type=[Type]"

 

When I asked it to show me the error, it showed me the Source step.

Use your excel table as the source and then manually add the other two transforms.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors