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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
ojmayo21
Frequent Visitor

Consecutive Occurrences and Reset If Value

Trying to create a calculated column that counts the consecutive months that a ID appears but resets when the total usage is greater than 0. The dax code works but doesn't reset back to 0 when the next months total usage > 0, it just continues the streak count.

This is the expected output:

 

 

| Number | Total Usage | Date     | Streak |
|--------|-------------|----------|--------|
| A      | 0           | 1/1/2023 | 1      |
| A      | 0           | 2/1/2023 | 2      |
| A      | 1           | 3/1/2023 | 0      |
| A      | 0           | 4/1/2023 | 1      |
| A      | 0           | 5/1/2023 | 2      |

 

Dax:

 

Streak  = 
VAR CurrID = [Number]
VAR CurrDate = [Date]
VAR PreviousRows =
    FILTER(
        ALL('Table'),
        [Number] = CurrID &&
        [Date] < CurrDate
    )
VAR StreakCount =
    COUNTROWS(
        FILTER(
            ADDCOLUMNS(
                PreviousRows,
                "Usage",
                [Total Usage]
            ),
            [Usage] = 0
        )
    )
RETURN
    IF(
        [Total Usage] = 0,
        1 + StreakCount,
        0
    )

 

 

 

2 REPLIES 2

@amitchandak

I tried several of the solutions but couldn't get it to work. Consecutive column adds increments when Total Usage is 0 sometimes but then others it wouldn't add up. Some dates range from 4/27/23 - 5/27/23 with 0 Total Usage and consecutive will be 1,1.

 

 

| ID Number  | Date       | Total Usage | ResultCount | Latest Consecutive Streak | Months | consecutive |
|------------|------------|-------------|-------------|---------------------------|--------|-------------|
| A| 12/27/2022 | 0           | 1           | 1                         | 1      | 1           |
| A| 1/27/2023  | 200         | 0           | 1                         | 0      | 0           |
| A| 2/27/2023  | 0           | 2           | 1                         | 1      | 1           |
| A| 3/27/2023  | 0           | 3           | 1                         | 1      | 2           |
| A| 4/27/2023  | 0           | 4           | 1                         | 1      | 3           |
| A| 5/27/2023  | 100000      | 0           | 1                         | 0      | 0           |
| A| 6/27/2023  | 0           | 5           | 1                         | 1      | 1           |
| B| 1/27/2023  | 0           | 1           | 1                         | 1      | 1           |
| B| 2/27/2023  | 0           | 2           | 1                         | 1      | 1           |
| B| 3/27/2023  | 0           | 3           | 1                         | 1      | 1           |
| B| 4/27/2023  | 0           | 4           | 1                         | 1      | 1           |
| B| 5/27/2023  | 0           | 5           | 1                         | 1      | 1           |
| B| 6/27/2023  | 0           | 6           | 1                         | 1      | 1           |

consecutive = 
VAR ID = ' Total Usage'[ID Number]
VAR CurrentDate = ' Total Usage'[Date]
VAR PrevDate =
    CALCULATE(
        MAX(' Total Usage'[Date]),
        FILTER(
            ALL(' Total Usage'),
            ' Total Usage'[ID Number] = ID &&
            ' Total Usage'[Date] < CurrentDate &&
            ' Total Usage'[Total Usage] > 0
        )
    )
RETURN
    IF(
        ISBLANK(PrevDate),
        IF(
            ' Total Usage'[Total Usage] = 0,
            1,
            0
        ),
        IF(
            ' Total Usage'[Total Usage] = 0,
            DATEDIFF(PrevDate, CurrentDate, MONTH),
            0
        )
    )

 

 

 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.