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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
gabereal
Frequent Visitor

How do you increment a variable within an each function?

Hello, 

I am trying to see if it's possible to increment a variable within an each function. 

 

 

 

#"Table With EventKey" = Table.AddColumn(#"OriginalData", "EventKeyTable", each let
        AllData = [Custom],
        Counter = 0,
        AddToCounter = () => Counter = Counter +1,
        PrevRowComparison = 
            Table.AddColumn(AllData, "EventKey",
            (x) => 
                let
                    Val = if x[PrevEventNo] = x[EventNo] then null else AddToCounter()
                in 
                    Counter )
            in PrevRowComparison)

 

 

 

 

 

 

How would I go about creating and implementing a function that will increase the Counter value and then I can retrieve the counter value to place into the row?

 

I would like the output to be something like the image show below.

 

IndexEventNoPrevEventNoEventKey
0110
1110
2311
3331
4331
5432
6442
7143
8314

 

1 ACCEPTED SOLUTION
wdx223_Daniel
Community Champion
Community Champion

NewStep=Table.FromRecords(List.Accmulate(Table.ToRecords(PreviousStepName),{{},0},(x,y)=>let a=x{1}+Byte.From(y[EventNo]<>y[PrevEventNo]) in {x{0}&{y&[EventKey=a]},a}){0})

View solution in original post

2 REPLIES 2
gabereal
Frequent Visitor

If anyone is interested, using @wdx223_Daniels solution. I did the following since I have nested tables.

 

 

    #"Table With EventKey" = Table.AddColumn(#"OriginalData", "EventKeyTable", each let
        AllData = [Custom],
        NewStep=Table.FromRecords(List.Accumulate(Table.ToRecords(AllData),{{},0},(x,y)=>let 
                 a=x{1}+Byte.From(y[EventNo]<>y[PrevEventNo]) in {x{0}&{y&[EventKey=a]},a}){0})
            in NewStep)

 

 

wdx223_Daniel
Community Champion
Community Champion

NewStep=Table.FromRecords(List.Accmulate(Table.ToRecords(PreviousStepName),{{},0},(x,y)=>let a=x{1}+Byte.From(y[EventNo]<>y[PrevEventNo]) in {x{0}&{y&[EventKey=a]},a}){0})

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Kudoed Authors