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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
bsas
Post Patron
Post Patron

Calculate distinctcount for ID within quarter based on last month range in quarter

Hi All!

I'm struggling with measure for distinct values of IDs within quarter based on last active month range.

I need to show only 1 value for quater per ID in chart based on range of last active month, tried something like below but I figured out that there some IDs which have only 1st or 2nd month of quarter active and measure below didn't calculate them. 

 

Calculate( DISTINCTCOUNT(ID), FILTER(DATA, DATA(DATE) = ENDOFQUARTER(DATE) && RANGEID <> BLANK))

 

 

What I need - distinct value per ID per quarter based on last active month range even if range shanged within quarter, please help with changes in measure.
sample of data below:

IDPeriodRange
1111/1/20222
1112/1/20221
2221/1/20223
2222/1/20222
2223/1/20223
2224/1/20222

 

For ID 111 should be feb as last month for q1 with range 1, for 222 should be mar as last month for q1 with range 3 and apr as last month for q2 with range 2. 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

HI @bsas,

Which logic that you used to pick records from same id group? The maximum range or date period?

If you mean the first one, you can use the id to find out the max range first, then use id and range as condition to filter records to get the count.

 

formula =
VAR currDate =
    MAX ( Table[Period] )
VAR _range =
    CALCULATE (
        MAX ( Table[Range] ),
        FILTER (
            ALLSELECTED ( Table ),
            YEAR ( [Period] ) = YEAR ( currDate )
                && QUARTER ( [Period] ) = QUARTER ( currDate )
        ),
        VALUES ( Table[ID] )
    )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( Table[ID] ),
        FILTER ( ALLSELECTED ( Table ), [Range] = _range ),
        VALUES ( Table[ID] )
    )

 

If you mean the second one, you only need to change above formula to get the period, then use it as condition in formula to calculate.

Regards,

Xiaoxin Sheng

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

Show the expected result very clearly in a Table format.


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

@bsas , Have date table with qtr and join with your table and have qtr in visual or filter from date table

 

calculate(lastnonblankvalue(Table[period]), distinctCOUNT(Table[ID]))

 


Calendar = Addcolumns(calendar(date(2020,01,01), date(2021,12,31) ), "Month no" , month([date])
, "Year", year([date])
, "Month Year", format([date],"mmm-yyyy")
, "Month year sort", year([date])*100 + month([date])
, "Qtr Year", format([date],"yyyy-\QQ")
, "Qtr", quarter([date])
, "Month",FORMAT([Date],"mmmm")
, "Month sort", month([DAte])
, "Is Today" ,if([Date]=TODAY(),"Today",[Date]&"")
,"Day of Year" , datediff(date(year([DAte]),1,1), [Date], day)+1
, "Month Type", Switch( True(),
eomonth([Date],0) = eomonth(Today(),-1),"Last Month" ,
eomonth([Date],0)= eomonth(Today(),0),"This Month" ,
Format([Date],"MMM-YYYY") )
,"Year Type" , Switch( True(),
year([Date])= year(Today()),"This Year" ,
year([Date])= year(Today())-1,"Last Year" ,
Format([Date],"YYYY")
)
)

 

https://www.linkedin.com/pulse/five-recent-power-bi-functions-you-should-use-more-often-amit-chandak

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi @amitchandak 
Thanks for your help. I have date table with diff attributes, tried your measure but it is not working as expected, in case ID have 2 diff ranges within quarter it shows this ID twice and I need only last value for quarter e.g. for ID 222 in 1st qarter it shows count 2 with two different rages 2 and 3, should only show count 1 and range 3

created two measures:

count1 =  CALCULATE( DISTINCTCOUNT(ID), FILTER( Period, Period[Date] = LASTDATE(Data[Date])))
count2 =  CALCULATE( DISTINCTCOUNT(ID), FILTER( Data, Data[Date] = ENDOFQUARTER(Period[Date])))

bsas_0-1671474418505.png

but both give not what I want - 2 2 1 1, maybe this can help?



Anonymous
Not applicable

HI @bsas,

Which logic that you used to pick records from same id group? The maximum range or date period?

If you mean the first one, you can use the id to find out the max range first, then use id and range as condition to filter records to get the count.

 

formula =
VAR currDate =
    MAX ( Table[Period] )
VAR _range =
    CALCULATE (
        MAX ( Table[Range] ),
        FILTER (
            ALLSELECTED ( Table ),
            YEAR ( [Period] ) = YEAR ( currDate )
                && QUARTER ( [Period] ) = QUARTER ( currDate )
        ),
        VALUES ( Table[ID] )
    )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( Table[ID] ),
        FILTER ( ALLSELECTED ( Table ), [Range] = _range ),
        VALUES ( Table[ID] )
    )

 

If you mean the second one, you only need to change above formula to get the period, then use it as condition in formula to calculate.

Regards,

Xiaoxin Sheng

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.