Forum Discussion
Take the last Row per category
Hello everyone!
First of all, I want to apolgize because of my English, because I am not a native speaker.
I have spending so much time trying to transform this:
| Timestamp | User | Tag | SetReset |
| 12/11/2023 6:24:51 | User 1 | Tag 1 | Set Bypass |
| 12/11/2023 6:28:46 | User 1 | Tag 1 | Reset Bypass |
| 12/11/2023 9:36:19 | User 1 | Tag 2 | Set Bypass |
| 12/11/2023 10:00:05 | User 1 | Tag 2 | Reset Bypass |
| 12/11/2023 14:56:23 | User 2 | Tag 8 | Set Bypass |
| 12/11/2023 14:56:32 | User 2 | Tag 8 | Reset Bypass |
| 12/11/2023 21:29:33 | User 3 | Tag 4 | Set Bypass |
| 12/11/2023 21:30:23 | User 3 | Tag 4 | Reset Bypass |
| 13/11/2023 0:09:45 | User 4 | Tag 1 | Set Bypass |
| 13/11/2023 0:11:30 | User 4 | Tag 1 | Reset Bypass |
| 14/11/2023 1:42:28 | User 4 | Tag 5 | Set Bypass |
| 14/11/2023 1:48:10 | User 4 | Tag 6 | Set Bypass |
| 14/11/2023 20:41:55 | User 5 | Tag 7 | Set Bypass |
| 14/11/2023 21:13:55 | User 5 | Tag 7 | Reset Bypass |
Into this:
| Timestamp | User | Tag | SetReset |
| 13/11/2023 0:11:30 | User 4 | Tag 1 | Reset Bypass |
| 12/11/2023 10:00:05 | User 1 | Tag 2 | Reset Bypass |
| 12/11/2023 21:30:23 | User 3 | Tag 4 | Reset Bypass |
| 14/11/2023 1:42:28 | User 4 | Tag 5 | Set Bypass |
| 14/11/2023 1:48:10 | User 4 | Tag 6 | Set Bypass |
| 14/11/2023 21:13:55 | User 5 | Tag 7 | Reset Bypass |
| 12/11/2023 14:56:32 | User 2 | Tag 8 | Reset Bypass |
I only want to show the latest row of each tag.
I mean, only a number of rows corresponding with the number of the existent tags. Also, the row that it is shown must be the latest (taking timestamp).
Thank you very much!! I'm starting with Power Query 🙂
Hi,
= Table.FromRecords(
Table.Group(
Prev_Step,
{"Tag"},
{{"Data", each Table.Max(_,"Timestamp"), type record}}
)[Data]
)Stéphane
3 Replies
- slorin
Super User
Hi,
= Table.FromRecords(
Table.Group(
Prev_Step,
{"Tag"},
{{"Data", each Table.Max(_,"Timestamp"), type record}}
)[Data]
)Stéphane
- CazucFrequent Visitor
Hi Stéphane,
First, I doubled the original table and I did what you posted:
= Table.Group (#"Prev. step", {"Tag"}, {{"New Column Name", each List.Max([Timestamp]), type nullable datetime}})Then, I combined the new table with the previous one, with the common column "Timestamp". Then I expand the new merged table and selected the columns that I need (user and SetReset).
If anyone has a better, more efficient way to do it I would be pleased to read.
Thank you Stéphane!
- Qasim_JanFrequent Visitor
Give this a try:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fdFLCsMgEAbgq0jWhTgPbTLLHqGPVcgii9Btqd309rUlMWqsEhAGvvmdyTA0gC1AixpJWUEWA2o9zaG5ufmpksp1uqeFy/xSp/djcm6rjoe8cSdsQ7+1jb/Psws+Z72QFehzhklojkCL9p8pqVoY+NH9O2l1uLiunvZThCVVS0MQ9OOFNFocV9O8Ih29MVb7NArO76MXDivhaP95WIzgG1dC+yzeNiKM/nfnzBSyEtQJ7LJsFaEWBjFhLLOoY12BAP1R6VzjBw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Timestamp = _t, User = _t, Tag = _t, SetReset = _t]),
#"Trimmed Text" = Table.TransformColumns(Source,{{"Tag", Text.Trim, type text}, {"User", Text.Trim, type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Trimmed Text", "Timestamp", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Timestamp.1", "Timestamp.2", "Timestamp.3", "Timestamp.4", "Timestamp.5", "Timestamp.6", "Timestamp.7", "Timestamp.8", "Timestamp.9", "Timestamp.10", "Timestamp.11"}),
#"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Timestamp.3", "Timestamp.4", "Timestamp.5", "Timestamp.6", "Timestamp.7", "Timestamp.8", "Timestamp.9", "Timestamp.10", "Timestamp.11"}),
#"Merged Columns" = Table.CombineColumns(#"Removed Columns",{"Timestamp.1", "Timestamp.2"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged"),
#"Grouped Rows" = Table.Group(#"Merged Columns", {"Tag"}, {{"Count", each _, type table [Merged=text, User=text, Tag=text, SetReset=nullable text]}, {"a", each List.Max([Merged]), type text}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Tag", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each Table.Last([Count],"Merged")),
#"Expanded Custom" = Table.ExpandRecordColumn(#"Added Custom", "Custom", {"Merged", "User", "SetReset"}, {"Merged", "User", "SetReset"}),
#"Removed Columns1" = Table.RemoveColumns(#"Expanded Custom",{"Count", "a"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Merged", "Timestamp"}}),
#"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Timestamp", "User", "Tag", "SetReset"})
in
#"Reordered Columns"