Forum Discussion
Count changes in a value/column
I have a set of data and I need to count the amount of times there were changes in a value.
My data is setup similar to this...
TableA
| DateTime | Part | Quantity |
| 9/20/2021 12:00:00 am | 8250 | 720 |
| 9/20/2021 1:00:00 am | 9142 | 84 |
| 9/20/2021 2:00:00 am | 8250 | 800 |
| 9/20/2021 3:00:00 am | 8250 | 95 |
| 9/20/2021 4:00:00 am | 7748 | 91 |
| 9/20/2021 5:00:00 am | 9142 | 700 |
For this table, the returned value would be 5.
I'm also linking this to another table which has more details regarding the parts. This table is setup similar to this:
TableB
| Part | TD | ST |
| 8250 | 10000 | 7314 |
| 9142 | 10001 | 7314 |
| 7748 | 10000 | 9548 |
| 4152 | 10002 | 9548 |
| 2829 | 10002 | 4314 |
| 5416 | 66148 | 7314 |
I'm linking table A and B through a relationship on "part." I'm pulling in the TD and ST values from Tableb and using them in the same visualization.
I need unique values for how many times parts were changed, how many times TD changed, and how many times ST changed.
Based on the supplied data, the values should be 5 (part changes), 4 (td changes), and 4 (st changes).
I've tried a few formulas I have found online but I can't get anything to work correctly, or to not just return count/distinct count.
This turned out to be context issue that was solved by using ALLSELECTED versus ALL:
Changed Column = VAR __DateTime = [DateTime] VAR __PreviousDateTime = MAXX(FILTER(ALLSELECTED('Table'),[DateTime]<__DateTime),[DateTime]) VAR __PreviousPart = MAXX(FILTER(ALLSELECTED('Table'),[DateTime]=__PreviousDateTime),[Part]) RETURN IF([Part]=__PreviousPart,0,1)
15 Replies
- Greg_DecklerCommunity Champion
Locco So would this be correct for part changes, I assume it needs to account for both changes in parts and quantity?
Parts Changes = COUNTROWS( DISTINCT( SELECTCOLUMNS('TableA',"Part",[Part],"Quantity",[Quantity]) ) ) - 1I'm not grasphing how to get the changes for TD and ST numbers you presented given your sample data, can you explain?
- LoccoHelper III
Thanks Greg_Deckler,
That did not work, it only returns a distinct count and not how many times parts actually changed. If a part repeats later then that part isn't counted.
It's possible for a part to repeat multiple times in a column, in this case it would still count as a "1" since the part didn't actually change until a new part was loaded. If that part repeats later in the day, then it would count towards the change total.
For the TD and ST counts, I have merged the queries so this may no longer be an issue and if I get something to correctly count the part changes then that should also work for TD and ST changes.
Very similar issue here, but the solution there I could not get to work for me:
https://community.powerbi.com/t5/Desktop/Count-number-of-changes-of-the-value/m-p/487205
- Greg_DecklerCommunity Champion
Locco OK, try the MTBF approach. See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/339586.
The basic pattern is:
Column =
VAR __Current = [Value]
VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])
VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
__Current - __PreviousIn your case:
Changed Column = VAR __DateTime = [DateTime] VAR __PreviousDateTime = MAXX(FILTER('Table',[DateTime]<__DateTime),[DateTime]) VAR __PreviousPart = MAXX(FILTER('Table',[DateTime]=__PreviousDateTime),[Part]) RETURN IF([Part]=__PreviousPart,0,1)