Forum Discussion
Help with mquery index formula
- Anonymous2 years ago
Hi RyanVSS ,
Please try the following steps:let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtc3MtY3MjAyUdJRcnQEEp55aflFuakpwcn5RalAvolSrA5YmRGKMo+YUgMDI7PwYqgyY5gyQxRlwYlpqSWVweiKDPBaCVNmaEmUlYYWMGVOTjitNDRHUYThSz0zmEIzFIU4LTXFb2ksAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [createdon = _t, slgc_contact = _t, Attribute = _t, Value = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"createdon", type date}, {"slgc_contact", type text}, {"Attribute", type text}, {"Value", type number}}), #"Sorted Rows" = Table.Sort(#"Changed Type",{{"createdon", Order.Descending}}), #"Grouped Rows" = Table.Group(#"Sorted Rows", {"slgc_contact", "Attribute"}, {{"Data", each Table.FirstN(_,1)}}), #"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data", {"createdon", "Value"}, {"createdon", "Value"}), #"Grouped Rows2" = Table.Group(#"Expanded Data", {"slgc_contact"}, {{"LatestSession", each List.Max([createdon]), type nullable date}, {"All", each _, type table [createdon=nullable date, slgc_contact=nullable text, Attribute=nullable text, Value=nullable number]}}), #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows2", "All", {"Attribute", "Value"}, {"Attribute", "Value"}), #"Pivoted Column" = Table.Pivot(#"Expanded All", List.Distinct(#"Expanded All"[Attribute]), "Attribute", "Value") in #"Pivoted Column"Best Regards,
Gao
Community Support TeamIf there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!How to get your questions answered quickly -- How to provide sample data in the Power BI Forum -- China Power BI User Group
RyanVSS , Try using below m-code
let
// Load your data
Source = YourDataSource,
// Add an Index Column
AddIndex = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
// Group by slgc_contact and Attribute, and get the max createdon
Grouped = Table.Group(AddIndex, {"slgc_contact", "Attribute"}, {{"MaxCreatedOn", each List.Max([createdon]), type datetime}}),
// Merge the grouped data back with the original table
Merged = Table.NestedJoin(AddIndex, {"slgc_contact", "Attribute", "createdon"}, Grouped, {"slgc_contact", "Attribute", "MaxCreatedOn"}, "GroupedData", JoinKind.Inner),
// Expand the merged table to get the latest records
Expanded = Table.ExpandTableColumn(Merged, "GroupedData", {"MaxCreatedOn"}, {"MaxCreatedOn"}),
// Filter to get only the latest records
LatestRecords = Table.SelectRows(Expanded, each [createdon] = [MaxCreatedOn])
in
LatestRecords