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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
MSW
Helper I
Helper I

Finding the number of Occurrences Prior to a Date for each year by unique ID

Help, 

 

I need to find a way to count the number of occurances an ID receives a code prior from the first date the code is sent to the first purchase by the customer. 

 

Table 1 Example:

Customer IDCodeEarliest Code Sent Date 
1230011/1/2020
1230011/1/2020
1230021/1/2019
4560013/1/2020
4560023/1/2019
4560026/1/2019
7890011/1/2020
7890021/1/2019
7890013/1/2018

 

Table 2: 

customerIDPurchase Date

Earliest Purchase Date

Outcome from formula

1231/10/20201/10/20202
1232/1/20201/10/20202
1232/1/20192/1/20191
4567/1/20197/1/20192
45610/1/20197/1/20192
4561/2/20201/2/20201
7894/1/20204/1/20201
7893/1/20183/1/2018

1

7895/1/20195/1/2019

1

 

The goal is to try to determine how many codes a customer receives before their first purchase each year. Any help would be much appreciated. Bonus if I can figure out how many codes are received between send dates and purchase dates. 

 

Thank you!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @MSW ,

 

Please check the formulas.

Column = CALCULATE(MIN(Table1[Earliest Code Sent Date ]),FILTER(Table1,Table1[Customer ID]=Table2[customerID]&&YEAR(Table1[Earliest Code Sent Date ])=YEAR(Table2[Purchase Date])))

Column 2 = CALCULATE(COUNT(Table1[Code]),FILTER(Table1,Table1[Customer ID]=Table2[customerID]&&Table1[Earliest Code Sent Date ]>=Table2[Column]&&Table1[Earliest Code Sent Date ]<=Table2[Earliest Purchase Date]))

vjaywmsft_0-1649229731255.png

 

Best Regards,

Jay

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @MSW ,

 

Please check the formulas.

Column = CALCULATE(MIN(Table1[Earliest Code Sent Date ]),FILTER(Table1,Table1[Customer ID]=Table2[customerID]&&YEAR(Table1[Earliest Code Sent Date ])=YEAR(Table2[Purchase Date])))

Column 2 = CALCULATE(COUNT(Table1[Code]),FILTER(Table1,Table1[Customer ID]=Table2[customerID]&&Table1[Earliest Code Sent Date ]>=Table2[Column]&&Table1[Earliest Code Sent Date ]<=Table2[Earliest Purchase Date]))

vjaywmsft_0-1649229731255.png

 

Best Regards,

Jay

lbendlin
Super User
Super User

Your expected outcome is incorrect for two entries, as the code was sent after the first purchase that year.

 

Table 1

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRMjAwBJKG+ob6RgZGBkqxOkRIGMElDC3BEiamZnAdxsg6YBJGcAk0HSAJM2QJcwtL7JbDJDAsR9YBtcNCKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer ID" = _t, Code = _t, #"Earliest Code Sent Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{ {"Earliest Code Sent Date", type date}})
in
    #"Changed Type"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Rename to Table 1

Table 2

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRMjAwBJKG+ob6RgZGBkqxOkRIGMElDC3BEiamZnAdxsg6YBJGcAk0HSAJM2QJcwtL7JbDJDAsR9YBtcNCKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer ID" = _t, Code = _t, #"Earliest Code Sent Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{ {"Earliest Code Sent Date", type date}})
in
    #"Changed Type"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Rename to Table 2

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.