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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Anthony_A
Regular Visitor

IF ELSE and Count

Hello, 

 

I am trying to create a column that will count every time a specific ID has a value larger than 0, ELSE if the row below or above is the same ID, do not count. below is an example of the data i am working with. PLEASE HELP! 

 

Anthony_A_0-1701449613680.png

 

1 ACCEPTED SOLUTION

If you are relying on direct query then you may have to do this transformation in SQL instead.  Otherwise, you can switch over to import mode for this query.

 

There is also an alternative way of doing this that may be supported by direct query.  Let me know if you would like to see an alternative way.  It involves created an index starting from 0, then another starting from 1, a self join, and then comparing the values from the previous row after the join.

View solution in original post

7 REPLIES 7
Anthony_A
Regular Visitor

Its not allowing because its not supported in direct query. 

If you are relying on direct query then you may have to do this transformation in SQL instead.  Otherwise, you can switch over to import mode for this query.

 

There is also an alternative way of doing this that may be supported by direct query.  Let me know if you would like to see an alternative way.  It involves created an index starting from 0, then another starting from 1, a self join, and then comparing the values from the previous row after the join.

I was able to accomplish in SQL. much easier! thank you for your help. 

Anthony_A
Regular Visitor

How does this work with thousands of IDs? 

I think it should run effectively as long as you buffer the step right before generating the list.

Anthony_A
Regular Visitor

yes!! wow GENIUS! LET ME TRY THIS. MAY HAVE ADDITIONAL QUESTIONS. 

spinfuzer
Solution Sage
Solution Sage

something like this?  Gotta make sure you sorted by the id and the value first.

 

spinfuzer_0-1701452838557.png

 

 

let
    Source = Table.FromColumns(
        {
            {null,null,1,2,2,3,3,3,4,4,4,5,5,5,5},
            {null,null,0,1,1,0,3,3,2,2,1,1,1,1,0}
        },
        {"id col","val column"}
    ),
    buffer = Table.Buffer(Source), // optional buffer to potentially increase speed of query
    CountList =
        List.Generate( () =>
            [
                n = 0, 
                prevVal = {null,null}, 
                currVal = {buffer[id col]{0} , buffer[val column]{0}}
            ],
        each [n] < Table.RowCount(buffer),
        each 
            [
                n = [n] + 1,
                prevVal =  
                    { 
                        buffer[id col]{[n]} , 
                        buffer[val column]{[n]}
                    },
                currVal =  
                    { 
                        buffer[id col]{[n]+1}, 
                        buffer[val column]{[n]+1} 
                    }
            ],
        each if [currVal] <> [prevVal] and [currVal]{1} > 0 then 1 else 0


        ),
    AddCountColumn = Table.FromColumns(Table.ToColumns(buffer)&{CountList}, Table.ColumnNames(buffer)&{"ct"})

in
    AddCountColumn

 

 

Helpful resources

Announcements
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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

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