Forum Discussion
kalleeljas
9 years agoFrequent Visitor
How to hold values in variables while populating a column?
Hello. Im trying to create a table that will replace cell values based on previous value for each itemID. Row 1 should be 0, because there is no value yet, row 2 gets a value and row 3 should ge...
- 9 years ago
It's much easier: just use Fill Down and Replace Values on the Transform tab as demonstrated in this 30 sec video.
- 9 years ago
Well, it seems I was a bit fast with my answer...
In this case, the trick is to merge the table with itself so you will have Items and previous items on the same row.
The rest is pretty straightforward as you can see in this video.
During video recording, the following code was generated (in Power BI Desktop, with data from Excel):
let Source = Excel.Workbook(File.Contents("C:\Users\Marcel\Documents\Forum bijdragen\Power BI Community\How to hold variables etcetera.xlsx"), null, true), Tabel1_Table = Source{[Item="Tabel1",Kind="Table"]}[Data], #"Changed Type" = Table.TransformColumnTypes(Tabel1_Table,{{"Date", type date}, {"ItemID", Int64.Type}, {"Value", Int64.Type}}), #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1), #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1), #"Merged Queries" = Table.NestedJoin(#"Added Index1",{"Index"},#"Added Index1",{"Index.1"},"Prev",JoinKind.LeftOuter), #"Expanded Prev" = Table.ExpandTableColumn(#"Merged Queries", "Prev", {"ItemID"}, {"Prev.ItemID"}), #"Sorted Rows" = Table.Sort(#"Expanded Prev",{{"Index", Order.Ascending}}), #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each if ([Prev.ItemID] = null or [Prev.ItemID] <> [ItemID]) then (if [Value] = null then 0 else [Value]) else [Value]), #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}), #"Removed Columns" = Table.RemoveColumns(#"Filled Down",{"Value", "Index", "Index.1", "Prev.ItemID"}), #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Value"}}), #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Value", Int64.Type}}) in #"Changed Type1"
MarcelBeug
9 years agoCommunity Champion
It's much easier: just use Fill Down and Replace Values on the Transform tab as demonstrated in this 30 sec video.
kalleeljas
9 years agoFrequent Visitor
That was easy. Thanks!