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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
jerryr125
Helper IV
Helper IV

Filtering a table on the Max date by department

Hi - 

I am trying to do the following (I posted something earlier this month and I think the example was incorrect).

I would like to pull the max date based upon a specific department.

 

Input: Department-KPI-Table

 

DEPARTMENTKPIDATEKPISCORE
A11/01/202510
B11/01/202520
B12/01/202530
C12/01/202535
D09/01/202515
D09/15/202520
D10/15/202525

 

Needed rows after code/logic:

DEPARTMENTKPIDATEKPISCORE
A11/01/202510
B12/01/202530
C12/01/202535
D10/15/202525

 

Assistance is appreciated - Jerry

1 ACCEPTED SOLUTION

Thank you everyone for examples and assistance - appreciate it.

I ended up doing the following:

- Sort by Department (Ascending) then by date (Decending - putting the most recent date first)

- Adding ranking logic by Department

- The result is the most recent date results in a ranking of 1 for each departmnt

- Filter on the 1 Ranking

As the data dynamically updates, I will always get the most recent date.

Thanks - Jerry

View solution in original post

18 REPLIES 18
v-tsaipranay
Community Support
Community Support

Hi @jerryr125.

 

I'm glad to hear that you've resolved your issue.
If you encounter any further problems, please feel free to reach out to the community forum for assistance. We're here to help with any questions or challenges you may have.

Thank you.

v-tsaipranay
Community Support
Community Support

Hi @jerryr125 ,

Thank you for reaching out to the Microsoft Fabric Community Forum. 

 

Could you please let us know if the issue has been resolved? I wanted to check if you had the opportunity to review the information provided. If you still require support, please let us know, we are happy to assist you. Thank you for all the members who have already provided helpful responses.

 

Thank you.

Thank you everyone for examples and assistance - appreciate it.

I ended up doing the following:

- Sort by Department (Ascending) then by date (Decending - putting the most recent date first)

- Adding ranking logic by Department

- The result is the most recent date results in a ranking of 1 for each departmnt

- Filter on the 1 Ranking

As the data dynamically updates, I will always get the most recent date.

Thanks - Jerry

Omid_Motamedise
Super User
Super User

Hi @jerryr125 

 

 

You can easily handle this problem using GroupBY command, but in the third argument you need to apply a modification.


consider the following code: (copy and past it in advanced editor to see the result)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI01Dcw1DcyMDIFcQyUYnWilZzQxY2QxI2QxI0h4s4Y4qZgcRcg08AS2XwUcUNTNPNdwE5AFgeqjwUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DEPARTMENT = _t, KPIDATE = _t, KPISCORE = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"KPIDATE", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"DEPARTMENT"}, {{"Count", each Table.Max(_,"KPIDATE")}}),
    #"Expanded Count" = Table.ExpandRecordColumn(#"Grouped Rows", "Count", {"KPIDATE", "KPISCORE"}, {"KPIDATE", "KPISCORE"})
in
    #"Expanded Count"

 

see in the Table.Group function, I have just modify the third arguemnt and replaced it by Table.Max(_,"KPIDATE")

 

 

 

 


If my answer helped solve your issue, please consider marking it as the accepted solution.
cengizhanarslan
Super User
Super User

Create this measure:

Is Latest KPI =
VAR MaxDeptDate =
    CALCULATE (
        MAX ( 'Department-KPI-Table'[KPIDATE] ),
        ALLEXCEPT ( 'Department-KPI-Table', 'Department-KPI-Table'[DEPARTMENT] )
    )
RETURN
IF (
    'Department-KPI-Table'[KPIDATE] = MaxDeptDate,
    1,
    0
)

 

Then:

  • Put the table in a Table visual

  • Add Is Latest KPI to Filters

  • Filter where Is Latest KPI = 1

_________________________________________________________
If this helped, ✓ Mark as Solution | Kudos appreciated
Connect on LinkedIn
AI-assisted tools are used solely for wording support. All conclusions are independently reviewed.
MasonMA
Super User
Super User

Hi, 

 

You can also do this only in Power BI. 

 

LATEST KPI FLAG =
VAR _MaxDeptDate =
    CALCULATE (
        MAX ( 'Department-KPI-Table'[KPIDATE] ),
        ALLEXCEPT (
            'Department-KPI-Table',
            'Department-KPI-Table'[DEPARTMENT]
        )
    )
RETURN
    'Department-KPI-Table'[KPIDATE] = _MaxDeptDate

MasonMA_0-1766942337014.png

MasonMA_1-1766942412808.png

 

Hi - I like this method, but I need create a column in the Power BI Workflow (power query) in the table itself.

I add a column and enter this code in the 'custom column formula' - got an error - any thoughts ?

 

CALCULATE (
        MAX ([KPIDATE]),
        ALLEXCEPT ([DEPARTMENT]
        )
    ))

your syntax for ALLEXCEPT is wrong. You need to show the Table and the Column as arguments:

 

ALLEXCEPT('Table','Table'[DEPARTMENT])

Thank you again for your help - update:

 

Table: Department-KPI-Table
Columns: KPIDATE (Date)
DEPARTMENT (Text)

 

New column:KPIMaxDate
Datatype: I Kept this blank but wants to convert to text

Formula:
CALCULATE (
MAX ([KPIDATE]),
ALLEXCEPT(Department-KPI-Table,Department-KPI-Table[DEPARTMENT])
)

Result: all null when I convert to Text

Seems like I am getting close - any thoughts ?

 

Your help is appreciated - Jerry

 

Hi Jerry;

 

I was not able to reproduce your errors either unless you can share your real 'KPIDATE' or more data so that we can play with. Tried with some cases like if your 'KPIDATE' has nulls, Non-date values etc. but all works. 

HI - the following attachment is an example of data. The INPUTTABLE contains a sample of data in the Power Query Workflow.  The FINALRESULTS is what I am looking to achieve via the Power Query Workflow.  Any help is appreciated - I think we are close! 

Thanks - jerry

 

https://docs.google.com/spreadsheets/d/1cEnZ31RToVBIZX20J5A5aI-MwuylhSI6/edit?usp=sharing&ouid=11087...

 

Hi !

 

I'm not seeing any issue with the solution i provided using your Excel data. If i create a new column with below DAX, or your tested fomular, i have this result. 

MasonMA_0-1767032679647.png

 

and in reporting

MasonMA_1-1767032794157.png

 

 

 

Hi @MasonMA 

Thank you again for your help.

Ok so my question is this - exactly where do I put that code inthe Power BI / Power Query workflow ?

I already have a lot of code in the workflow when I click on the 'advanced editor' - do I add the code below to the end? 

thanks - Jerry

For entering DAX code, you would navigate to the Table Tools tab on the PBI interface.

For M code you would use the Power Query Advanced Editor.

 

I've attached a file showing the two approaches using the same data as you showed in your Excel sheet or you can access it via this link.

Hi Jerry;

 

The one i provided and tested is a Power BI approach and you can use without involving Power Query. To add M code in Power Query/Dataflow, you would need to use @ronrsnfld and others' Power Query solution.

 

For that, yes you would need to open the Query editor in Power Query and paste the code inside, 

 

"

 #"Changed Type" = Table.TransformColumnTypes(LastStep,{{"DEPARTMENT", type text}, {"KPIDATE", type date}, {"KPISCORE", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"DEPARTMENT"}, {
        {"KPIDATE", (t)=> Table.SelectRows(t, each [KPIDATE]=List.Max(t[KPIDATE])),
            type table[DEPARTMENT=text, KPIDATE=date, KPISCORE=Int64.Type]}}),
    #"Expanded KPIDATE" = Table.ExpandTableColumn(#"Grouped Rows", "KPIDATE", {"KPIDATE", "KPISCORE"})
        
in
    #"Expanded KPIDATE"

"-- from ronrsnfld 

I cannot reproduce your problem (nor do I understand why you would want to convert the date to text). Perhaps @MasonMA can.

 

Or you could just create the new table as I showed in my DAX related answer above.

ronrsnfld
Super User
Super User

Your Data:

ronrsnfld_0-1766941350020.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI01Dcw1DcyMDIFcQyUYnWilZzQxY2QxI2QxI0h4s4Y4qZgcRcg08AS2XwUcUNTNPNdwE5AFgeqjwUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DEPARTMENT = _t, KPIDATE = _t, KPISCORE = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DEPARTMENT", type text}, {"KPIDATE", type date}, {"KPISCORE", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"DEPARTMENT"}, {
        {"KPIDATE", (t)=> Table.SelectRows(t, each [KPIDATE]=List.Max(t[KPIDATE])),
            type table[DEPARTMENT=text, KPIDATE=date, KPISCORE=Int64.Type]}}),
    #"Expanded KPIDATE" = Table.ExpandTableColumn(#"Grouped Rows", "KPIDATE", {"KPIDATE", "KPISCORE"})
        
in
    #"Expanded KPIDATE"

 Results

ronrsnfld_1-1766941381148.png

 

You can also do this in Power BI creating a new table using DAX:

Table 2 = 
SUMMARIZE (
    'Table',
    'Table'[DEPARTMENT],
    "MaxKPIDATE",
        MAX ( 'Table'[KPIDATE] ),
    "KPISCORE",
        MAXX (
            TOPN (
                1,
                FILTER (
                    'Table',
                    'Table'[DEPARTMENT] = EARLIER ( 'Table'[DEPARTMENT] )
                ),
                'Table'[KPIDATE], DESC
            ),
            'Table'[KPISCORE]
        )
)

 

ronrsnfld_0-1766951512848.png

 

 

ralf_anton
Helper II
Helper II

Hi Jerry,

 

let
    Quelle = Excel.CurrentWorkbook(){[Name="Tabelle3"]}[Content],
    #"Geänderter Typ" = Table.TransformColumnTypes(Quelle,{{"KPIDATE", type date}}),
    #"Gruppierte Zeilen" = Table.Group(#"Geänderter Typ", {"ABTEILUNG"}, {{"LastDate", each List.Max([KPIDATE]), type nullable date}})
in
    #"Gruppierte Zeilen"

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.