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
SHullen
Frequent Visitor

Measure to count by min / max date range while ignoring slicer

Hi, I have a measure that counts program completions.  It works but only for selected terms.  I need it to include the terms between the two selected terms based on the start and end dates of the selected terms.  In this example, Spring 2023 and Summer 2023 need to be included in the count.  Thanks!  UPDATE: The problem with my example is that it only has records for the terms that I want to count.  But if you look at the slicer, you'll see that it goes from Fall 2020 to Fall 2024.  If I use any version of All() it counts everything.  Without All() it only includes the selected terms in the slicer.  That is why I'm trying to use the Min(Term_Start_Date) & Max(Term_End_Date) from the slicer to count everything between those dates.  Thanks!!

 

SHullen_0-1713443446759.png

TermStudentIDProgram CodeTerm_Start_DateTerm_End_DateCompletion_Date
Fall 202211111Prog_19/5/202212/31/20228/1/2022
Fall 202211111Prog_29/5/202212/31/2022 
Fall 202222222Prog_39/5/202212/31/202210/1/2022
Fall 202222222Prog_49/5/202212/31/2022 
Fall 202233333Prog_59/5/202212/31/2022 
Spring 202311111Prog_61/1/20235/15/20233/1/2023
Spring 202311111Prog_71/1/20235/15/2023 
Spring 202322222Prog_81/1/20235/15/2023 
Spring 202322222Prog_91/1/20235/15/20232/2/2023
Spring 202333333Prog_101/1/20235/15/2023 
Summer 202311111Prog_115/20/20239/2/2023 
Summer 202311111Prog_125/20/20239/2/20236/2/2003
Summer 202322222Prog_135/20/20239/2/2023 
Summer 202322222Prog_145/20/20239/2/202310/1/2003
Summer 202333333Prog_155/20/20239/2/2023 
Fall 202311111Prog_169/8/200312/31/2023 
Fall 202311111Prog_179/8/200312/31/202311/1/2023
Fall 202322222Prog_189/8/200312/31/2023 
Fall 202322222Prog_199/8/200312/31/202312/1/2023
Fall 202333333Prog_209/8/200312/31/20232/2/2024

Here's my DAX code which either counts all records or the two fall terms if I get rid of all.

 

#_Comp_Awd_Term_All = CALCULATE(COUNTROWS( FILTER(GROUPBY( 'dw vw_Exec_Course_Performance','dw vw_Exec_Course_Performance'[StudentID],
'dw vw_Exec_Course_Performance'[ProgramCode],'dw vw_Exec_Course_Performance'[Term]  ,'dw vw_Exec_Course_Performance'[ConferralDate]),
'dw vw_Exec_Course_Performance'[ConferralDate] >= MIN('dw vw_Exec_Course_Performance'[AP_Start_Dt]) &&
'dw vw_Exec_Course_Performance'[ConferralDate] <= MAX('dw vw_Exec_Course_Performance'[AP_End_Dt]) )),

ALL())

 

1 ACCEPTED SOLUTION
SHullen
Frequent Visitor

I figured this out based on another post I came across.  Calculate the min and max variables first using the slicer values.  Then filter ALL() with the min and max variables.  Works as expected!!

#_Comp_Awd_Term_All =
var _max = MAX('dw vw_Exec_Course_Performance'[AP_End_Dt])
var _min = min('dw vw_Exec_Course_Performance'[AP_End_Dt])
RETURN
CALCULATE(
    CALCULATE (
        COUNTROWS (
            FILTER (
                GROUPBY (
                    'dw vw_Exec_Course_Performance',
                    'dw vw_Exec_Course_Performance'[StudentID],
                   'dw vw_Exec_Course_Performance'[ProgramCode],
 --                   'dw vw_Exec_Course_Performance'[Term],
                   'dw vw_Exec_Course_Performance'[ConferralDate]
                ),
                'dw vw_Exec_Course_Performance'[ConferralDate]
                    >=  ( _min )
                    && 'dw vw_Exec_Course_Performance'[ConferralDate]
                        <= _max)
            )
        )  , ALL('dw vw_Exec_Course_Performance'[Term],'dw vw_Exec_Course_Performance'[Conf_Friendly]   )
)

View solution in original post

3 REPLIES 3
SHullen
Frequent Visitor

I figured this out based on another post I came across.  Calculate the min and max variables first using the slicer values.  Then filter ALL() with the min and max variables.  Works as expected!!

#_Comp_Awd_Term_All =
var _max = MAX('dw vw_Exec_Course_Performance'[AP_End_Dt])
var _min = min('dw vw_Exec_Course_Performance'[AP_End_Dt])
RETURN
CALCULATE(
    CALCULATE (
        COUNTROWS (
            FILTER (
                GROUPBY (
                    'dw vw_Exec_Course_Performance',
                    'dw vw_Exec_Course_Performance'[StudentID],
                   'dw vw_Exec_Course_Performance'[ProgramCode],
 --                   'dw vw_Exec_Course_Performance'[Term],
                   'dw vw_Exec_Course_Performance'[ConferralDate]
                ),
                'dw vw_Exec_Course_Performance'[ConferralDate]
                    >=  ( _min )
                    && 'dw vw_Exec_Course_Performance'[ConferralDate]
                        <= _max)
            )
        )  , ALL('dw vw_Exec_Course_Performance'[Term],'dw vw_Exec_Course_Performance'[Conf_Friendly]   )
)
SHullen
Frequent Visitor

Hi  Yilong Zhou,

 Thanks for taking the time to look into this issue.  Your solution doesn't include spring and summer in the final result.  I want the DAX to use the min start date and max end date as a range and ignore the seleted terms in the slicer.  So there would be 7 counted in the final results instead of 3.

v-yilong-msft
Community Support
Community Support

Hi @SHullen ,

I create a table as you mentioned.

vyilongmsft_0-1713508146033.png

Then I change the DAX codes and here is the updated DAX codes.

#_Comp_Awd_Term_All =
VAR _A =
    CALCULATE (
        COUNTROWS (
            FILTER (
                GROUPBY (
                    'dw vw_Exec_Course_Performance',
                    'dw vw_Exec_Course_Performance'[StudentID],
                    'dw vw_Exec_Course_Performance'[Program Code],
                    'dw vw_Exec_Course_Performance'[Term],
                    'dw vw_Exec_Course_Performance'[Completion_Date]
                ),
                'dw vw_Exec_Course_Performance'[Completion_Date]
                    >= MIN ( 'dw vw_Exec_Course_Performance'[Term_Start_Date] )
                    && 'dw vw_Exec_Course_Performance'[Completion_Date]
                        <= MAX ( 'dw vw_Exec_Course_Performance'[Term_End_Date] )
            )
        ),
        ALLSELECTED ( 'dw vw_Exec_Course_Performance'[Term] )
    )
RETURN
    IF ( _A, "Count", "Out" )
Measure = 
IF (
    'dw vw_Exec_Course_Performance'[#_Comp_Awd_Term_All] = "Out"
        && SELECTEDVALUE ( 'dw vw_Exec_Course_Performance'[Completion_Date] ) = BLANK (),
    BLANK (),
    'dw vw_Exec_Course_Performance'[#_Comp_Awd_Term_All]
)

Fianlly when I select the slicer, it will give me the things you want.

vyilongmsft_1-1713508883049.png

 

 

Best Regards

Yilong Zhou

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

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.