Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
kalleeljas
Frequent 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 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

2 ACCEPTED SOLUTIONS
MarcelBeug
Community Champion
Community Champion

It's much easier: just use Fill Down and Replace Values on the Transform tab as demonstrated in this 30 sec video.

Specializing in Power Query Formula Language (M)

View solution in original post

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"
Specializing in Power Query Formula Language (M)

View solution in original post

6 REPLIES 6
MarcelBeug
Community Champion
Community Champion

It's much easier: just use Fill Down and Replace Values on the Transform tab as demonstrated in this 30 sec video.

Specializing in Power Query Formula Language (M)

I was a bit fast there in accepting the answer as a solution. It was not the solution i needed. I need the value to "reset" on each new item.

 

if we look at row 6, thats a new itemID and the value there sould be 0, not 1800.

bild.png

 

 

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"
Specializing in Power Query Formula Language (M)

Yes, thank you that solved my problem perfectly.

 

And you made a video! Awesome.

 

I have struggled learning the Power Query Formula Language, you know any good tutorials that covers the basics that can help me (and others ofc) to better understand the "flow" of the code.

Some suggestions can be found in the answer to this topic: https://community.powerbi.com/t5/Desktop/Training-in-the-UK/m-p/116687/highlight/true#M49239

 

Otherwise I can recommend the book "M is for (Data) Monkey" by Ken Puls and Miguel Escobar.

 

Personally I have been studying Power Query the hard way going through the information from Microsoft, reading blogs, trying things myself, answering questions on forums, for almost half a year now and continuing...

Specializing in Power Query Formula Language (M)

That was easy. Thanks!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.