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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

How to count rows with row calculation?

Hey guys, 

 

I want to count accumulated number of rows in this situation: 

When RELAY = ON and Temperature goes down by minimum 2 degrees F until Temperature starts rising in temperature again and RELAY = OFF, 

take an example from table is rows in red here: 

Date TimeTemperatureMode Relay
5/8/201911:01:3273.24CON
5/8/201911:02:0262.44CON
5/8/201911:02:3258.67CON
5/8/201911:03:0258.18CON
5/8/201911:03:3257.53CON
5/8/201911:04:0256.9CON
5/8/201911:04:3256.45CON
5/8/201911:05:0259.75COFF

 

How I can write a query in new measure?

 

Thank you, 

 

Mengyang

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

Hi @Anonymous ,

 

To create a measure as below.

 

Measure = 
VAR a =
    ADDCOLUMNS (
        'Table',
        "a",
        VAR time1 = 'Table'[Time]
        VAR time2 =
            CALCULATE (
                MAX ( 'Table'[Time] ),
                FILTER (
                    'Table',
                    'Table'[Date ] = EARLIER ( 'Table'[Date ] )
                        && 'Table'[Time] < time1
                )
            )
        VAR pre =
            CALCULATE (
                MAX ( 'Table'[Temperature] ),
                FILTER (
                    'Table',
                    'Table'[Time] = time2
                        && 'Table'[Date ] = EARLIER ( 'Table'[Date ] )
                )
            )
        RETURN
            IF ( pre <> BLANK () && pre - 'Table'[Temperature] > 1, 1, BLANK () )
    )
RETURN
    COUNTROWS ( FILTER ( a, [a] = 1 ) )

 

Capture.PNG

For more details, please check the pbix as attached.

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

4 REPLIES 4
v-frfei-msft
Community Support
Community Support

Hi @Anonymous ,

 

To create a measure as below.

 

Measure = 
VAR a =
    ADDCOLUMNS (
        'Table',
        "a",
        VAR time1 = 'Table'[Time]
        VAR time2 =
            CALCULATE (
                MAX ( 'Table'[Time] ),
                FILTER (
                    'Table',
                    'Table'[Date ] = EARLIER ( 'Table'[Date ] )
                        && 'Table'[Time] < time1
                )
            )
        VAR pre =
            CALCULATE (
                MAX ( 'Table'[Temperature] ),
                FILTER (
                    'Table',
                    'Table'[Time] = time2
                        && 'Table'[Date ] = EARLIER ( 'Table'[Date ] )
                )
            )
        RETURN
            IF ( pre <> BLANK () && pre - 'Table'[Temperature] > 1, 1, BLANK () )
    )
RETURN
    COUNTROWS ( FILTER ( a, [a] = 1 ) )

 

Capture.PNG

For more details, please check the pbix as attached.

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

Thank you, this solution meets most of needs, 

 

but do you know why I get error "can not find table "ON"" when I add this as another condition in IF? 

 

Mengyang

Nathaniel_C
Community Champion
Community Champion

Hi @Anonymous ,
Try this: Create two calculated columns

Previous temp = 

var _currTime = myTable[Time]
var _prevTime = CALCULATE(MAX(myTable[Time]),ALLEXCEPT(myTable,myTable[Time]),myTable[Time]<_currTime)
var _currTemp = myTable[Temperature]
var _prevTemp = CALCULATE(MAX(myTable[Temperature]),ALLEXCEPT(myTable,myTable[Time]),myTable[Time] = _prevTime)

return _prevTemp

 

Count = IF(myTable[Relay] = "on" && myTable[Previous temp]-myTable[Temperature]>=2,1,0)

Create the measure:

Countrows = 
sum(myTable[Count]

)



Relay.PNG

============================================== 

Relay1.PNG

 
Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Hi, 

thank you but I got an error "token eof expected" when creating columns "Previous Temp" and "count" by editing query - custom columns. 

Mengyang

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors