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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

Dax consecutive Count by months

Hello all,
i'm sorry if i posted this in a wrong category (i'm new in this community)
I would like to ask for a help.
I'm trying to count consecutive months.

The logic is: 
if the Latency Value > Target for 2 and more months in a row then write "Watch out" 

I've got a problem also when the year turns into new one.


For example if target was not reached 3 months in a row (Nov 2021, Dec 2021, Jan 2022) i am getting only consecutive for year 2021 of "0" and "1" but the 3rd number is (-10) instead of "2". - the right consecutive should be: target not reached: Nov 2021 (0) , Dec 2021 (1) , Jan 2022 (2)

 Below is how i would like the report to be shown

Stashinho_0-1646985119116.png


Thank you for every help





1 ACCEPTED SOLUTION
v-kkf-msft
Community Support
Community Support

Hi @Anonymous ,

 

Please create a date table and the relationship.

 

vkkfmsft_0-1647414709683.png

vkkfmsft_1-1647414738244.png

 

Then create these measures:

 

Measure = 
IF ( SUM ( 'Table'[Latency Value] ) > SUM ( 'Table'[Target value] ), 1, 0 )
Measure2 = 
VAR tab =
    SUMMARIZE ( ALLSELECTED ( 'Date' ), 'Date'[Year_Month], "M", [Measure] )
VAR mindate =
    CALCULATE (
        MAX ( 'Date'[Year_Month] ),
        ALLSELECTED ( 'Date' ),
        FILTER ( tab, 'Date'[Year_Month] < MAX ( 'Date'[Year_Month] ) && [M] = 0 )
    )
RETURN
    IF (
        [Measure] = 1,
        SUMX (
            FILTER (
                tab,
                'Date'[Year_Month] <= MAX ( 'Date'[Year_Month] )
                    && 'Date'[Year_Month] > mindate
            ),
            [M]
        ) - 1
    )

vkkfmsft_2-1647414839025.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

10 REPLIES 10
v-kkf-msft
Community Support
Community Support

Hi @Anonymous ,

 

Please create a date table and the relationship.

 

vkkfmsft_0-1647414709683.png

vkkfmsft_1-1647414738244.png

 

Then create these measures:

 

Measure = 
IF ( SUM ( 'Table'[Latency Value] ) > SUM ( 'Table'[Target value] ), 1, 0 )
Measure2 = 
VAR tab =
    SUMMARIZE ( ALLSELECTED ( 'Date' ), 'Date'[Year_Month], "M", [Measure] )
VAR mindate =
    CALCULATE (
        MAX ( 'Date'[Year_Month] ),
        ALLSELECTED ( 'Date' ),
        FILTER ( tab, 'Date'[Year_Month] < MAX ( 'Date'[Year_Month] ) && [M] = 0 )
    )
RETURN
    IF (
        [Measure] = 1,
        SUMX (
            FILTER (
                tab,
                'Date'[Year_Month] <= MAX ( 'Date'[Year_Month] )
                    && 'Date'[Year_Month] > mindate
            ),
            [M]
        ) - 1
    )

vkkfmsft_2-1647414839025.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hello all,
i have one more question.
Is it possible to use this measure also as a slicer ( see in the picture below)
I created new table where i would like to filter by "Status" - the statuses will be only
"Breached 2+" and unfiler (means "All")

quest.jpg

 

Anonymous
Not applicable

Hello @v-kkf-msft ,
unbelievable !! 
This works just like a magic !

Thank you very much for your help ! 😊

mahoneypat
Microsoft Employee
Microsoft Employee

Your sample data are missing rows for Feb and Apr. Is your logic based on 2+ consecutive values or actually calculate the # of months difference?

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Hello Pat,
data are only on monthly basis.

I have multiple files with data, in the filename there is month and year (which is not shown in report - so i putted there the 1st date of the month in custom column) , then i appended all files
I created a column with date of 1st date in the specific month.
There can be also year/month.
i would like to count only consecutive months not days.
The problem is when there is turning year over ... 
If latency is bigger than target for 2and more months in a row i would like to demonstrate as i showen above in the picture.
Nov 2021 - fail 1, Dec 2021 - fail 2, Jan 2022 - fail 3 (should show the fail as 3rd month in a row)


Anonymous
Not applicable

Yes, the logic is 2+ month
Stashinho_0-1646985119116.png

Since your data were an image (couldn't copy/paste), I used a model I user for demos, but here is a DAX pattern that you can adapt.

 

mahoneypat_0-1647176602073.png

 

Two Months Over Target =
VAR thismonthindex =
MIN( 'Date'[Months from Now] )
VAR prev2monsummary =
CALCULATETABLE(
ADDCOLUMNS( DISTINCT( 'Date'[Months from Now] ), "cSales", [Total Sales] ),
ALL( 'Date' ),
'Date'[Months from Now]
IN { thismonthindex, thismonthindex - 1 }
)
RETURN
IF(
COUNTROWS( FILTER( prev2monsummary, [cSales] > 3700 ) ) = 2,
"Watch Out",
"OK"
)

 

Note that I used the Date table I mentioned previously found here (that has the Months From Now column).

No Sort Date Tables! – Hoosier BI

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Hello Pat,
i think i dont get it.
I don't know where are you dragging those 3700

Stashinho_0-1647266215298.png

Please see the data for copy paste below

          My desired consecutive
OriginDestinationLatency ValuePacket Loss ValueJitter ValueDateKeyTarget value  ConsecutiveStatus
Site 1Site 1114.7800.0101.11.2021Site 1Site 117.4  0OK
Site 1Site 1114.1100.0201.12.2021Site 1Site 117.4  1Watch out
Site 1Site 1114.1100.0201.01.2022Site 1Site 117.4  2Watch out
Site 1Site 1114.1100.0201.03.2022Site 1Site 117.4  0OK
Site 1Site 1114.1100.0201.05.2022Site 1Site 117.4  0OK
Site 1Site 1114.1100.0201.06.2022Site 1Site 117.4  1Watch out

Did you add the Date table to your model? The 3700 is just a value that would replicate your scenario with the mock dataset I used.  You would replace the table/column names for your date, and 3700 with your target value.

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Hello Pat,
i tried to use also your code but when the year turns over it is not showing me the right data.
Please see below.
Do you also have an idea how to put the measure as a slicer and let the text "Watch out" in the column ? 
I tried many oprions but without success

jp.jpg

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.