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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Anonymous
Not applicable

DAX Power BI select only max date rows of each month and year

Hello, 

i've a table (left one), i want to apply a dax formula to keep only IDs of the last date of a month. (if a date is the last for the month, i keep all its rows)

My ouput will be the right table. I've many columns of course, i just displayed two for the POC.

 

Capture.PNG

 

SELECT *

FROM table t1

WHERE t1.date = (select max(t2.date)

from table t2

where month(t1.date) = month(t2.date) and year(t1.date) = year(t2.date)

)

 

This is the sql code i want 🙂  

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

 

Please check the formula:

Table 2 =
VAR tmp =
    SELECTCOLUMNS (
        SUMMARIZE (
            'Table',
            'Table'[Date].[Year],
            'Table'[Date].[Month],
            "max_", MAX ( 'Table'[Date] )
        ),
        "max_", [max_]
    )
RETURN
    FILTER ( 'Table', 'Table'[Date] IN tmp )

vjaywmsft_0-1653981743391.png

vjaywmsft_1-1653981769417.png

 

Best Regards,

Jay

 

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi @Anonymous ,

 

Please check the formula:

Table 2 =
VAR tmp =
    SELECTCOLUMNS (
        SUMMARIZE (
            'Table',
            'Table'[Date].[Year],
            'Table'[Date].[Month],
            "max_", MAX ( 'Table'[Date] )
        ),
        "max_", [max_]
    )
RETURN
    FILTER ( 'Table', 'Table'[Date] IN tmp )

vjaywmsft_0-1653981743391.png

vjaywmsft_1-1653981769417.png

 

Best Regards,

Jay

 

Anonymous
Not applicable

Hi Mounir

Try this in a new table.

FILTER (
    ADDCOLUMNS (
        SUMMARIZE (
            'ajout col',
            'ajout col'[Qui],
            'ajout col'[Jour],
            'ajout col'[Valeur],
            'Date'[Mois_Num]
        ),
        "@max",
            IF (
                'ajout col'[Jour]
                    = CALCULATE (
                        MAX ( 'ajout col'[Jour] ),
                        KEEPFILTERS ( 'Date'[Mois_Num] ),
                        ALL ( 'Date'[Date] ),
                        ALLEXCEPT ( 'ajout col', 'ajout col'[Qui] )
                    ),
                "Yes",
                "No"
            )
    ),
    [@max] = "Yes"
)

ajout col=Tabkle Name

qui=ID

jour=Date

Valeur= Amount

Shishir22
Solution Sage
Solution Sage

Hello @Anonymous ,

 

Use Below M code in advance editor in Power Query-

 

 #"Inserted Month Name" = Table.AddColumn(#"Changed Type1", "Month Name", each Date.MonthName(Date.From([Date])), type text),
    #"Inserted Year" = Table.AddColumn(#"Inserted Month Name", "Year", each Date.Year([Date]), Int64.Type),
    #"Grouped Rows" = Table.Group(#"Inserted Year", {"ID", "Month Name", "Year"}, {{"MaxDate", each List.Max([Date]), type nullable date}, {"ALLRows", each _, type table [Date=nullable date, ID=nullable text, Amount=nullable number, Month Name=text, Year=number]}}),
    #"Expanded ALLRows" = Table.ExpandTableColumn(#"Grouped Rows", "ALLRows", {"Date", "Amount"}, {"Date", "Amount"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded ALLRows", each [Date] = [MaxDate]),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"MaxDate"})
in
    #"Removed Columns"

 

Refer .PBIX file from here-

max date rows of each month and year.pbix

 

Please mark it as answer if it solves your issue. Kudos are also appreciated.

 

 

Cheers,
Shishir
Anonymous
Not applicable

it's not exactly what i want, and i've a lot of columns to manage, this was just a small example.

I'm looking for a DAX formula to select exactly what i want.

Thanks for your effort

Hello @Anonymous ,

 

Create a calculated column using DAX as below-  

Flag = 
VAR _MaxDate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER (
            'Table',
            'Table'[ID] = EARLIER ( 'Table'[ID] )
                && 'Table'[Year] = EARLIER ( 'Table'[Year] )
                && 'Table'[Month] = EARLIER ( 'Table'[Month] )
        )
    )
RETURN
    IF ( _MaxDate = 'Table'[Date], 1, BLANK () )

 

Shishir22_0-1653483476116.png

 

 

And use this at report/visual level as FLAG =1 

 

Please mark it as answer if it resolves your issue. Kudos are also appreciated.

 

Cheers,
Shishir

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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