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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Calva
New Member

Runnig totals that reset by value changes on other column

Hello,

I need help to calculate how long (seconds) a change of a parameter (column Presenca_Skid_RB02) lasts (updating every second). My original table is this:

Calva_1-1659624409620.png

I've got a calendar with date and time increasing by 1 second. The parameters may get 0 or 1, it dependes on the conditions of the machine over time. I need to get the exact timestamp after 120 seconds that the parameter went to "1", and, the exact timestamp after 10 seconds it went to "0".... repeatdly.

 

I tried to count an index for 0 and 1 as follows:

 

= Table.Group(#"Linhas Filtradas1", {"Presenca_Skd_RB02"}, {{"Contagem", each Table.AddIndexColumn(_,"Index",1,1), type table }})

 

After expanded the result is: 

Calva_0-1659624246783.png

It counts the indexes separatedly for "0" and "1". It works fine, BUT i need it to reset the count for each change. Ex: In the picture, when it changed from 1 to 0 the index went to "1" (good) but when the value gets back to 1 I need it to index "1" again, not continue the old index "457".

 

I hope I've been clear on my needs. Better if the solution is on PowerQuery!

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

I answered a similar question here:

https://community.powerbi.com/t5/Power-Query/How-to-count-sequence-per-group/m-p/2222977

 

Here's a cleaner example query though:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc7RCcAwCATQXfwO6Gkj7Swh+6/RJLSk2k8f3J2tEagQGKwiQr28oBGUbYMs8AxIGeOaAdcWXWKIUvnIAI81zj/Qz7QNOcMzUyDjfqjf", BinaryEncoding.Base64), Compression.Deflate)), type table [ID = Int64.Type, Date = Date.Type]),
    T = Table.Buffer(Table.AddIndexColumn(Source, "Order", 0, 1, Int64.Type)),
    AddIndex = Table.FromRecords(
        List.Generate(
            () => [Order = 0, Index = 1],
            each [Order] < Table.RowCount(T),
            each [
                    Order = [Order] + 1,
                    Index = if T{Order}[ID] = T{[Order]}[ID] then [Index] + 1 else 1
                ]
        ),
        type table [Order = Int64.Type, Index = Int64.Type]
    ),
    #"Merged Queries" = Table.NestedJoin(T, {"Order"}, AddIndex, {"Order"}, "Index", JoinKind.LeftOuter),
    #"Expanded NewID" = Table.ExpandTableColumn(#"Merged Queries", "Index", {"Index"}, {"Index"})
in
    #"Expanded NewID"

 

This query ^^ starts with the left two columns and adds the other two.

AlexisOlson_0-1659636624034.png

View solution in original post

2 REPLIES 2
AlexisOlson
Super User
Super User

I answered a similar question here:

https://community.powerbi.com/t5/Power-Query/How-to-count-sequence-per-group/m-p/2222977

 

Here's a cleaner example query though:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc7RCcAwCATQXfwO6Gkj7Swh+6/RJLSk2k8f3J2tEagQGKwiQr28oBGUbYMs8AxIGeOaAdcWXWKIUvnIAI81zj/Qz7QNOcMzUyDjfqjf", BinaryEncoding.Base64), Compression.Deflate)), type table [ID = Int64.Type, Date = Date.Type]),
    T = Table.Buffer(Table.AddIndexColumn(Source, "Order", 0, 1, Int64.Type)),
    AddIndex = Table.FromRecords(
        List.Generate(
            () => [Order = 0, Index = 1],
            each [Order] < Table.RowCount(T),
            each [
                    Order = [Order] + 1,
                    Index = if T{Order}[ID] = T{[Order]}[ID] then [Index] + 1 else 1
                ]
        ),
        type table [Order = Int64.Type, Index = Int64.Type]
    ),
    #"Merged Queries" = Table.NestedJoin(T, {"Order"}, AddIndex, {"Order"}, "Index", JoinKind.LeftOuter),
    #"Expanded NewID" = Table.ExpandTableColumn(#"Merged Queries", "Index", {"Index"}, {"Index"})
in
    #"Expanded NewID"

 

This query ^^ starts with the left two columns and adds the other two.

AlexisOlson_0-1659636624034.png

Greg_Deckler
Community Champion
Community Champion

@Calva I don't have a Power Query solution but a resetting counter index in DAX is the Cthulhu pattern. 

Cthulhu - Microsoft Power BI Community



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Kudoed Authors