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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
dlukin778
New Member

Power Query (M Language) Count Duplicates within two dates

I have a table with a unique key that I would like to count. However, I only want to count the item IF it exists between two date columns. 

Logic: count if THIS unique key occurrs again between date opened and dateplus 7. 

Resulting code in M Language please. 

UNIQUE KEY              DATE OPENED               DATEPLUS7                         CUSTOM COLUM RESULT WANTED
B                                12/05/2022                   12/12/2022                          2  

B                                12/07/2022                   12/19/2022                          2

B                                12/13/2022                   12/20/2022                          1

B                                6/13/2022                      6/20/2022                           1

B                                4/13/2022                      4/20/2022                           1

1 ACCEPTED SOLUTION
v-yalanwu-msft
Community Support
Community Support

Hi, @dlukin778 ;

Yes, that one really didn't work, you can change to:

let 
b=[#"DATEPLUS7 "]
in
Table.RowCount(Table.SelectRows(#"Changed Type",each 
([DATE OPENED]<=b and [#"DATEPLUS7 "]>=b) ))

 
Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-yalanwu-msft
Community Support
Community Support

Hi, @dlukin778 ;

Yes, that one really didn't work, you can change to:

let 
b=[#"DATEPLUS7 "]
in
Table.RowCount(Table.SelectRows(#"Changed Type",each 
([DATE OPENED]<=b and [#"DATEPLUS7 "]>=b) ))

 
Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

v-yalanwu-msft
Community Support
Community Support

Hi, @dlukin778 ;

Try it custom column.

let 
a =[DATE OPENED],
b=[#"DATEPLUS7 "]
in
Table.RowCount(Table.SelectRows(#"Changed Type",each 
([DATE OPENED]<=b and [#"DATEPLUS7 "]>=b)

 ))

The final show:

vyalanwumsft_0-1671516239625.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclLSUTI00jcw1TcyMDKCcIAIzInVQcibI8tbYsgbGiPJGxmgypvBpBXAHHRpEyTdJgjZWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"UNIQUE KEY  " = _t, #"DATE OPENED" = _t, #"DATEPLUS7 " = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"UNIQUE KEY  ", type text}, {"DATE OPENED", type date}, {"DATEPLUS7 ", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each let 
a =[DATE OPENED],
b=[#"DATEPLUS7 "]
in
Table.RowCount(Table.SelectRows(#"Changed Type",each 
([DATE OPENED]<=b and [#"DATEPLUS7 "]>=b)

 )))
in
    #"Added Custom"


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks for the answer. 
I'm currently 
I noticed that you have two definitions. A and B. But A is never used for some reason. Am I missing something?

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.