Forum Discussion

user35131's avatar
user35131
Icon for Helper III rankHelper III
4 years ago
Solved

How to group by latest date but within creating table process with multiple variables?

I would like to subset the largest date column from each group(id)

IDDate
14/10/2020
14/17/2020
14/29/2020
25/17/2020
212/17/2020

 

My out come should look like this

 

IDDate
14/29/2020
212/17/2020
 
Normally the way I would do this is using this line of Code
Date = CALCULATE(MAX(Table[date]),ALLEXCEPT(Table,Table[id]))
 
However I am doing this within a create table box with multiple variables. At a certain point its harder to reference the column names and I get stuck.
 

Var table2 =

 

DISTINCT(SELECTCOLUMNS( CALCULATETABLE('Table',

FILTER('Table’,NOT('Table’ [Col1]) IN {"Subset1","Subset2”

),

FILTER('Table,'Table[Col2] IN

{"Subset1","Subset2","Subset3",}

),

FILTER('Table,'Table'[Date]>=DATE(YEAR(MAX('Table'[Date]))-1,MONTH(MAX('Table'[ Date])),DAY(MAX('Table'[Date])))

))

,"Col1",'Table'[Col1],"Col2",'Table'[Date],"Col3", 'Table'[Col3]

))

 

var table3 =

 

SELECTCOLUMNS(table,"ID",[Col1],"Date",[Date])

 
Return table3
 
This returns the table shown above. However I want to select and filter by the max date for each id or if easier create a long column that pulls the largest date for each line based on group.
 
note that I'm trying to get the columns from table3 and not original table. How do i go about this? 
 
I tried doing this 
 
var table4 = 
 
SELECT
 
SELECTCOLUMNS(table3,"ID",[ID],"date",[date],"Max Date",CALCULATE(MAX([_date]),ALLEXCEPT(table3,[id])))
 
Also note disregard if it doesn't truly make sense. I've done my best to replicate the code to get it to what it is without showing the subset names of the data i'm using. 
 
 
  • user35131 , You are suing date then it will not reduce the table

     

    filter(SELECTCOLUMNS(table3,"ID",[ID],"date",[date],"Max Date",CALCULATE(MAX([_date]),ALLEXCEPT(table3,[id])))

    [Date] =[Max Date])

     

     

    or

     

    Summarize(Table, Table[ID] , "Date" , Max(Table[Date]) )

3 Replies

  • user35131 , You are suing date then it will not reduce the table

     

    filter(SELECTCOLUMNS(table3,"ID",[ID],"date",[date],"Max Date",CALCULATE(MAX([_date]),ALLEXCEPT(table3,[id])))

    [Date] =[Max Date])

     

     

    or

     

    Summarize(Table, Table[ID] , "Date" , Max(Table[Date]) )

  • The problem in both scripts is that it doesn't pick up on the column name within functions. I'm able to add ID and Date in select columns as a stand alone. Note where its bold i'm having trouble.

     

    filter(SELECTCOLUMNS(table3,"ID",[ID],"date",[date],"Max Date",CALCULATE(MAX([_date]),ALLEXCEPT(table3,[id])))

     

    Summarize(Table, Table[ID] , "Date" , Max(Table[Date]) )
     
    • user35131's avatar
      user35131
      Icon for Helper III rankHelper III

      I found that instead of Summarize(Table, Table[ID] , "Date" , Max(Table[Date]) ) working

       

      that its better to do table 3 as the funneled table and then in the max add the original table not of a variable. I got the result i wanted. Thanky you. 

       

      Summarize(Table3, Table3[ID] , "Date" , Max(Table[Date]) )