Forum Discussion

cinderelly678's avatar
cinderelly678
Frequent Visitor
3 years ago
Solved

DAX Measure Help: Counting Status Changes Over Time

I am having some trouble writing a measure which would count the number of times a status has changed for a particular item. My source is an SDC Type 2 table containing all history of status changes. 

 

I want to make a measure that ranks the count of status changes over time. In the sample data, the grouping is by Fruit, _Timestamp, _Operation, Consumable Status, and Status Change Date. I want to make a column called "Status Change Count" with the below output.

 

 

Could anyone help me out with the DAX to accomplish this? Thank you in advance for your help.

 

Attached is my sample data. 

 

Fruit_Timestamp_OperationConsumable StatusStatus Change Date
Apples8/8/2023 17:46DSpoiled Fruit - Discard8/8/2023 0:00
Apples8/7/2023 17:46UUndocked11/8/2022 0:00
Apples7/25/2023 17:49UUndocked11/8/2022 0:00
Apples5/31/2023 17:39UUndocked11/8/2022 0:00
Apples5/25/2023 17:38UUndocked11/8/2022 0:00
Apples5/22/2023 17:36UUndocked11/8/2022 0:00
Apples5/18/2023 17:39UUndocked11/8/2022 0:00
Apples3/17/2023 17:38UUndocked11/8/2022 0:00
Apples3/3/2023 17:33UUndocked11/8/2022 0:00
Apples2/1/2023 17:06UUndocked11/8/2022 0:00
Apples12/27/2022 17:07UUndocked11/8/2022 0:00
Apples12/6/2022 17:07UUndocked11/8/2022 0:00
Apples11/8/2022 17:07UUndocked11/8/2022 0:00
Apples10/28/2022 14:54DShipped to Store10/27/2022 0:00
Apples10/28/2022 14:54IShipped to Store10/27/2022 0:00
Apples10/27/2022 17:06DShipped to Store10/27/2022 0:00
Apples10/27/2022 17:06IShipped to Store10/27/2022 0:00
Apples10/27/2022 17:05IShipped to Store10/27/2022 0:00
Apples10/26/2022 17:06UPayment Received10/26/2022 0:00
Apples10/24/2022 17:06UPurchase Order Approved10/24/2022 0:00
Apples10/24/2022 17:05IPurchase Order Received10/24/2022 0:00
Oranges8/7/2023 17:46UUndocked11/8/2022 0:00
Oranges7/25/2023 17:49UUndocked11/8/2022 0:00
Oranges5/31/2023 17:39UUndocked11/8/2022 0:00
Oranges5/25/2023 17:38UUndocked11/8/2022 0:00
Oranges5/22/2023 17:36UUndocked11/8/2022 0:00
Oranges5/18/2023 17:39UUndocked11/8/2022 0:00
Oranges3/17/2023 17:38UUndocked11/8/2022 0:00
Oranges3/3/2023 17:33UUndocked11/8/2022 0:00
Oranges2/1/2023 17:06UUndocked11/8/2022 0:00
Oranges12/27/2022 17:07UUndocked11/8/2022 0:00
Oranges12/6/2022 17:07UUndocked11/8/2022 0:00
Oranges11/8/2022 17:07UUndocked11/8/2022 0:00
Oranges10/28/2022 14:54DShipped to Store10/27/2022 0:00
Oranges10/28/2022 14:54IShipped to Store10/27/2022 0:00
Oranges10/27/2022 17:06DShipped to Store10/27/2022 0:00
Oranges10/27/2022 17:06IShipped to Store10/27/2022 0:00
Oranges10/27/2022 17:05UShipped to Store10/27/2022 0:00
Oranges10/26/2022 17:06UPayment Received10/26/2022 0:00
Oranges10/24/2022 17:06UPurchase Order Approved10/24/2022 0:00
Oranges10/24/2022 17:05IPurchase Order Received10/24/2022 0:00
  • cinderelly678 Try:

    Measure = 
        VAR __Fruit = MAX('Table'[Fruit])
        VAR __TS = MAX('Table'[Timestamp])
        VAR __Table = DISTINCT(SELECTCOLUMNS(FILTER(ALL('Table'), [Fruit] = __Fruit && [Timestamp] <= __TS), "__Status", [Consumable Status]))
        VAR __Result = COUNTROWS(__Table)
    RETURN
        __Result

4 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    cinderelly678 Try:

    Measure = 
        VAR __Fruit = MAX('Table'[Fruit])
        VAR __TS = MAX('Table'[Timestamp])
        VAR __Table = DISTINCT(SELECTCOLUMNS(FILTER(ALL('Table'), [Fruit] = __Fruit && [Timestamp] <= __TS), "__Status", [Consumable Status]))
        VAR __Result = COUNTROWS(__Table)
    RETURN
        __Result
  • cinderelly678's avatar
    cinderelly678
    Frequent Visitor

    Greg_Deckler , thank you very much, that worked like a charm!

     

    Now I have a secondary question. Let's say I wanted to iterate over the results of the Measure to flag when the measure has incremented by one over the previous value. How could I achieve that?