Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hi,
I need to do some data manipulation for the following dataset: Column A, B, and C
21654756 | Closed | Aug 30, 2020 11:32 PM |
21654747 | Closed | Aug 30, 2020 11:17 PM |
21654745 | Closed | Aug 30, 2020 11:13 PM |
21654745 | Open | Aug 30, 2020 11:16 PM |
21654652 | Escalated | Aug 30, 2020, 2:00 AM |
21654652 | Fixed | Aug 31, 2020 1:02 AM |
Column A has duplicate values and unique values.
When Column A is unique, Column B and Column C remains the same;
When Column A is duplicate, then I would like to take the value in Column B based on the latest time of Column C.
So here is the result table I want to see:
21654756 | Closed | Aug 30, 2020 11:32 PM |
21654747 | Closed | Aug 30, 2020 11:17 PM |
21654745 | Open | Aug 30, 2020 11:16 PM |
21654652 | Fixed | Aug 31, 2020 1:02 AM |
I am a beginner of PowerQuery. Can someone guide me on how to achieve the result I would like to have?
Solved! Go to Solution.
Hi @Anonymous , see if this works. It turns this:
into this:
THe code is as follows:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjI0MzUxNzVT0lFyzskvTk0BMhxL0xWMDXQUjAyMDBQMDa2MjRQCfJVidWCqTczxqTY0R1Ntile1MRbV/gWpedjUmqGoNTM1AipyLU5OzEkswTAcSFoZGCg4Ymhwy6xAKDaEmW5lYARWGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column3", type datetime}}),
#"Grouped Rows" =
Table.Group(
#"Changed Type",
{"Column1"},
{
{"Column2",
each
let
varMaxDate = List.Max(_[Column3])
in
Table.SelectRows(_, each [Column3] = varMaxDate)[Column2]{0}, type text
},
{"Column3", each List.Max(_[Column3]), type datetime}
}
)
in
#"Grouped Rows"
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
I actually wrote a blog post on this a month or so ago that goes into more detail about what I did here.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingHi @Anonymous ,
You can also just try this:
Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hello @Anonymous
you can do a Table.Sort surrounded by a Table Buffer. Sort it in a way that the first row is showing what you want. Then use a Table.Distinct where you input "ColumnA" as second parameter. Here the complete code
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjI0MzUxNzVT0lFyzskvTk0BMhxL0xWMDXQUjAyMDBQMDa2MjRQCfJVidWCqTczxqTY0R1Ntile1MRbV/gWpedjUmqGoNTM1AipyLU5OzEkswTAcSFoZGCg4Ymhwy6xAKDaEmW5lYARWGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column A" = _t, #"Column B" = _t, #"Column B.1" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column A", Int64.Type}, {"Column B", type text}, {"Column B.1", type datetime}}),
#"Sorted Rows" = Table.Buffer(Table.Sort(#"Changed Type",{{"Column A", Order.Descending},{"Column B.1", Order.Descending}})),
#"Removed Duplicates" = Table.Distinct(#"Sorted Rows", {"Column A"})
in
#"Removed Duplicates"
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Hi @Anonymous , see if this works. It turns this:
into this:
THe code is as follows:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjI0MzUxNzVT0lFyzskvTk0BMhxL0xWMDXQUjAyMDBQMDa2MjRQCfJVidWCqTczxqTY0R1Ntile1MRbV/gWpedjUmqGoNTM1AipyLU5OzEkswTAcSFoZGCg4Ymhwy6xAKDaEmW5lYARWGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column3", type datetime}}),
#"Grouped Rows" =
Table.Group(
#"Changed Type",
{"Column1"},
{
{"Column2",
each
let
varMaxDate = List.Max(_[Column3])
in
Table.SelectRows(_, each [Column3] = varMaxDate)[Column2]{0}, type text
},
{"Column3", each List.Max(_[Column3]), type datetime}
}
)
in
#"Grouped Rows"
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
I actually wrote a blog post on this a month or so ago that goes into more detail about what I did here.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI Reporting
In DAX this would be Lookup Min/Max - https://community.powerbi.com/t5/Quick-Measures-Gallery/Lookup-Min-Max/m-p/985814#M434
check if is this what you want:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjI0MzUxNzVT0lFyzskvTk0BMhxL0xWMDXQUjAyMDBQMDa2MjRQCfJVidWCqTczxqTY0R1Ntile1MRbV/gWpedjUmqGoNTM1AipyLU5OzEkswTAcSFoZGCg4Ymhwy6xAKDaEmW5lYARWGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", type text}, {"Column3", type datetime}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Column1"}, {{"last", each Table.Max(_,"Column3")}}),
#"Expanded last" = Table.ExpandRecordColumn(#"Grouped Rows", "last", {"Column2", "Column3"}, {"last.Column2", "last.Column3"})
in
#"Expanded last"
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
102 | |
53 | |
21 | |
12 | |
12 |