Quantcast
Channel: Desktop topics
Viewing all articles
Browse latest Browse all 213819

How to hold values in variables while populating a column?

$
0
0

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).

 

DateItemIDValueCorrect values
2017-01-011null0
2017-01-02120002000
2017-01-031null2000
2017-01-041null2000
2017-01-05118001800
2017-01-012null0
2017-01-022null0
2017-01-032500500
2017-01-042400400
2017-01-052null400

 

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"

bild.png

 

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


Viewing all articles
Browse latest Browse all 213819

Trending Articles