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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Possibilitis
Frequent Visitor

How to get Max date for each group of data for each fiscal year using Power Query ( M )

Hi folks, 

 

I am quite new to powerQuery, and I would appreciate your assistance. 

I have a table with data from different fiscal year, including the fiscal year - which is not at its end. 

I would like to get the maxdate, at any time, for each fiscal year, using data for each fiscal year. For instance, If today is January 20, then, for this fiscal year, the max date would be 2024-01-20, for fiscal year 2024.  The max date for fiscal year would be whatever last run date for fiscal year 2023. Assuming reg calendar, I can assume it is 2023-12-31. 

 

The following is the DAX code that works.

 

MaxDate =

'Table_Name'[Report Run Date] = CALCULATE(MAX('Table_Name'[Report Run Date]),ALLEXCEPT('Table_Name','Table_Name'[Fiscal year]))

I tried the following code -which gives me the table with the max date. The problem is, when I try to load, it adds more data and I don't know why. For instance, if, I have 10 rows in the data, it will load 1000 rows. I don't understand where the other rows come from. 

 


let
Source = #”Source”,
    // Group by 'Fiscal year' and calculate the maximum 'Report Run Date' within each group
   GroupedTable = Table.Group(Source, {"Fiscal year"}, {{"MaxDate", each List.Max([Report Run Date]), type datetime}}),

   
    // Merge the calculated maximum date back to the original table
    MergedTable = Table.Join(Source, "Fiscal year", GroupedTable, "Fiscal year"),
   
    // Extract the 'MaxDate' column from the merged table
    ResultTable = Table.SelectColumns(MergedTable, {"MaxDate"})
in
    ResultTable

 

I also tried  just using List.max( #"source" Report Run Date), it gives the max date for ALL THE FISCAL YEARS. NOT for each fiscal year. 

 

Can someone help with this? Thanks in advance!

Thanks

1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@Possibilitis 

I created a dummy table to represent your data.

Fowmy_0-1705779403965.png


Create a blank Query, go to the Advanced Editor, clear the existing code, and paste the codes give below and follow the steps.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc6xDcAgDAXRXaixZJsEwyyI/deIi0TE8u9Or7q1irJqqUVvEiXPXY91YAZsAJvZGgOTaO28tGAdmAEbwGa29yWaRLvcpBMLef7NgA1gM5vyZ/sB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Fiscal Year" = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Fiscal Year", Int64.Type}, {"Date", type date}},"en-gb"),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Fiscal Year"}, {{"Last Date", (t)=>  Table.AddColumn(t,"Latest Date", each List.Max(t[Date] )) }}),
    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Fiscal Year"}),
    #"Expanded Last Date" = Table.ExpandTableColumn(#"Removed Columns", "Last Date", {"Fiscal Year", "Date", "Latest Date"} , {"Fiscal Year", "Date", "Latest Date"})
in
    #"Expanded Last Date"


Desired results:

Fowmy_1-1705779481096.png

 



 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

4 REPLIES 4
Fowmy
Super User
Super User

@Possibilitis 

I created a dummy table to represent your data.

Fowmy_0-1705779403965.png


Create a blank Query, go to the Advanced Editor, clear the existing code, and paste the codes give below and follow the steps.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc6xDcAgDAXRXaixZJsEwyyI/deIi0TE8u9Or7q1irJqqUVvEiXPXY91YAZsAJvZGgOTaO28tGAdmAEbwGa29yWaRLvcpBMLef7NgA1gM5vyZ/sB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Fiscal Year" = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Fiscal Year", Int64.Type}, {"Date", type date}},"en-gb"),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Fiscal Year"}, {{"Last Date", (t)=>  Table.AddColumn(t,"Latest Date", each List.Max(t[Date] )) }}),
    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Fiscal Year"}),
    #"Expanded Last Date" = Table.ExpandTableColumn(#"Removed Columns", "Last Date", {"Fiscal Year", "Date", "Latest Date"} , {"Fiscal Year", "Date", "Latest Date"})
in
    #"Expanded Last Date"


Desired results:

Fowmy_1-1705779481096.png

 



 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Possibilitis
Frequent Visitor

I don't know. All I want is the exact number of records, with just a new column added. I don't understand why I have additional records. How do I get rid of that.  Or what would be the best way to get calcuate the maxdate for each fiscal year ? How would you do it ?

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

lbendlin
Super User
Super User

For instance, if, I have 10 rows in the data, it will load 1000 rows. I don't understand where the other rows come from. 

From the Table.Join step.  Why do you have that?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors