Reputation: 169
I would like to create a table basic on two criteria - max date for ID, and if date is the same, then max value for the date. If it happens that date and value is the same for all lines for certain ID - just take one line for that ID
Can be done in either DAX or PQ (if PQ is it even possible to not have many steps?)
My initial table
id | date | value |
---|---|---|
ID1 | 01.01.2022 | 100 |
ID1 | 01.01.2022 | 150 |
ID1 | 01.02.2022 | 100 |
ID2 | 01.03.2022 | 150 |
ID2 | 01.03.2022 | 100 |
ID3 | 01.04.2022 | 300 |
ID3 | 01.04.2022 | 300 |
ID3 | 01.04.2022 | 300 |
Expected output
id | date | value |
---|---|---|
ID1 | 01.02.2022 | 100 |
ID2 | 01.03.2022 | 150 |
ID3 | 01.04.2022 | 300 |
Thank you in advance for help
Upvotes: 0
Views: 5083
Reputation: 21393
Your question does not match the sample data. If you find the max date for an ID, then how can the same ID appear twice in the output like you show for ID1? The max value for ID1 on 01.02.2022 is 150, not 100, so how does that match the request for max value for the max date?
That said, if you are really asking "Find the max date, and max value on that date" by ID, then you can use below in PowerQuery, which groups and sorts, then takes the first row to return
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"id", type text}, {"date", type date}, {"value", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"id"}, {{"Count", each Table.Sort(_,{{"date", Order.Descending}, {"value", Order.Descending}}){0} }}),
#"Expanded Count" = Table.ExpandRecordColumn(#"Grouped Rows", "Count", {"date", "value"}, {"date", "value"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Count",{{"date", type date}})
in #"Changed Type1"
Upvotes: 1
Reputation: 60379
Assuming your output table is incorrect, and should really be:
Then you can
M Code
let
//change next line to reflect actual data sourdce
Source = Excel.CurrentWorkbook(){[Name="Table23"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"id", type text}, {"date", type date}, {"value", Int64.Type}}),
//Group by ID
//Aggregate by max value
#"Grouped Rows" = Table.Group(#"Changed Type", {"id"}, {
{"Count", (t)=> Table.Max(t,"value")}}),
//expand each subtable and set the data types
#"Expanded Count" = Table.ExpandRecordColumn(#"Grouped Rows", "Count", {"date", "value"}, {"date", "value"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Count", {{"date", type date},{"value", Int64.Type}})
in
#"Changed Type1"
Upvotes: 1
Reputation: 1029
Using PowerQuery:
= Table.SelectRows(#"Expanded All", each [date] = #datetime(2022, 8, 3, 0, 0, 0))
= Table.SelectRows(#"Expanded All", each [date] = [MaxDate])
Upvotes: 1