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

Power Query to fillter rows from the most recent files from each quarter of the year

Hi!

I created a table that combines multiple excel files in a Sharepoint folder, creating a single table in my PBI like this:

FileNameFileDateLevel   Value  
file_03-02.2024    03/02/2024   DD44
file_03-02.2024    03/02/2024   BB33
file_09-01.202509/01/2025AA10
file_09-01.202509/01/2025YY12
file_02-02.202502/02/2025FF26
file_02-02.202502/02/2025UU65
file_26-03.202526/03/2025BB30
file_26-03.202526/03/2025QQ98
file_08-04.202508/04/2025II43
file_01-05.202501/05/2025OO50
file_22-06.202522/06/2025DD69
file_07-07.202507/07/2025XX70
file_13-08.202513/08/2025ZZ88
file_30-09.202530/09/2025AA34
file_30-09.202530/09/2025VV100
file_05-10.202505/10/2025AA77
file_05-10.202505/10/2025EE43
file_11-11.202511/11/2025ZZ73
file_23-12.202523/12/2025BB12
file_23-12.202523/12/2025KK51


In Power Query, I want to create a new column called "Year_Quarter" which indicates all rows from the most recent file for each quarter of the year, based on: Q1 (January 1 – March 31), Q2 (April 1 – June 30), Q3 (July 1 – September 30), and Q4 (October 1 – December 31). I want to do this because I want my table to filter and show only the rows from the most recent file for each quarter. The final result would be this:

FileNameFileDateLevel    Value   Year_Quarter   
file_03-02.2024    03/02/2024    DD442024-Q1
file_03-02.202403/02/2024BB332024-Q1
file_26-03.202526/03/2025BB302025-Q1
file_26-03.202526/03/2025QQ982025-Q1
file_22-06.202522/06/2025DD692025-Q2
file_30-09.202530/09/2025AA342025-Q3
file_30-09.202530/09/2025VV1002025-Q3
file_23-12.202523/12/2025BB122025-Q4
file_23-12.202523/12/2025KK512025-Q4


How can I do this?

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Inserted year and quarter" = Table.AddColumn(Source, "Year-Quarter", each Text.From(Date.Year([FileDate]))&" Q-"&Text.From(Date.QuarterOfYear([FileDate]))),
    #"Grouped Rows" = Table.Group(#"Inserted year and quarter", {"Year-Quarter"}, {{"Count", each Table.SelectRows(Table.AddRankColumn(_,"Rank",{"FileDate", Order.Descending},[RankKind = RankKind.Competition]),each [Rank]<2)}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"FileName", "FileDate", "Level   ", "Value  "}, {"FileName", "FileDate", "Level   ", "Value  "})
in
    #"Expanded Count"

Hope this helps.

Ashish_Mathur_0-1759461318187.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Inserted year and quarter" = Table.AddColumn(Source, "Year-Quarter", each Text.From(Date.Year([FileDate]))&" Q-"&Text.From(Date.QuarterOfYear([FileDate]))),
    #"Grouped Rows" = Table.Group(#"Inserted year and quarter", {"Year-Quarter"}, {{"Count", each Table.SelectRows(Table.AddRankColumn(_,"Rank",{"FileDate", Order.Descending},[RankKind = RankKind.Competition]),each [Rank]<2)}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"FileName", "FileDate", "Level   ", "Value  "}, {"FileName", "FileDate", "Level   ", "Value  "})
in
    #"Expanded Count"

Hope this helps.

Ashish_Mathur_0-1759461318187.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
PhilipTreacy
Super User
Super User

Hi @nok 

 

Why does your expected result include files from Feb?  if you only want the most recent file from each quarter then these should be ommitted, and only a file from Mar included for Q1.

 

Also, why are there more than 1 file per quarter?  You have said you want the most recent file, singular, but you have included more than 1 file per quarter?  Why?

 

Regards,

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


lbendlin
Super User
Super User

What's your definition of a quarter?

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZLJagMxDIZfpcw5rhavc0xIAiGHkENCFoaeWij0/c+Vp7arKRQ8DD/IfBK/ludz+Pj8en9Da5BfGdm9yDesBrSADOVB4u1WxLlhWvVmbDYi1qqM0SDlDJ/pEZCgBOu1CGEPer9nlBXKxceMcvGQg/1ehEMPermIBP+LcjBoK8oBpLkS/LSFPej5LDImZSAZdM1AAnQVPRzyePWwyKBvKAH6ip5OIl4bkLZCMyBthYrOSwujqhoNxlY1gvwluN1EoqpKst9UUZLdpoo+HiJJtWXR4FhRiyAL04u1rge9Xucb0EfgDWEz64FwUTbGHnS3+zNYIkPttIiAaNFXVChbQ+1e2ALx4gj0Ff6PHo95XTRM0zc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [FileName = _t, FileDate = _t, #"Level   " = _t, #"Value  " = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"FileName", type text}, {"FileDate", type date}, {"Level   ", type text}, {"Value  ", Int64.Type}},"en-GB"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Quarter", each Text.From(Date.Year([FileDate])) & "-Q" & Text.From(Date.QuarterOfYear([FileDate])),type text),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Quarter"}, {{"Max Date", each List.Max([FileDate]), type nullable date}, {"Rows", each _, type table [FileName=nullable text, FileDate=nullable date, #"Level   "=nullable text, #"Value  "=nullable number, Quarter=text]}}),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"FileName", "FileDate", "Level   ", "Value  "}, {"FileName", "FileDate", "Level   ", "Value  "}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Rows", each [FileDate] = [Max Date]),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Max Date"})
in
    #"Removed Columns"

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". Once you examined the code, replace the entire Source step with your own source.

 

lbendlin_0-1759441449652.png

 

GeraldGEmerick
Solution Supplier
Solution Supplier

@nok You can create a custom column with the following formula:

= Text.From(Date.Year([FileDate])) & "-Q" & Text.From(Date.QuarterOfYear([FileDate]))

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