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
Giorgi1989
Advocate II
Advocate II

Keep the rows for the last day of each month in Query Editor

Hi,

 

I am struggling with an M-code in Query Editor. 

 

I have a table similar to the one below (simplified): 

 

ValueVolumeBonusCalendar DayMonth
10020010001.01.202201
20020020008.01.202201
20020020022.01.202201
20030030007.09.202209
20050050011.09.202209
20040040021.09.202209
20040040028.09.202209
2004200420021.11.202211
20020010031.11.202211

 

I want to filter the rows so that only the max day in the date column ('Calendar Day') for each month is returned. 

 

So, in a nutshell, the desired output would look something like this:

 

ValueVolumeBonusCalendar DayMonth
20020020022.01.202201
20040040028.09.202209
20020010031.11.202211

 

Any help would be appreciated.

 

P.S. I have tried List.Max, 'Group by Month' and other solutions to no avail.

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @Giorgi1989 ,

 

Group your table on [Month] and set the aggregation column to 'All Rows' and call it 'data'.

Create a new custom column with the following code:

Table.Max([data], "Calendar Day")

Expand the resulting record column to reinstate the grouped columns for only rows where [Calendar Day] = max date in month.

 

Power Query example code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jY/RCcAgDER3ybeUu2tLdRZx/zUqViRtEYTwkpBHSHI2AhZMjU8NbjUEqTVWQu5zT8QVS5pZuyOuDWlYyVmnIzmzDketWXFqvVLdxnE//1+yf/Gxyg0=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t, Volume = _t, Bonus = _t, #"Calendar Day" = _t, Month = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}, {"Volume", Int64.Type}, {"Bonus", Int64.Type}, {"Calendar Day", type date}, {"Month", Int64.Type}}),
    groupMonthAllRows = Table.Group(chgTypes, {"Month"}, {{"data", each _, type table [Value=nullable number, Volume=nullable number, Bonus=nullable number, Calendar Day=nullable date, Month=nullable number]}}),
    addMaxDateRecord = Table.AddColumn(groupMonthAllRows, "maxDateRecord", each Table.Max([data], "Calendar Day")),
    expandMaxDateRecord = Table.ExpandRecordColumn(addMaxDateRecord, "maxDateRecord", {"Value", "Volume", "Bonus", "Calendar Day"}, {"Value", "Volume", "Bonus", "Calendar Day"}),
    remOthCols = Table.SelectColumns(expandMaxDateRecord,{"Value", "Volume", "Bonus", "Calendar Day", "Month"})
in
    remOthCols

 

Example output:

BA_Pete_0-1664371896373.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

4 REPLIES 4
Giorgi1989
Advocate II
Advocate II

Thank you for the comment. The (memory) problem in our case may lie more with the fact that we use SAP Business Warehouse as our source, which has placed a great deal of limitations on us in the past. There is an organizational decision to move away from SAP BW in the future, but that won't happen anytime soon. 

BA_Pete
Super User
Super User

Hi @Giorgi1989 ,

 

Group your table on [Month] and set the aggregation column to 'All Rows' and call it 'data'.

Create a new custom column with the following code:

Table.Max([data], "Calendar Day")

Expand the resulting record column to reinstate the grouped columns for only rows where [Calendar Day] = max date in month.

 

Power Query example code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jY/RCcAgDER3ybeUu2tLdRZx/zUqViRtEYTwkpBHSHI2AhZMjU8NbjUEqTVWQu5zT8QVS5pZuyOuDWlYyVmnIzmzDketWXFqvVLdxnE//1+yf/Gxyg0=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t, Volume = _t, Bonus = _t, #"Calendar Day" = _t, Month = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}, {"Volume", Int64.Type}, {"Bonus", Int64.Type}, {"Calendar Day", type date}, {"Month", Int64.Type}}),
    groupMonthAllRows = Table.Group(chgTypes, {"Month"}, {{"data", each _, type table [Value=nullable number, Volume=nullable number, Bonus=nullable number, Calendar Day=nullable date, Month=nullable number]}}),
    addMaxDateRecord = Table.AddColumn(groupMonthAllRows, "maxDateRecord", each Table.Max([data], "Calendar Day")),
    expandMaxDateRecord = Table.ExpandRecordColumn(addMaxDateRecord, "maxDateRecord", {"Value", "Volume", "Bonus", "Calendar Day"}, {"Value", "Volume", "Bonus", "Calendar Day"}),
    remOthCols = Table.SelectColumns(expandMaxDateRecord,{"Value", "Volume", "Bonus", "Calendar Day", "Month"})
in
    remOthCols

 

Example output:

BA_Pete_0-1664371896373.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Thank you immensely for your kind reply, and my sincere apologies for returning so late to this. I have had personal reasons for this, which I won't tire you here with unnecessarily. Generally speaking, your proposed method indeed works fine. Unfortunately, however, in my specific application it did not help me overcome the problem, which I had. Let me clarify this a bit further:

 

In my real-life scenario I had about 20 or so columns to work with. When I applied all the suggested steps to them, I ran into some memory issues, whereby it was stated that not enough memory was available to handle the amount of data. One of the reasons I wanted to only have the latest days filtered by month, was exactly to bypass this memory problem. Indeed, if I were to leave all days of month in, then for 12-month period I would be winding up with 50+ million rows, which seems to be too much to be handled by PBI (at least with our current subscription of PBI Pro). What I had hoped for was to be able to first filter the dates to the latest calendar day, and then, as next steps, add all those columns in, which greatly multiply the number of rows. But, I seem to be unable to do that. On the other hand, if I include all the columns that I later need for my reporting, and only then apply those 'last calendar day' filtering steps (suggested by you), I run into the memory issues. I feel like I am in a Catch-22 situation 😄 

Hi @Giorgi1989 ,

 

50 million rows is nothing for Power BI, it can handle literally billions (unless you're reaching PBIX file size limits, but this wouldn't throw an error until publishing). The issue is most likely with your hardware, more specifically, the amount of RAM you have on the machine where you're doing your processing. This can be cheaply and easily upgraded for instant results. For context, I have 48GB RAM on my dev machine.

If you're not able to upgrade your hardware, there's a couple of other options available. In order of preference:

 

1) Limit the data at source by using views or similar. If your source is an SQL Server, then you should be able to get the outcome you need using max('Calendar Day') over (partition by Month) and Group By.

2) Change this table, or the whole model, to Direct Query. Using DQ will avoid any data being loaded into your report, so avoids processing limitations like this. However, your PQ transformations will be limited to only those that fold back to your source, and you will need to work a bit harder at the visualisation stage to maintain good, responsive, report performance (as every visual will send its own query direct to your source to display).

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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