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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
v-jayw-msft
Community Support
Community Support

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

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

View solution in original post

2 REPLIES 2
v-jayw-msft
Community Support
Community Support

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

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors