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
guilhermemedin1
Regular Visitor

Count same value in interval

Hello,

 

I have a problem to identify how many days a machine operates in a role every time it starts operating. If it was already operating on day one it should not be counted. The output for days in a role would be as follows.

Apreciate any help in this matter.

 

DayMachineStatusStatus ChangeDays in a role
day 1Machine 1Operating 0
day 1Machine 2Operating 0
day 1Machine 3Operating 0

day 2

Machine 1Stopped 0
day 2Machine 2OperatingStarted operating1
day 2Machine 3Operating 1
day 3Machine 1Stopped 0
day 3Machine 2OperatingStarted operating1
day 3Machine 3StoppedStopped0
1 ACCEPTED SOLUTION
guilhermemedin1
Regular Visitor

Thank you for following the question. The ideia is to count the number of days the machines are operating after starting. Machine 2 started operating on day 2 and is still on, hence it counts day 2 and 3.

I was able to come up with this solution.


Days_in_a_role =

VAR dateOperationChange = CALCULATE(MINX('Table','Table'[day]),

FILTER('Table',

'Table'[Status Change] = EARLIER('Table'[Status Change]) &&

'Table'[Status Change] = "Started Operating"

)) return

 

VAR countChangeState = CALCULATE(

COUNTX('Table','Table'[Status Change]),

FILTER('Table',

'Table'[Machine] = EARLIER('Table'[Machine]) &&

'Table'[Status Change] = "Started Operating")

) return

 

VAR firstRowIsOperation = CALCULATE(MINX('Table', 'Table'[Status]), FILTER('Table', 'Table'[Machine] = EARLIER('Table'[Machine]))) = "Started Operating" return

 

VAR checkIsOpational =

IF( firstRowIsOperation && countChangeState = 0, false,

IF(firstRowIsOperation && countChangeState > 0, true,

IF(firstRowIsOperation = false && countChangeState > 0, true, false)

)

) return

 

VAR checkIsOperation = IF(CALCULATE(

MAXX('Table','Table'[Status]),

FILTER('Table',

'Table'[Machine] = EARLIER('Table'[Machine]) &&

'Table'[Status] = EARLIER('Table'[Status]) &&

checkIsOpational = true &&

'Table'[day] >= dateOperationChange

)) = "Started Operating", 1, 0) return checkIsOperation

 

 

 

 

View solution in original post

2 REPLIES 2
guilhermemedin1
Regular Visitor

Thank you for following the question. The ideia is to count the number of days the machines are operating after starting. Machine 2 started operating on day 2 and is still on, hence it counts day 2 and 3.

I was able to come up with this solution.


Days_in_a_role =

VAR dateOperationChange = CALCULATE(MINX('Table','Table'[day]),

FILTER('Table',

'Table'[Status Change] = EARLIER('Table'[Status Change]) &&

'Table'[Status Change] = "Started Operating"

)) return

 

VAR countChangeState = CALCULATE(

COUNTX('Table','Table'[Status Change]),

FILTER('Table',

'Table'[Machine] = EARLIER('Table'[Machine]) &&

'Table'[Status Change] = "Started Operating")

) return

 

VAR firstRowIsOperation = CALCULATE(MINX('Table', 'Table'[Status]), FILTER('Table', 'Table'[Machine] = EARLIER('Table'[Machine]))) = "Started Operating" return

 

VAR checkIsOpational =

IF( firstRowIsOperation && countChangeState = 0, false,

IF(firstRowIsOperation && countChangeState > 0, true,

IF(firstRowIsOperation = false && countChangeState > 0, true, false)

)

) return

 

VAR checkIsOperation = IF(CALCULATE(

MAXX('Table','Table'[Status]),

FILTER('Table',

'Table'[Machine] = EARLIER('Table'[Machine]) &&

'Table'[Status] = EARLIER('Table'[Status]) &&

checkIsOpational = true &&

'Table'[day] >= dateOperationChange

)) = "Started Operating", 1, 0) return checkIsOperation

 

 

 

 

lbendlin
Super User
Super User

Machine 2 never stopped - why is the result 1 ?  Please explain the logic in more detail and maybe provide some more sample data.  

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!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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.