Forum Discussion
Anonymous
5 years agoNot applicable
averages
Dear Community,
I need to calculate the average for three specific product (#1,#2,#3) costs, on a daily basis. Then that average should appear as "product 5". I don't know what should be the proper approach to do that.
| Date | Product | Cost |
| 1-1-2020 | 1 | 4 |
| 1-1-2020 | 2 | 3 |
| 1-1-2020 | 3 | 5 |
| 1-1-2020 | 4 | 1 |
| 5-1-2020 | 1 | 1 |
| 5-1-2020 | 2 | 2 |
| 5-1-2020 | 3 | 3 |
| 5-1-2020 | 4 | 6 |
Outcome:
| Date | Product | Cost |
| 1-1-2020 | 1 | 4 |
| 1-1-2020 | 2 | 3 |
| 1-1-2020 | 3 | 5 |
| 1-1-2020 | 4 | 1 |
| 1-1-2020 | 5 | 4 |
| 5-1-2020 | 1 | 1 |
| 5-1-2020 | 2 | 2 |
| 5-1-2020 | 3 | 3 |
| 5-1-2020 | 4 | 6 |
| 5-1-2020 | 5 | 2 |
Thanks in advance.
Cheers
Hi, Anonymous , a simple measure + matrix viz do the trick with ease
AVG 123 = AVERAGEX ( FILTER ( DISTINCT ( 'Table'[Product] ), 'Table'[Product] IN { 1, 2, 3 } ), CALCULATE ( SUM ( 'Table'[Cost] ) ) )
2 Replies
- mahoneypatMicrosoft Employee
Here is one way to do it in the query editor. To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below. Note that two steps of the same query are appended to get your desired result.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ11DUyMDJQ0lEyBGITpVgdFEEjIDZGFzQGYlN0QROwESBBU1QzMQSNwBhN0BhukSmqmWZKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Product = _t, Cost = _t]), InitialTable = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Product", Int64.Type}, {"Cost", Int64.Type}}), Custom1 = InitialTable, #"Filtered Rows" = Table.SelectRows(Custom1, each ([Product] <> 4)), #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Date"}, {{"Product", each List.Min([Product]), type nullable number}, {"Cost", each List.Average([Cost]), type nullable number}}), TableToAppend = Table.ReplaceValue(#"Grouped Rows",1,5,Replacer.ReplaceValue,{"Product"}), #"Appended Query" = Table.Combine({InitialTable, TableToAppend}), #"Sorted Rows" = Table.Sort(#"Appended Query",{{"Date", Order.Ascending}}) in #"Sorted Rows"Regards,
Pat
- CNENFRNLCommunity Champion
Hi, Anonymous , a simple measure + matrix viz do the trick with ease
AVG 123 = AVERAGEX ( FILTER ( DISTINCT ( 'Table'[Product] ), 'Table'[Product] IN { 1, 2, 3 } ), CALCULATE ( SUM ( 'Table'[Cost] ) ) )