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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Cannot convert Type to Type List

Hello,

 

I want to remove all the records from a table called F_SOURCE_DAILY except the most recent date and the 4 following dates (in total 5 consequal dates)

The only field that indicates the date is the field FK_DATE. The field FK_DATE is Whole Number and it's type should not be changed

Example:

 

Filtered_Rows_01.jpg

In this example is the most recent date 20200820 (August 20, 2020).
In this case I want to keep the records from 20200816 till 20200820   (August 16, 2020 till August 20, 2020)

Result:

Filtered_Rows_04.jpg

Remark that 20200815 is not included because it is not in {20200820, 20200819, 20200818, 20200817, 20200816,}

I want to apply a Filtered Rows step to do this.

To find the most recent date in the tabel, I could use in the Power Query the functions List.Max() or List.MaxN()

I tried this expression:

= Table.SelectRows(F_SOURCE_DAILY,

each (#date(

Int32.From(Text.Middle(Text.From([FK_DATE]),0,4)),
Int32.From(Text.Middle(Text.From([FK_DATE]),4,2)),
Int32.From(Text.Middle(Text.From([FK_DATE]),6,2)))

>= Date.AddDays(

#date(Int32.From(Text.Middle(Text.From(List.Max([FK_DATE])),0,4)),
          Int32.From(Text.Middle(Text.From(List.Max([FK_DATE])),4,2)),
          Int32.From(Text.Middle(Text.From(List.Max([FK_DATE])),6,2))), -4)))

 

I get an error:

Filtered_Rows_03.jpg

 

I also tried this expression:


= Table.SelectRows(F_SOURCE_DAILY, each ([FK_DATE] = List.MaxN([FK_DATE],5)))


but that gives the error:

 

Filtered_Rows_02.jpg

 

Anybody an idea how to solve this?

1 ACCEPTED SOLUTION

Hello @RudyWelvaert

you'd have to adjust the syntax to this:

Table. SelectRows (F_SOURCE_DAILY, each each ([FK_DATE] á List. MaxN (F_SOURCE_DAILY[FK_DATE], 5)))

For performance reasons, it would probably make sense to buffer the F_SOURCE_DAILY table before feeding in this step.

Within the 20th argument of Table.Select Rows, the short form [FK_Date] returns the record field (the current row) and not the entire column. Therefore, you must explicitly type the name of the table whose column you want to choose for List.Max.

@FarhanAhmed ,

just a warning: Sort commands in Power Query do not reliably maintain sort order without a buffer: https://community.powerbi.com/t5/Community-Blog/Bug-warning-for-Table-Sort-and-removing-duplicates-i...


Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

4 REPLIES 4
FarhanAhmed
Community Champion
Community Champion

Try this

 

You need to sort your column

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\farhan.ahmed\Desktop\Book1.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    
    #"Sorted Rows" = Table.Sort(Table1_Table,{{"FK_Date", Order.Descending}}),

    Distincttable =  Table.Distinct(Table.SelectColumns(#"Sorted Rows","FK_Date")),
    TopTable = Table.FirstN(Distincttable,5),
   
   
    FilteredTable = Table.NestedJoin(#"Sorted Rows", {"FK_Date"}, TopTable, {"FK_Date"}, "Table1", JoinKind.Inner),
    RemoveColumn = Table.RemoveColumns(FilteredTable,{"Table1"})
in
    RemoveColumn

 







Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!

Proud to be a Super User!




Hello @RudyWelvaert

you'd have to adjust the syntax to this:

Table. SelectRows (F_SOURCE_DAILY, each each ([FK_DATE] á List. MaxN (F_SOURCE_DAILY[FK_DATE], 5)))

For performance reasons, it would probably make sense to buffer the F_SOURCE_DAILY table before feeding in this step.

Within the 20th argument of Table.Select Rows, the short form [FK_Date] returns the record field (the current row) and not the entire column. Therefore, you must explicitly type the name of the table whose column you want to choose for List.Max.

@FarhanAhmed ,

just a warning: Sort commands in Power Query do not reliably maintain sort order without a buffer: https://community.powerbi.com/t5/Community-Blog/Bug-warning-for-Table-Sort-and-removing-duplicates-i...


Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

@Anonymous I took a slightly different approach using List.Contains. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jc67CcAwDAXAXVS7kJ5/8izG+68RUCACJQS1V93eJFQIDGYF0ymbEKE+IMugRegOajAiTIdpoBGWwzAQfolfpd+CP2kJqQlBQiQh/CXnAg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, DateKey = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DateKey", Int64.Type}}),
    #"Keep 5 Days" = 
        Table.SelectRows(
            #"Changed Type",
            each
                let
                    varLastDate = List.Max(#"Changed Type"[DateKey]),
                    varKeepDays = {varLastDate - 4..varLastDate}
                in
            List.Contains(varKeepDays, [DateKey])
        )
in
    #"Keep 5 Days"
  • varLastDate equals 20200820
  • varKeepDays returns a list of integers from 20200816 through 20200820
  • ListContains is your Select Rows filter condition - it keeps everything in your table from the #Changed Step step that exists in the varKeepDays list.

I think it depends on what you need. The List.MaxN will keep the most recent 5 days in the list, which would be 20, 19, 18, 17, and 15. I don't see a 16 in your data. My method will keep 5 calendar days, so 20, 19, 18, 17, and 16, even though there is no 16 in the data.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Greg_Deckler
Community Champion
Community Champion

@ImkeF , @edhans 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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
Top Kudoed Authors