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
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
v-jayw-msft
Community Support
Community Support

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

 

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

View solution in original post

5 REPLIES 5
v-jayw-msft
Community Support
Community Support

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

 

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
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
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.