Forum Discussion
kirvis
Helper I
4 years agoDetermine invoice category based on highest-value line item
Hi all, I am working on a project to assess the sales at our company, based on the invoices. I have a dataset at line item level, and want to assign a category to each invoice number based on the...
- 4 years ago
kirvis with PQ
List.Max( Table.SelectRows( CT, (r) => r[Invoice number] = [Invoice number] and r[Value] = List.Max(Table.SelectRows(CT, (q) => q[Invoice number] = [Invoice number])[Value]) )[Category] )let Source = Table.FromRows( Json.Document( Binary.Decompress( Binary.FromText( "i45WMlTSAWOn/KKU1MTSChDX0EApVgciZQTEPol56aWpKfnJIDlTuJQxEDvmFCcmpwIZphBxIyymmRrApYyQtRgbACViAQ==", BinaryEncoding.Base64 ), Compression.Deflate ) ), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Invoice number" = _t, #"Line item" = _t, Category = _t, Value = _t] ), CT = Table.TransformColumnTypes( Source, { {"Invoice number", Int64.Type}, {"Line item", Int64.Type}, {"Category", type text}, {"Value", Int64.Type} } ), #"Added Custom" = Table.AddColumn(CT, "maxCAT", each List.Max( Table.SelectRows( CT, (r) => r[Invoice number] = [Invoice number] and r[Value] = List.Max(Table.SelectRows(CT, (q) => q[Invoice number] = [Invoice number])[Value]) )[Category] )) in #"Added Custom"with DAX
_maxCAT = VAR _partition = ALLEXCEPT ( 'Table', 'Table'[Invoice number] ) VAR _val = CALCULATE ( MAX ( 'Table'[Value] ), _partition ) RETURN CALCULATE ( MAX ( 'Table'[Category] ), 'Table'[Value] = _val, _partition )
kirvis
Helper I
4 years agoWow, this truly is like magic.
Thanks for the quick reply and for the elegant solution!