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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Count Occurence based on Product ID and Date Range

I want to create a calculated column that would look at the Product ID of that row, look at the Order date, and, looking at the entire list, count the number of product IDs that are greater than the lookback date and less than the order date...

I've tried countX, sum, nothing seems to get me what I would need on a per row basis...

 

Any ideas?

 

Product IDOrder DateLookback
D30-Jun23-Jun
D1-Jul24-Jun
D30-Jun23-Jun
D4-Jul27-Jun
D9-Jul2-Jul
A2-Jul25-Jun
A4-Jul27-Jun
D6-Jul29-Jun
D1-Jul24-Jun
D9-Jul2-Jul
D4-Jul27-Jun
D30-Jun23-Jun
D7-Jul30-Jun
D30-Jun23-Jun
D6-Jul29-Jun
D7-Jul30-Jun
B7-Jul30-Jun
D1-Jul24-Jun
A4-Jul27-Jun
D9-Jul2-Jul
B1-Jul24-Jun
B1-Jul24-Jun
A1-Jul24-Jun
A1-Jul24-Jun
D4-Jul27-Jun
B1-Jul24-Jun
D9-Jul2-Jul
BJun-3125-May
D30-Jun23-Jun
B30-Jun23-Jun
D30-Jun23-Jun
D1-Jul24-Jun
D1-Jul24-Jun
B6-Jul29-Jun
6 REPLIES 6
PaulDBrown
Community Champion
Community Champion

Try

ID count =
VAR _OrderDate =
    MAX ( Table[Order Date] )
VAR _LookBackDate =
    MAX ( Table[Lookback] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( Table[ID] ),
        FILTER (
            ALL ( 'Table' ),
            Table[Order Date] < _OrderDate
                && Table[Lookback] > _LookBackDate
        )
    )




Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






ddpl
Solution Sage
Solution Sage

@Anonymous ,...if I'm understood your concern correctly then following Calculated Column might be your solution...

 

Your Need =
COUNTROWS (
FILTER (
'Table',
'Table'[Product ID] = EARLIER ( 'Table'[Product ID] )
&& 'Table'[Order Date] <= EARLIER ( 'Table'[Order Date] )
&& 'Table'[Order Date] >= EARLIER ( 'Table'[Lookback] )
)
)
ddpl
Solution Sage
Solution Sage

@Anonymous 

 

Could you please share the sample solution with required column in above database.

Anonymous
Not applicable

Maybe this will help....

Product IDID OrderDate Look Back Expected count
a24-Jun17-Jun0
a27-Jun20-Jun2
a2-Jul25-Jun3
a4-Jul27-Jun3
a4-Jul27-Jun4
b29-Jun22-Jun0
b30-Jun23-Jun1
b1-Jul24-Jun3
b1-Jul24-Jun4
b1-Jul24-Jun5
b6-Jul29-Jun6
b7-Jul30-Jun6
d30-Jun23-Jun0
d30-Jun23-Jun2
d30-Jun23-Jun3
d30-Jun23-Jun4
d30-Jun23-Jun5
d30-Jun23-Jun6
d1-Jul24-Jun7
d1-Jul24-Jun8
d1-Jul24-Jun9
d1-Jul24-Jun10
d1-Jul24-Jun11
d4-Jul27-Jun12
d4-Jul27-Jun13
d4-Jul27-Jun14
d6-Jul29-Jun15
d6-Jul29-Jun16
d7-Jul30-Jun17
d7-Jul30-Jun18
d9-Jul2-Jul7
d9-Jul2-Jul8
d9-Jul2-Jul9
d9-Jul2-Jul10
Anonymous
Not applicable

Hi @Anonymous ,

 

Could you show me more details about your calculation? I am confused about how to get expected count.

Product ID ID Order Date Look Back  Expected count
a 24-Jun 17-Jun 0
a 27-Jun 20-Jun 2

Do you want to count the Product ID which is greate than look back date and less than Order date?

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Thanks Rico...

 

So, yes, what I need to do, per row, I'm looking at the product ID, and the order date (there's actually a date/time for the Order Date, that might make it easier to understand)... 

I'm taking the order date/time, and subtracting 7 from the date, so I can say "within the past seven days (including this transaction), how many times did this product get ordered.

For the first entry, Product A was ordered on 6/24, the "Lookback date" is 7 days back, so I start looking anything from 6/17 through 6/24..  And count how many Product A's were ordered.

Since I have no orders within that time frame for Product A, that would be 1 (now that I think about it since I have to count that order with it)...

Next row, there's a Product A ordered on 6/27, lookback date is 6/20...

Counting all of the Product A orders between 6/20 - 6/27, that should give me 2..

 

etc etc... 

 

Hope that makes sense...

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.