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
condale7
Frequent Visitor

How to find consecutive days with a sale?

SQLBI and Patrick recently came out with a video for consecutive days without a sale (https://www.youtube.com/watch?v=GR9ROCQVyLk). Does anyone have any insights into how one would find consecutive days with a sale? I hope to make a table like below. Any insights would help! Thanks again!

 

Table:

DateSale IDSaleConsecutive Days with Sale
5-10-20211$5751
5-11-20212$4842
5-12-20213$303
5-13-2021  0
5-14-20214$902

1

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi, @condale7 

Please check the below picture and the sample pbix file's link down below. (creating a new measure).

I created a sample pbix file with expanded dates based on the explanation.

 

Picture3.png

 

Sales Consecutive Days =
VAR currentdate =
MAX ( Dates[Date] )
VAR lastblankdate =
MAXX (
FILTER (
ALL ( Dates ),
Dates[Date] <= currentdate
&& ISBLANK ( [Sales Total] )
),
Dates[Date]
)
RETURN
IF (
ISFILTERED ( Dates[Date] ),
IF (
ISBLANK ( [Sales Total] ),
0,
IF (
ISBLANK ( lastblankdate ),
DATEDIFF ( CALCULATE ( MIN ( Dates[Date] ), ALL ( Dates ) ), currentdate, DAY ) + 1,
DATEDIFF ( lastblankdate, currentdate, DAY )
)
)
)
 
 
 

Hi, My name is Jihwan Kim.


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.


Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

4 REPLIES 4
Jihwan_Kim
Super User
Super User

Hi, @condale7 

Please check the below picture and the sample pbix file's link down below. (creating a new measure).

I created a sample pbix file with expanded dates based on the explanation.

 

Picture3.png

 

Sales Consecutive Days =
VAR currentdate =
MAX ( Dates[Date] )
VAR lastblankdate =
MAXX (
FILTER (
ALL ( Dates ),
Dates[Date] <= currentdate
&& ISBLANK ( [Sales Total] )
),
Dates[Date]
)
RETURN
IF (
ISFILTERED ( Dates[Date] ),
IF (
ISBLANK ( [Sales Total] ),
0,
IF (
ISBLANK ( lastblankdate ),
DATEDIFF ( CALCULATE ( MIN ( Dates[Date] ), ALL ( Dates ) ), currentdate, DAY ) + 1,
DATEDIFF ( lastblankdate, currentdate, DAY )
)
)
)
 
 
 

Hi, My name is Jihwan Kim.


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.


Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
mahoneypat
Microsoft Employee
Microsoft Employee

And if you needed the measure version

 

ConsecDays Measure =
VAR thisdate =
    MIN ( Sales2[Date] )
VAR overallmin =
    CALCULATE ( MIN ( Sales2[Date] )ALL ( Sales2 ) )
VAR lastnosales =
    CALCULATE (
        MAX ( Sales2[Date] ),
        ALL ( Sales2 ),
        Sales2[Date] < thisdate,
        ISBLANK ( Sales2[Sale] )
    )
VAR day1 =
    IF ( ISBLANK ( lastnosales )overallmin - 1lastnosales )
RETURN
    IF ( ISBLANK ( MIN ( Sales2[Sale] ) )0DATEDIFF ( day1thisdateDAY ) )

 

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


Hey Pat, 

I almost seem to be there and thank you so much for your help. I provided a screenshot below of the behavior in a table. As you can see, I have the Dayswith0 behaving how I want it. It counts 1,2,3 when there are no sales for three days. However, the consecutive sales DAX measure that you gave me seems to count from the first day. I have tried to manipulate the code to change this behavior but this is a little over my head to be honest. Do you know what in your code could be changed to change this behavior?

 

Thanks again!Consecutive Days.png

 

Connor

 

mahoneypat
Microsoft Employee
Microsoft Employee

Here is a column expression that shows one way to do it.  Replace Sales2 with your actual table name.

 

ConsecDays =
VAR thisdate = Sales2[Date]
VAR overallmin =
    MIN ( Sales2[Date] )
VAR lastnosales =
    CALCULATE (
        MAX ( Sales2[Date] ),
        ALL ( Sales2 ),
        Sales2[Date] < thisdate,
        ISBLANK ( Sales2[Sale] )
    )
VAR day1 =
    IF ( ISBLANK ( lastnosales )overallmin - 1lastnosales )
RETURN
    IF ( ISBLANK ( Sales2[Sale] )0DATEDIFF ( day1thisdateDAY ) )

 

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


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.