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
Baarathi88
Helper III
Helper III

DAX for filtering start month and end month data based on slicer selection

Is there a way to create a measure, displaying count users in first month (minimum) of selection of date range with last month (maximum) of selection of date from the date slicer.

 

Please find the data below and example scenario

 

For Example : When I select date range from Jan-01-2019 to Feb-02-2019 in date slicer. I need to create 2 measures

 

1. FirstMonthUsers = DistinctCount(UserName) for Month of Jan-2019

2. LastMonthUsers = DistinctCount(UserName) for Month of Feb -2019 (even though selection is only till Feb-2, i need entire feb month)

 

Users:

IDUserNameStartDateEndDateCountryID
1David1/1/2019 1
2Prince1/1/2019 1
3Kristen1/1/2019 1
4Laura1/1/2019 2
5Clara1/1/2019 2
6Lucus1/1/2019 3
7Johnny1/1/2019 4
8Sam1/1/2019 4
9Peter1/1/2019 4
10Benjamin1/1/2019 5
11Lucifer1/1/2019 5
12Janice1/1/2019 5
13Julia1/1/2019 5
14Lisa1/1/2019 5
15Dhoni1/1/20192/1/20191
16Rahul1/1/20192/1/20192
17Dravid1/1/20192/1/20193
18Sachin1/1/20192/1/20194
19Ganguly2/28/2019 5
20Ponting2/28/2019 1
21Clarke2/28/2019 1
22Khan2/28/2019 1
23Akram2/28/2019 1
24Jason2/28/2019 1
25Pollock2/28/2019 2
26James2/28/2019 2
27Anderson2/28/2019 3
1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

5 REPLIES 5
Icey
Community Support
Community Support

Hi @Baarathi88 ,

 

I have different ideas. I think you're not just trying to filter start and end month data based on slicer selection, but also about start and end date.

For example, you select 1/4/2019 as the minimum value and 3/23/2019 as the maximum value, the min count users should be 18 and the max count users should be 23.

user1.png    user2.png

 

If my understanding is right, heres my idea:

 

First you create a calendar table:

Calendar = CALENDAR(DATE(2018,1,1),DATE(2020,12,31))

calendar.png

 

Then you can write your measures as below.

 

In 'Users'[StartDate] <= EOMONTH ( MAX ( 'Calendar'[Date] ), 0 ) , it means that the start date is less than or equal to the last day of the month, the last day that is included.

 

In  'Users'[EndDate] > EOMONTH ( MAX ( 'Calendar'[Date] ), -1 ) + 1 , it means that the end date is greater than the first day of the month, the first day of the month is not included.

 

CountMax =
CALCULATE (
    DISTINCTCOUNT ( Users[UserName] ),
    FILTER (
        ALL ( 'Users' ),
        'Users'[StartDate] <= EOMONTH ( MAX ( 'Calendar'[Date] ), 0 )
            && (
                'Users'[EndDate]
                    > EOMONTH ( MAX ( 'Calendar'[Date] ), -1 ) + 1
                    || 'Users'[EndDate] = BLANK ()
            )
    )
)
CountMin =
CALCULATE (
    DISTINCTCOUNT ( 'Users'[UserName] ),
    FILTER (
        ALL ( 'Users' ),
        'Users'[StartDate] <= EOMONTH ( MIN ( 'Calendar'[Date] ), 0 )
            && (
                'Users'[EndDate]
                    > EOMONTH ( MIN ( 'Calendar'[Date] ), -1 ) + 1
                    || 'Users'[EndDate] = BLANK ()
            )
    )
)

 

At last the result is as below:

re2.pngre1.png

You can check more details from here.

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@Baarathi88 , Not very clear. See if time intelligence can help

MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
last month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth('Date'[Date]))
last MTD (complete) Sales =  CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(ENDOFMONTH(dateadd('Date'[Date],-1,MONTH))))
last year MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-12,MONTH)))
last year MTD (complete) Sales =  CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(ENDOFMONTH(dateadd('Date'[Date],-12,MONTH))))
Month behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Month))
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))
Next month value =  CALCULATE(sum('table'[total hours value]),nextmonth('Date'[Date]))


diff = [MTD Sales]-[last MTD Sales]

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-Y...


Appreciate your Kudos.

Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@Ashish_Mathur  Thank You. That helps 😊

You are welcome.  If my reply helped, please mark it as Answer.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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.

Top Solution Authors