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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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.


Go to My LinkedIn Page


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.


Go to My LinkedIn Page


mahoneypat
Employee
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
Employee
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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors