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
Syndicate_Admin
Administrator
Administrator

Count repeated records by dates with respect to another record

Good

I am trying to count duplicate records Ref, if such registration occurs according to Event M, on the same day or after Event I, or how more up to 7 days after Event I.

RefEventDate
OffI01/01/2022
OffM02/01/2022
OffM03/01/2022
OffM04/01/2022
CDI03/01/2022
CDM03/01/2022
IfI04/01/2022
IfM03/01/2022
GHI05/01/2022
GHM13/01/2022
JKR14/01/2022
LLM15/01/2022

The expected result, for table above and the premises given:

Off3M occurs after I, 3 times
CD1M occurs on the same day as I
If0M occurs before I
GH0M occurs +7days after I
JK0is not M and is not duplicate
LL0is not duplicate

Total 4 repeated Ref records.

I am looking for a measure to be able to count the total of repeated records Ref according to given premises.

I'm not sure whether to create a column or how to do it.

Thank you!

Best regards

Jojudima

5 REPLIES 5
Syndicate_Admin
Administrator
Administrator

Good again,

When moving the solution to the model, I find that if there is data from different months, it does not work correctly.

RefEventDate
OffI01/01/2022
OffM02/01/2022
OffM03/01/2022
OffM04/01/2022
CDI03/01/2022
CDM03/01/2022
IfI04/01/2022
IfM03/01/2022
GHI05/01/2022
GHM13/01/2022
JKR14/01/2022
LLM15/01/2022
OffM01/02/2022
OffM02/02/2022
CDI03/02/2022
CDM04/02/2022
CDM10/02/2022

For this case with data in January and February the total that yields is 5, but it really should be 7.

I understand that for February it is taking the first date of I, which occurs in January, so M happens +7days later.

jojudima_0-1661321634863.png

When grouped by months, the desired result would be 4 in January and 3 in February.

jojudima_1-1661321658093.png

Any help will be welcome.

Thanks in advance.

Jojudima

Syndicate_Admin
Administrator
Administrator

Thank you very much, it works perfectly.

Anonymous
Not applicable

Hi @Syndicate_Admin ,

 

I think you can try my measure to achieve your goal.

Count M after I = 
VAR _DATE_I = CALCULATE(MAX('Table'[Date]),FILTER(ALLEXCEPT('Table','Table'[Ref]),'Table'[Event]="I"))
RETURN
CALCULATE(count('Table'[Ref]),FILTER(ALLEXCEPT('Table','Table'[Ref]),_DATE_I<>BLANK()&&'Table'[Date]>=_DATE_I&&'Table'[Event]="M"&&DATEDIFF(_DATE_I,MAX('Table'[Date]),DAY)<7))+0
Measure = 
VAR _DATE_I = CALCULATE(MAX('Table'[Date]),FILTER(ALLEXCEPT('Table','Table'[Ref]),'Table'[Event]="I"))
VAR _FIRST_M = CALCULATE(MIN('Table'[Date]),FILTER(ALLEXCEPT('Table','Table'[Ref]),'Table'[Event] = "M"))
VAR _DATEDIFF = DATEDIFF(_DATE_I,_FIRST_M,DAY)
RETURN
IF( _DATE_I = BLANK(),IF(_FIRST_M = BLANK(),"is not M and is not duplicate","is not duplicate"),IF(_DATEDIFF<0,"M occurs before I",IF(_DATEDIFF>7,"M occurs +7days after I",IF(_DATEDIFF = 0,"M occurs on the same day as I","M occurs after I, "&""&[Count M after I]&""&" times"))))

Result is as below.

RicoZhou_0-1661151553178.png

 

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.

Hello

Thank you very much for the answer, with this method I do not get the total, it is true that it counts the duplicate records.

jojudima_0-1661166505903.png

Best regards

Jojudima

lbendlin
Super User
Super User

You could do this as a column or a measure.  I think a measure is more appropriate because a column would duplicate data.

 

 

Measure Repeated = 
var a = values(Events[Ref])
var b = ADDCOLUMNS(a, "i",var r=[Ref] return CALCULATE(max(Events[Date]),all(Events),Events[Ref]=r,Events[Event]="I"))
var c = ADDCOLUMNS(b,"m",var r = [Ref] var i = [i] return CALCULATE(countrows(Events),all(Events),Events[Ref]=r,Events[Date]>=i,Events[Date]<=i+7,Events[Event]="M"))
return sumx(c,[m])

 

see attached

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.

Top Solution Authors
Top Kudoed Authors