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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
xjsuarexcx
Regular Visitor

counting consecutive identical values - calculated column

Dear ones,
I need your support, thank you very much!
I want to list (in the "Consecutive Imbalance" column) consecutive records that have a value equal to 1 (from the "Imbalance" column) It is also critical that the entire enumeration calculation must be RESTARTed by changing the value of the "Attribute 1" column.
In the Photo is the column "Consecutive Imbalance" which is what you want to obtain.
 
xjsuarexcx_0-1663626876623.png

 

 
I found some solutions but they consume too much memory

 

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @xjsuarexcx ,

If you need a calculated column, here's my solution.

Create two calculated columns.

Flag =
MAXX (
    FILTER (
        'Query1',
        'Query1'[Timestamp] <= EARLIER ( 'Query1'[Timestamp] )
            && 'Query1'[Desbalance] = 0
    ),
    'Query1'[Timestamp]
)
Consecutive Imbalance =
IF (
    'Query1'[Desbalance] = 1,
    RANKX (
        FILTER (
            'Query1',
            'Query1'[Desbalance] = 1
                && 'Query1'[Flag] = EARLIER ( 'Query1'[Flag] )
        ),
        'Query1'[Timestamp],
        ,
        ASC
    )
)

Get the correct result.

vkalyjmsft_0-1663754406092.png

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

2 REPLIES 2
v-yanjiang-msft
Community Support
Community Support

Hi @xjsuarexcx ,

If you need a calculated column, here's my solution.

Create two calculated columns.

Flag =
MAXX (
    FILTER (
        'Query1',
        'Query1'[Timestamp] <= EARLIER ( 'Query1'[Timestamp] )
            && 'Query1'[Desbalance] = 0
    ),
    'Query1'[Timestamp]
)
Consecutive Imbalance =
IF (
    'Query1'[Desbalance] = 1,
    RANKX (
        FILTER (
            'Query1',
            'Query1'[Desbalance] = 1
                && 'Query1'[Flag] = EARLIER ( 'Query1'[Flag] )
        ),
        'Query1'[Timestamp],
        ,
        ASC
    )
)

Get the correct result.

vkalyjmsft_0-1663754406092.png

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

jbwtp
Memorable Member
Memorable Member

Hi @xjsuarexcx,

 

Try something like this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc/LDcAgDATRXnzmwBryoRZE/20ERdFObnNZPXvOUJSoscqMdDVXdx279Nbpuly3a7hUPRaKYISjzgxJUMISmAbXo+XvJ7RsnuWnrQc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Items = _t, Desbalance = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Items", Int64.Type}, {"Desbalance", Int64.Type}}),
    Process = List.Accumulate(Table.ToRecords(#"Changed Type"), {}, (a, n)=> a & {if n[Desbalance] = 0 then Record.AddField(n, "Consec", 0) else Record.AddField(n, "Consec", List.Last(a)[Consec]+1)}),
    Output = Table.FromRecords(Process, Value.Type(Table.AddColumn(#"Changed Type", "Consec", each null, type number)))
in
    Output

 

Kind regards,

John

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors
Top Kudoed Authors