Forum Discussion

jerryr125's avatar
jerryr125
Helper IV
8 months ago
Solved

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

  • 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

18 Replies

  • Your Data:

     

    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

     

    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]
            )
    )
    

     

     

     

  • 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

  • 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")

     

     

     

     

  • 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"

     

  • 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

     

    • jerryr125's avatar
      jerryr125
      Helper IV

      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]
              )
          ))
      • ronrsnfld's avatar
        ronrsnfld
        Super User

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

         

        ALLEXCEPT('Table','Table'[DEPARTMENT])
  • Anonymous's avatar
    Anonymous
    Not applicable

    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.

    • jerryr125's avatar
      jerryr125
      Helper IV

      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

  • Anonymous's avatar
    Anonymous
    Not applicable

    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.