Forum Discussion
Einomi
Helper V
1 year agoAdd a "Markup" Row to my Table
Hello, I have a table of food production and a table of sales of these products. I need to follow on a weekly basis the trend. So, both tables, have a column called "Start of the Week" and it rep...
- 1 year ago
Hi Einomi, check this:
Output
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("NY67CgJBDEX/ZeoQJq9JprW3sly2EIRVEBdE/9/ZCXaHw703WZZy2r/b/VOgkAKbY5Nk7RWlH2xQzdAjvblgTC8QNjKeLOFYuaywlMv+vB2ygdUxSDnSWdE0Pev/0GBjpJaZcMaQZKdAoTl4vr63x2toHs2Ofb4CKopkE6kTRmLzQPaZrcyobaIIIfeyrj8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Attribut = _t, #"09/02/2025" = _t, #"02/02/2025" = _t, #"26/01/2025" = _t, #"19/01/2025" = _t, #"12/01/2025" = _t, #"05/01/2025" = _t]), ChangedType = Table.TransformColumnTypes(Source, List.Transform(List.Skip(Table.ColumnNames(Source)), each {_, type number}), "en-US"), Ad_MarkupValue = List.TransformMany(List.Skip(Table.ToColumns(ChangedType)), each {_}, (x,y)=> x & {y{2} / y{0}} ), ToTbl = Table.FromColumns({Table.Column(ChangedType, Table.ColumnNames(ChangedType){0}) & {"Markup"}} & Ad_MarkupValue, Value.Type(Table.FirstN(ChangedType, 0))) in ToTbl - 1 year ago
Somewhat different approach:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("NY67CgJBDEX/ZeoQJq9JprW3sly2EIRVEBdE/9/ZCXaHw703WZZy2r/b/VOgkAKbY5Nk7RWlH2xQzdAjvblgTC8QNjKeLOFYuaywlMv+vB2ygdUxSDnSWdE0Pev/0GBjpJaZcMaQZKdAoTl4vr63x2toHs2Ofb4CKopkE6kTRmLzQPaZrcyobaIIIfeyrj8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Attribut = _t, #"09/02/2025" = _t, #"02/02/2025" = _t, #"26/01/2025" = _t, #"19/01/2025" = _t, #"12/01/2025" = _t, #"05/01/2025" = _t]), #"Type modifié" = Table.TransformColumnTypes(Source,{{"Attribut", type text}, {"09/02/2025", type text}, {"02/02/2025", type text}, {"26/01/2025", type text}, {"19/01/2025", type text}, {"12/01/2025", type text}, {"05/01/2025", type text}}), //set to useful data types #"Type as Number" = Table.TransformColumnTypes(#"Type modifié", List.Transform(List.Skip(Table.ColumnNames(#"Type modifié")), each {_, type number})), //Add Markup Row #"Markups" = let a=Table.RemoveColumns(#"Type as Number","Attribut") in [Attribut="Markups"] & Record.FromList(List.Transform( List.Zip({Record.FieldValues(a{0}), Record.FieldValues(a{2})}), each _{1} / _{0}), List.Skip(Table.ColumnNames(#"Type as Number"))), #"Add Markups Row" = Table.FromRecords(Table.ToRecords(#"Type as Number") & {Markups}) in #"Add Markups Row"Of course, the visual problem is that all of the values in a given column must have the same data type. Ideally you might want to have the top rows with a currency data type and the Markup row with a percent data type. But that's not possible in PQ.
Consider unpivoting, then Pivot on the expanded Attribut column:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("NY67CgJBDEX/ZeoQJq9JprW3sly2EIRVEBdE/9/ZCXaHw703WZZy2r/b/VOgkAKbY5Nk7RWlH2xQzdAjvblgTC8QNjKeLOFYuaywlMv+vB2ygdUxSDnSWdE0Pev/0GBjpJaZcMaQZKdAoTl4vr63x2toHs2Ofb4CKopkE6kTRmLzQPaZrcyobaIIIfeyrj8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Attribut = _t, #"09/02/2025" = _t, #"02/02/2025" = _t, #"26/01/2025" = _t, #"19/01/2025" = _t, #"12/01/2025" = _t, #"05/01/2025" = _t]), #"Type modifié" = Table.TransformColumnTypes(Source,{{"Attribut", type text}, {"09/02/2025", type text}, {"02/02/2025", type text}, {"26/01/2025", type text}, {"19/01/2025", type text}, {"12/01/2025", type text}, {"05/01/2025", type text}}), //set to useful data types #"Type as Number" = Table.TransformColumnTypes(#"Type modifié", List.Transform(List.Skip(Table.ColumnNames(#"Type modifié")), each {_, type number})), //Add Markup Row #"Markups" = let a=Table.RemoveColumns(#"Type as Number","Attribut") in [Attribut="Markups"] & Record.FromList(List.Transform( List.Zip({Record.FieldValues(a{0}), Record.FieldValues(a{2})}), each _{1} / _{0}), List.Skip(Table.ColumnNames(#"Type as Number"))), #"Add Markups Row" = Table.FromRecords(Table.ToRecords(#"Type as Number") & {Markups}), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Add Markups Row", {"Attribut"}, "Date", "Value"), #"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Attribut]), "Attribut", "Value", List.Sum), #"Changed Type" = Table.TransformColumnTypes(#"Pivoted Column",{{"Date", type date}},"en-150"), #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Bought", Currency.Type}, {"Sold", Currency.Type}, {"Margin", Currency.Type}, {"Markups", Percentage.Type}}) in #"Changed Type1"Then you can set the column data types more appropriately:
- 1 year ago
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("NY67CgJBDEX/ZeoQJq9JprW3sly2EIRVEBdE/9/ZCXaHw703WZZy2r/b/VOgkAKbY5Nk7RWlH2xQzdAjvblgTC8QNjKeLOFYuaywlMv+vB2ygdUxSDnSWdE0Pev/0GBjpJaZcMaQZKdAoTl4vr63x2toHs2Ofb4CKopkE6kTRmLzQPaZrcyobaIIIfeyrj8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Attribut = _t, #"09/02/2025" = _t, #"02/02/2025" = _t, #"26/01/2025" = _t, #"19/01/2025" = _t, #"12/01/2025" = _t, #"05/01/2025" = _t]), types = Table.TransformColumns(Source, {"Attribut", (x) as text => x}, (x) as number => Number.From(x)), z = types & #table(Value.Type(types), {{"Markup"} & List.Transform(List.Skip(List.Zip(Table.ToList(types, each _))), (x) => x{2} / x{0})}) in z
dufoq3
Community Champion
1 year agoHi Einomi, check this:
Output
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("NY67CgJBDEX/ZeoQJq9JprW3sly2EIRVEBdE/9/ZCXaHw703WZZy2r/b/VOgkAKbY5Nk7RWlH2xQzdAjvblgTC8QNjKeLOFYuaywlMv+vB2ygdUxSDnSWdE0Pev/0GBjpJaZcMaQZKdAoTl4vr63x2toHs2Ofb4CKopkE6kTRmLzQPaZrcyobaIIIfeyrj8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Attribut = _t, #"09/02/2025" = _t, #"02/02/2025" = _t, #"26/01/2025" = _t, #"19/01/2025" = _t, #"12/01/2025" = _t, #"05/01/2025" = _t]),
ChangedType = Table.TransformColumnTypes(Source, List.Transform(List.Skip(Table.ColumnNames(Source)), each {_, type number}), "en-US"),
Ad_MarkupValue = List.TransformMany(List.Skip(Table.ToColumns(ChangedType)),
each {_},
(x,y)=> x & {y{2} / y{0}} ),
ToTbl = Table.FromColumns({Table.Column(ChangedType, Table.ColumnNames(ChangedType){0}) & {"Markup"}} & Ad_MarkupValue, Value.Type(Table.FirstN(ChangedType, 0)))
in
ToTbl