Reputation: 129
Hi I'm a beginner in Power bi
I have two tables Table 1 has Dates, Unique IDs and Emails
Table 2 has list of Unique IDs same as the ones in table 1
Using DAX, I want to add a calculated column to the 2nd table to get the most recent email from table 1 based on the unique id and also avoid Email Values
Thank you.
Upvotes: 0
Views: 8050
Reputation: 4346
with DAX measure - if both tables have a relationship
Measure1 =
CALCULATE (
MAX ( 'Table 1'[Email] ),
FILTER (
'Table 1',
'Table 1'[Date]
= CALCULATE (
MAX ( 'Table 1'[Date] ),
ALLEXCEPT ( 'Table 1', 'Table 1'[Unique_ID] )
)
)
)
with DAX calculated column - if both tables have a relationship
Column1 =
CALCULATE (
CALCULATE (
MAX ( 'Table 1'[Email] ),
FILTER (
'Table 1',
'Table 1'[Date]
= CALCULATE (
MAX ( 'Table 1'[Date] ),
ALLEXCEPT ( 'Table 1', 'Table 1'[Unique_ID] )
)
)
)
)
with DAX measure - if both tables have no relationship
Measure2 =
CALCULATE (
MAX ( 'Table 1'[Email] ),
FILTER (
'Table 1',
'Table 1'[Date]
= CALCULATE (
MAX ( 'Table 1'[Date] ),
ALLEXCEPT ( 'Table 1', 'Table 1'[Unique_ID] )
)
),
TREATAS ( VALUES ( 'Table 2'[Unique_ID] ), 'Table 1'[Unique_ID] )
)
with DAX calculated column - if both tables have no relationship
Column2 =
CALCULATE (
CALCULATE (
MAX ( 'Table 1'[Email] ),
FILTER (
'Table 1',
'Table 1'[Date]
= CALCULATE (
MAX ( 'Table 1'[Date] ),
ALLEXCEPT ( 'Table 1', 'Table 1'[Unique_ID] )
)
),
TREATAS ( VALUES ( 'Table 2'[Unique_ID] ), 'Table 1'[Unique_ID] )
)
)
Upvotes: 4
Reputation: 3294
Oh sorry I just realized you're already having a table2. Now I've split the answer in two party. New Column and New Table.
Creating NEW COLUMN
In this case you could try:
Most_Recent_Mail = SELECTCOLUMNS(TOPN(1,FILTER(table1, table1[Unique_ID] = table1[Unique_ID]), table1[Date]), "mail", Tabelle1[mail])
Creating NEW TABLE
I copied your table:
Now we can create a new table:
And use the following DAX code to fill this table:
New_Table = SUMMARIZE(table1,table1[Unique_ID], "Most_Recent_Mail",
CALCULATE(VALUES(table1[mail]), FILTER(all(table1), table1[Date] = max(table1[Date]))))
Result:
PS: Just to understand. If you only wanted the date, the formula would be much shorter
table2 = SUMMARIZE(table1,table1[Unique_ID], "Max_Date",
max(table1[Date]))
And if you would like to have both, you can add as much columns you want:
New_Table = SUMMARIZE(table1,table1[Unique_ID],
"Max_Date", max(table1[Date]),
"Most_Recent_Mail",
CALCULATE(VALUES(table1[mail]), FILTER(all(table1), table1[Date] = max(table1[Date]))))
Upvotes: 0