Forum Discussion
Combining rows and aggregating data at the same time
Hi Friends ,
I need some help,
Current State of my Data
| Category | Attribute | Consumption_Date | Value |
| Toilet Paper | On Stock | 5/29/2020 12:00:00 AM | 15.62 |
| Toilet Paper | On Order | 5/29/2020 12:00:00 AM | 0.00 |
| Gloves | On Stock | 5/29/2020 12:00:00 AM | 0.64 |
| Gloves | On Order | 5/29/2020 12:00:00 AM | 10.88 |
| Paper Towels | On Stock | 5/29/2020 12:00:00 AM | 4.81 |
| Paper Towels | On Order | 5/29/2020 12:00:00 AM | 1.92 |
| N95 Mask | On Stock | 5/29/2020 12:00:00 AM | 6.37 |
| N95 Mask | On Order | 5/29/2020 12:00:00 AM | 0.00 |
| Hand Sanitizer | On Stock | 5/29/2020 12:00:00 AM | 4.73 |
| Hand Sanitizer | On Order | 5/29/2020 12:00:00 AM | 1.35 |
| Wipes | On Stock | 5/29/2020 12:00:00 AM | 1.38 |
| Wipes | On Order | 5/29/2020 12:00:00 AM | 2.10 |
| Hand Soap | On Stock | 5/29/2020 12:00:00 AM | 1.12 |
| Hand Soap | On Order | 5/29/2020 12:00:00 AM | 1.34 |
| Disinfectant Spray / General Disinfectant | On Stock | 5/29/2020 12:00:00 AM | 4.04 |
| Disinfectant Spray / General Disinfectant | On Order | 5/29/2020 12:00:00 AM | 17.95 |
| 3-Ply Mask | On Stock | 5/29/2020 12:00:00 AM | 6.16 |
| 3-Ply Mask | On Order | 5/29/2020 12:00:00 AM | 20.35 |
Desired State: in Power Query
| Category | Attribute | Date | Value |
| Toilet Paper | On stock : On Order | 5/29/2020 12:00:00 AM | 15.62 |
| Gloves | On stock : On Order | 5/29/2020 12:00:00 AM | 11.52 |
| Paper Towels | Onstock : On Order | 5/29/2020 12:00:00 AM | 6.73 |
| N95 Mask | Onstock : On Order | 5/29/2020 12:00:00 AM | 6.37 |
| Hand Santizer | Onstock : On Order | 5/29/2020 12:00:00 AM | 6.08 |
| Wipes | Onstock : On Order | 5/29/2020 12:00:00 AM | 3.48 |
| Hand Soap | Onstock : On Order | 5/29/2020 12:00:00 AM | 2.47 |
| Disinfectant Spray / General Disinfectant | Onstock : On Order | 5/29/2020 12:00:00 AM | 21.99 |
| 3-Ply Mask | Onstock : On Order | 5/29/2020 12:00:00 AM | 26.51 |
Can someone help me how to achieve this ASAP
Hi,
This M code works
let Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Attribute", type text}, {"Consumption_Date", type datetime}, {"Value", type number}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Attribute.1", each "On Stock : On Order"), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Attribute"}), #"Grouped Rows" = Table.Group(#"Removed Columns", {"Category", "Consumption_Date", "Attribute.1"}, {{"Value", each List.Sum([Value]), type number}}), #"Renamed Columns" = Table.RenameColumns(#"Grouped Rows",{{"Attribute.1", "Attribute"}}), #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Category", "Attribute", "Consumption_Date", "Value"}) in #"Reordered Columns"Hope this helps.
hi Anonymous
Just try this simple way as below:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZLNasMwEIRfZfE5Wa/k/94KheSSJuBADyEHkaggIiRji5b06WvkS2htpBZ0WCQ+zc7Onk7J0SotHRxEJ/tklewNtM5ebmNZpLxJOXECxp+IxgPPu/GeFVjy5LyaY/f91ZdLLCGRRzfafsghTpCwzH9BISVGWNee8u3B0X5KHSmYY80W0KAsNtNsXpsCdmK4xSmWmFUzWPQ4t8JcoRVGOfUVm2OOVbYIh31mhYffVBeb48jUP5mQDkf26NCKLlaL8Tkuwte0ay9qUOZdXpwwDtquF3dIYSON7IWGx8fYYdO//g32W2EzBZGtD/r+p5Vj5SwYjIR89udv", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, Attribute = _t, Consumption_Date = _t, Value = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Attribute", type text}, {"Consumption_Date", type datetime}, {"Value", type number}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Category", "Consumption_Date"}, {{"Value", each List.Sum([Value]), type number}, {"AllData", each _, type table [Category=text, Attribute=text, Consumption_Date=datetime, Value=number]}}), #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Attribute", each [AllData][Attribute]), #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Attribute", each Text.Combine(List.Transform(_, Text.From), ":"), type text}), #"Removed Columns" = Table.RemoveColumns(#"Extracted Values",{"AllData"}), #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Category", "Consumption_Date", "Attribute", "Value"}) in #"Reordered Columns"Result:
here is a similar post, you could refer to it too:
https://stackoverflow.com/questions/44058355/powerquery-how-can-i-concatenate-grouped-values
and here is my sample pbix file, please try it.
Regards,
Lin
8 Replies
- amitchandakSuper User
Anonymous , What I observer is MAX Attribute and consumption date and sum of value in a Table or Matrix
- AnonymousNot applicable
I need to do 2 things here
1. Aggregate on the Category and at the same time combine the attribute into 1 column? do you think I can achieve both with Max?
- amitchandakSuper User
New Measure = max(Table[Category]) & " : " & max(Table[Date])
- Ashish_MathurSuper User
Hi,
Please descibe the conditions very clearly. Will there always be only 2 rows per Category? What is the relevance of On Stock : On order?
- AnonymousNot applicable
Ashish_Mathur wrote:Hi,
Please descibe the conditions very clearly. Will there always be only 2 rows per Category? What is the relevance of On Stock : On order?
Ashish_Mathur yes there will be always 2 rows per category,
I have to combine 2 rows ON Stock & On Order in to a New column ( where it shows as On Stock: On Order + at the same time the Value Column has to be aggregated
- v-lili6-msftCommunity Support
hi Anonymous
Just try this simple way as below:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZLNasMwEIRfZfE5Wa/k/94KheSSJuBADyEHkaggIiRji5b06WvkS2htpBZ0WCQ+zc7Onk7J0SotHRxEJ/tklewNtM5ebmNZpLxJOXECxp+IxgPPu/GeFVjy5LyaY/f91ZdLLCGRRzfafsghTpCwzH9BISVGWNee8u3B0X5KHSmYY80W0KAsNtNsXpsCdmK4xSmWmFUzWPQ4t8JcoRVGOfUVm2OOVbYIh31mhYffVBeb48jUP5mQDkf26NCKLlaL8Tkuwte0ay9qUOZdXpwwDtquF3dIYSON7IWGx8fYYdO//g32W2EzBZGtD/r+p5Vj5SwYjIR89udv", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, Attribute = _t, Consumption_Date = _t, Value = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Attribute", type text}, {"Consumption_Date", type datetime}, {"Value", type number}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Category", "Consumption_Date"}, {{"Value", each List.Sum([Value]), type number}, {"AllData", each _, type table [Category=text, Attribute=text, Consumption_Date=datetime, Value=number]}}), #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Attribute", each [AllData][Attribute]), #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Attribute", each Text.Combine(List.Transform(_, Text.From), ":"), type text}), #"Removed Columns" = Table.RemoveColumns(#"Extracted Values",{"AllData"}), #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Category", "Consumption_Date", "Attribute", "Value"}) in #"Reordered Columns"Result:
here is a similar post, you could refer to it too:
https://stackoverflow.com/questions/44058355/powerquery-how-can-i-concatenate-grouped-values
and here is my sample pbix file, please try it.
Regards,
Lin