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 get the value from row 2 etc.
But to do this i need to use a variable that can hold values for me while populating the column, and this is where i need help.
i tried to use parameters and Lists, but i cant seem to get it to work, (if it can be done at all?)
The table below is the table im using, and I manually inserted a column that shows the correct values and its sorted the way i want (date and itemID).
Date | ItemID | Value | Correct values |
2017-01-01 | 1 | null | 0 |
2017-01-02 | 1 | 2000 | 2000 |
2017-01-03 | 1 | null | 2000 |
2017-01-04 | 1 | null | 2000 |
2017-01-05 | 1 | 1800 | 1800 |
2017-01-01 | 2 | null | 0 |
2017-01-02 | 2 | null | 0 |
2017-01-03 | 2 | 500 | 500 |
2017-01-04 | 2 | 400 | 400 |
2017-01-05 | 2 | null | 400 |
The image below is my current result, and as you can see it doesnt hold the last valus for each itemID.
The Column "Calc val clm" is the one i want to look like "Correct values"
Table code:
let
Source = Excel.Workbook(File.Contents("C:\Users\kalle.eljas\SharePoint\Dalarna Power BI - Dokument\Extern\Byxshoppen\problem\Tableprobs.xlsx"), null, true),
ValueTable_Table = Source{[Item="ValueTable",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(ValueTable_Table,{{"Date", type date}, {"ItemID", Int64.Type}, {"Value", Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"ItemID", Order.Ascending}, {"Date", Order.Ascending}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Sorted Rows",{{"Correct values", Int64.Type}}),
#"Added Value clm" = Table.AddColumn(#"Changed Type1", "Calc val clm", each
if [ItemID] = List.Last(ValueList)
then List.First(CurrStockValueFn([Value],[ItemID]))
else List.First(CurrStockValueFnFirst([Value],[ItemID])) )
in
#"Added Value clm"
Functions:
CurrStockValueFn():
let
CurrStock = (stock, item) =>
List.ReplaceRange(ValueList, 0 , 2, {stock, item})
in
CurrStock
CurrStockValueFnFirst():
let
CurrStock = (stock, item) =>
if stock = null
then List.ReplaceRange(ValueList, 0 , 2, {0, item})
else List.ReplaceRange(ValueList, 0 , 2, {stock, item})
in
CurrStock
And the ValueList is simply a list i made from ={0,0} where the first parameter is value and second is ItemID
So the questions are really:
- can I use lists the way im trying to here?
- Can I use parameters? I couldnt find a way to set the values at all with parameters.
- Or am I missing something? Is there any other way of holding values while populationg a column?
// Kalle Eljas