Forum Discussion

nirvana_moksh's avatar
nirvana_moksh
Impactful Individual
8 years ago
Solved

Max Row Based on Insert Date

Hello,

 

So I have two solutions in place but they are causing some issues. Bascially, I have two tables:

 

1.) Table 1 has ID which is also present in Table 2

2.) Table 2 has ID and other data fields

 

Either in M or DAX I want a final table which gives me just one row per ID based on the Insert Date and for other ID's that are not present in Table 2 to return NULL:

 

 

Table 1 

 

IDNAMEROLE
1ABCDEVELOPER
2ADASQA
3MQEBA
4RTYAPM
5GJHTPM

 

 

Table 2 

 

IDTASKDURATIONINSERT DATE
1COMPLETE2 HRS6/22/2018 16:39
1INCOMPLETE3 HRS6/24/2018 15:39
1INCOMPLETE4 HRS6/24/2018 18:39
1INCOMPLETE3 HRS6/24/2018 15:39

 

Final Result 

 

IDTASKDURATIONINSERT DATE
1INCOMPLETE4 HRS6/24/2018 18:39
2nullnullnull
3nullnullnull
4nullnullnull
5nullnullnull

 

My DAX table function and M worked till now but is failing today showing weird results.

  • Hi nirvana_moksh

     

    This calculated table gets close.  It looks long, but it's not that complicated.  I have attached a PBIX file

     

    Table = 
    VAR x = SELECTCOLUMNS(
                Table2 ,
                "IDx",[ID] , 
                "TASK" ,[TASK] ,
                "DURATIONx" , 
                INT(SUBSTITUTE('Table2'[DURATION]," HRS","")) , 
                "INSERT DATE",[INSERT DATE]
                )
    VAR y = 
        GROUPBY(
            x,
            [IDx],
            "MAX_DURATIONx", MAXX(CURRENTGROUP(),[DURATIONx])
            )
    VAR z= 
        SELECTCOLUMNS(
            GENERATEALL(
                'Table1',
                FILTER(
                    y,
                    [ID] = [IDx]
                    )
                ),
                "IDz",[ID],
                "MAX_DURATOINx",[MAX_DURATIONx]
                )
    RETURN 
        SELECTCOLUMNS(
            GENERATEALL(
                z,
                FILTER(
                    'Table2',[ID] = [ID] && 
                    [MAX_DURATOINx] & " HRS" = 'Table2'[DURATION]
                    )
                ),
                "ID",[IDz],
                "TASK",[TASK],
                "DURATION",[DURATION],
                "INSERT DATE",[INSERT DATE]
                )

11 Replies

  • Phil_Seamark's avatar
    Phil_Seamark
    Microsoft Employee

    Hi nirvana_moksh

     

    This calculated table gets close.  It looks long, but it's not that complicated.  I have attached a PBIX file

     

    Table = 
    VAR x = SELECTCOLUMNS(
                Table2 ,
                "IDx",[ID] , 
                "TASK" ,[TASK] ,
                "DURATIONx" , 
                INT(SUBSTITUTE('Table2'[DURATION]," HRS","")) , 
                "INSERT DATE",[INSERT DATE]
                )
    VAR y = 
        GROUPBY(
            x,
            [IDx],
            "MAX_DURATIONx", MAXX(CURRENTGROUP(),[DURATIONx])
            )
    VAR z= 
        SELECTCOLUMNS(
            GENERATEALL(
                'Table1',
                FILTER(
                    y,
                    [ID] = [IDx]
                    )
                ),
                "IDz",[ID],
                "MAX_DURATOINx",[MAX_DURATIONx]
                )
    RETURN 
        SELECTCOLUMNS(
            GENERATEALL(
                z,
                FILTER(
                    'Table2',[ID] = [ID] && 
                    [MAX_DURATOINx] & " HRS" = 'Table2'[DURATION]
                    )
                ),
                "ID",[IDz],
                "TASK",[TASK],
                "DURATION",[DURATION],
                "INSERT DATE",[INSERT DATE]
                )

    • Anonymous's avatar
      Anonymous
      Not applicable

      I just tried in different way.

       

      Step 1 : Lets take your Table 2. Add new column with Max insert date for each ID.

       

      Max_Insert_Date = CALCULATE(MAX(Table2[INSERT DATE]),FILTER(Table2,Table2[ID]= EARLIER(Table2[ID])))

       

      Step 2: From step 1,Get the Latest record only

       

      Table3 = FILTER(Table2,Table2[INSERT DATE]=Table2[Max_Insert_Date])

       

      Step 3: Do a left outer join between Table 1 ( Original table - Just take ID column alone) and Table 3 ( derived in step 2- Select all columns but ID).

       

      You can combine these steps as well to reduce the steps .

       

      Thanks

      Raj

       

       

      • nirvana_moksh's avatar
        nirvana_moksh
        Impactful Individual

        I am gettign error on the second step. The error is "the expression referrs to multiple columsn. Multiple columns cannot be converted to scalar value"

    • nirvana_moksh's avatar
      nirvana_moksh
      Impactful Individual

      Giving this a shot, my actual tables have more data fields than I listed but hopefully my tweaks take them all.

    • nirvana_moksh's avatar
      nirvana_moksh
      Impactful Individual

      Phil_Seamark thanksa lot again Phil, last time you gave me a similar solution and this was an expansion to that and like previously I again appreciate you taking out time to detail it out and laying out new DAX functions which always expands my knowledge. I am customizing this to my need, but it is working so far. Thanks a lot again!

  • Hi,

     

    This M code works

     

    let
        Source = Table.NestedJoin(Table1,{"ID"},Table2,{"ID"},"Table2",JoinKind.LeftOuter),
        #"Expanded Table2" = Table.ExpandTableColumn(Source, "Table2", {"TASK", "DURATION", "INSERT DATE"}, {"TASK", "DURATION", "INSERT DATE"}),
        #"Grouped Rows" = Table.Group(#"Expanded Table2", {"ID"}, {{"Max", each List.Max([INSERT DATE]), type datetime}}),
        #"Merged Queries" = Table.NestedJoin(#"Grouped Rows",{"ID", "Max"},Table2,{"ID", "INSERT DATE"},"Table2",JoinKind.LeftOuter),
        #"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"TASK", "DURATION"}, {"TASK", "DURATION"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Expanded Table1",{"ID", "TASK", "DURATION", "Max"})
    in
        #"Reordered Columns"