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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
SebaSpotti
Advocate II
Advocate II

Count numbers of rows in a table with dates

Hi guys,

I have a table, Table1, with a column of dates, Table1[COMP_DATE]. The dates in Table1[COMP_DATE] can be repetead and they are unsorted.

For each row, I want to calculate how many rows there are in the table with Table1[COMP_DATE] between the date in the row and an year before. Something like:

Table.RowCount(Table1[COMP_DATE]>Table1[COMP_DATE](x)-1 year and Table1[COMP_DATE]<=Table1[COMP_DATE](x)).

 

any suggestions?

1 ACCEPTED SOLUTION
Kedar_Pande
Super User
Super User

@SebaSpotti 

Create a calculated column with the following DAX expression:

CountLastYear = 
VAR CurrentDate = Table1[COMP_DATE]
RETURN
COUNTROWS(
FILTER(
Table1,
Table1[COMP_DATE] > EDATE(CurrentDate, -12) &&
Table1[COMP_DATE] <= CurrentDate
)
)

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

View solution in original post

5 REPLIES 5
Kedar_Pande
Super User
Super User

@SebaSpotti 

Create a calculated column with the following DAX expression:

CountLastYear = 
VAR CurrentDate = Table1[COMP_DATE]
RETURN
COUNTROWS(
FILTER(
Table1,
Table1[COMP_DATE] > EDATE(CurrentDate, -12) &&
Table1[COMP_DATE] <= CurrentDate
)
)

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

It works. But I have a prolem. I need to filter to select a date in a slicer, so i need to remove that filters and still had to work

Sahir_Maharaj
Super User
Super User

Hello @SebaSpotti,

 

Can you please try the following approach:

RowsInLastYear = 
VAR CurrentDate = Table1[COMP_DATE]
RETURN
    COUNTROWS(
        FILTER(
            Table1,
            Table1[COMP_DATE] > (CurrentDate - 365) && 
            Table1[COMP_DATE] <= CurrentDate
        )
    )

Hope this helps.


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

It works. But I have a prolem. I need to filter to select a date in a slicer, so i need to remove that filters and still had to work

Anonymous
Not applicable

Hi @SebaSpotti ,

 

 

Could you add more details on where you're stuck now? From the discussion above you now get a new calculated column, what is your expected output next?

vcgaomsft_0-1730444049781.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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