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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

DAX: Counting the Number Between Milestones, Aggregating by Month

Hi!

I'm trying to create a monthly view of how many unique projects were "in progress" that month.
I got something working that will show how many projects were in progress on a particular day, but haven't cracked the code on how to show it accurately by month.

Currently, this is what I have:

 

TABLE = 
    VAR ExpandedTable = 
        GENERATE(
            CALENDAR(DATE(2018,1,1),TODAY()),
            
            FILTER(
                'Other Table',
                [Date]>='Other Table'[Start Date] &&
                [Date]<= 'Other Table'[END Date])
                )
            
       
    RETURN 
    SUMMARIZE(  
        ExpandedTable,
        [Date],
        "Count",COUNTROWS('Other Table')
        ) 

What do I need to change to have it return the total of unique projects worked that month?

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

Hi @Anonymous 

Assume "other table" is your main data table including "project", "start date" and "end date" fields.

I create a new table

calendar = ADDCOLUMNS(CALENDARAUTO(),"year",YEAR([Date]),"month",MONTH([Date]))

Then create two calculated columns in this table

start_month = CALCULATE(MIN('calendar'[Date]),ALLEXCEPT('calendar','calendar'[year],'calendar'[month]))

end_month = CALCULATE(MAX('calendar'[Date]),ALLEXCEPT('calendar','calendar'[year],'calendar'[month]))

Then create a measure in this table

count =
CALCULATE (
    COUNT ( 'other table'[project] ),
    FILTER (
        ALL ( 'other table' ),
        'other table'[start date] <= MAX ( 'calendar'[start_month] )
            && 'other table'[end date] >= MAX ( 'calendar'[end_month] )
    )
)

10.png

Note:

There is no relationship between "calendar" and "other table".

 

In my test, "projects were in progress" in that month means:

it should be in progress during the whole month, 

for example,

project8, start from2019/1/1, end at 2019/3/28,

in 2019/3, this project isn't considered as in progress, for it doesn't work for the whole month.

 

Best Regards

Maggie

 

Community Support Team _ Maggie Li
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

2 REPLIES 2
v-juanli-msft
Community Support
Community Support

Hi @Anonymous 

Assume "other table" is your main data table including "project", "start date" and "end date" fields.

I create a new table

calendar = ADDCOLUMNS(CALENDARAUTO(),"year",YEAR([Date]),"month",MONTH([Date]))

Then create two calculated columns in this table

start_month = CALCULATE(MIN('calendar'[Date]),ALLEXCEPT('calendar','calendar'[year],'calendar'[month]))

end_month = CALCULATE(MAX('calendar'[Date]),ALLEXCEPT('calendar','calendar'[year],'calendar'[month]))

Then create a measure in this table

count =
CALCULATE (
    COUNT ( 'other table'[project] ),
    FILTER (
        ALL ( 'other table' ),
        'other table'[start date] <= MAX ( 'calendar'[start_month] )
            && 'other table'[end date] >= MAX ( 'calendar'[end_month] )
    )
)

10.png

Note:

There is no relationship between "calendar" and "other table".

 

In my test, "projects were in progress" in that month means:

it should be in progress during the whole month, 

for example,

project8, start from2019/1/1, end at 2019/3/28,

in 2019/3, this project isn't considered as in progress, for it doesn't work for the whole month.

 

Best Regards

Maggie

 

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

Greg_Deckler
Super User
Super User


Take a look at these two Quick Measures as I think you want something like them.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Open-Tickets/m-p/409364
https://community.powerbi.com/t5/Quick-Measures-Gallery/Periodic-Billing/m-p/409365


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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