'PowerBI - increase/decrease by one if condition is met

My initial table looks as follows:

Time-ID Status Product
1 start a
2 end a
3 start b
4 start b
5 end b
6 end b
7 start a
8 start b
9 end a
10 start a
11 end b
12 start a
13 start a

Now I want to add a new column [New], which counts up by one when the status is "start" (+1) and reduces the counter by one (-1) when the status is "end".

Additional difficulty: in the product column there are two different products. i.e. a separate meter for each product. The logic I want to achieve is something like the following:

Time-ID Status Product New
1 start a 1
2 end a 0
3 start b 1
4 start b 2
5 end b 1
6 end b 0
7 start a 1
8 start b 1
9 end a 0
10 start a 1
11 end b 0
12 start a 2
13 start a 3

Actually I have more than thousands of lines. so I should rather do it in M ​​than in DAX right?

I think I have to use group by anyway, because i have two different products (a, b), right?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source