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
Anonymous
Not applicable

Count number of zeroes preceding a positive number

Hello, I have a data column that looks like this: 0, 0, -1, -2, 0, 0, 1, 2, 0, 1, etc
How do I count the number of zeroes preceding a positive number (in this case, the answer should be 2). Thanks you!
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Paste this into the Advanced Editor in Power Query and see it happening:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Rc3BDcAgDEPRXXLmQBwodJaI/dcopZV9e4pi/UxzK1ZtlTRs+VFQbQtHnX8XNajJxc2bV1EVhxhiEzub/qbi4xCnqBpUw19bDw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Index = _t, Number = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Number", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(
        #"Changed Type", 
        "Condition Met?", 
        (r) => 
            let
                currentNumberGreaterThan0 = r[Number] > 0,
                previousNumberIs0 = 
                    Table.SelectRows(
                        #"Changed Type",
                        each [Index] = r[Index] - 1
                    )[Number]{0}? = 0,
                conditionMet =
                    currentNumberGreaterThan0 and previousNumberIs0,
                output = if conditionMet then "Yes" else "No"
            in
                output
    )
in
    #"Added Custom"

 

Best

D

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Do it in Power Query. Not in DAX.

Best
D
Greg_Deckler
Super User
Super User

You would need an Index column or a date column so that you would know what is "before".

 

New Column =
  IF([Column] <= 0,
    BLANK(),
    VAR __LastNonZero = MAXX(FILTER('Table',[Index] < EARLIER([Index]) && [Column] <> 0),[Index])
    RETURN [Index] - __LastNonZero - 1
  )

@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler

Thanks for your repsonse, but I don't think this is what I needed. I am looking to create the "first change?" column. 

 

DateIndexNumberFirst change?
1/4/201610No
1/11/201621Yes
1/18/201631No
1/25/201642No
2/1/201650No
4/16/201860No
4/30/201870No
5/7/201881Yes
5/14/201890No
5/21/2018100No
5/28/2018110No
6/4/2018120No
6/11/2018130No
6/18/2018140No
6/25/2018152Yes
7/2/2018163No
7/9/2018173No
7/16/2018183No
7/23/2018190No
7/30/2018200No
8/6/2018210No
Anonymous
Not applicable

Paste this into the Advanced Editor in Power Query and see it happening:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Rc3BDcAgDEPRXXLmQBwodJaI/dcopZV9e4pi/UxzK1ZtlTRs+VFQbQtHnX8XNajJxc2bV1EVhxhiEzub/qbi4xCnqBpUw19bDw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Index = _t, Number = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Number", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(
        #"Changed Type", 
        "Condition Met?", 
        (r) => 
            let
                currentNumberGreaterThan0 = r[Number] > 0,
                previousNumberIs0 = 
                    Table.SelectRows(
                        #"Changed Type",
                        each [Index] = r[Index] - 1
                    )[Number]{0}? = 0,
                conditionMet =
                    currentNumberGreaterThan0 and previousNumberIs0,
                output = if conditionMet then "Yes" else "No"
            in
                output
    )
in
    #"Added Custom"

 

Best

D

Anonymous
Not applicable

Unbelievable! Thank you!

Hi @Greg_Deckler ,

 

Interesting solution! 🙂

 

Thanks,

Pragati

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

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.

Top Solution Authors