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

DISTINCTCOUNT for Year Ranges with Two Columns

Greetings, 

 

Brand new to PBI here. I am trying to create a measure that counts "ID" column based off of "YEAR" column. HOWEVER, another column ("TERM") has different codes before 2016 than after. My company started changing codes that year based off of reporting changes.) Codes for pre-2016 are 5, 10, 20, and 30. Codes for 2016 and after are 20, 30, 35, and 40. (20 and 30 mean the same time period, so that does not need to change, but the difference in the other numbers indicates different time ranges.)

 

One small hitch: 2016 has both sets of codes associated with it -- but I only need to use 20, 30, 35 and 40 for the count within that year. 

 

In short, I'm trying to find a way to have a consistent, distinct count of "ID" by "YEAR", and then filtering by the proper "TERM" code depending on the year. Pre-2016 will use codes 5, 10, 20, and 30 for the distinct count; 2016 and after will use codes 20, 30, 35, and 40. 

 

------- Edited Here --------

 

Thank you kindly @Anonymous  and @az38 for the quick responses -- and below is a sample dataset and then a desired outcome. I color coded who would drop and who would stay in the count, and provided details below. Definitely let me know if I can clarify further. 

 

TABLE1

IDYEARTERM
000120155
0002201510
0003201520
0004201530
0005*20165
0006*201610
0007201620
0008201630
0009201635
0010201640
0011201720
0012201730
0013201735
0014201740
0014**201730
0014**201740
0001***201740

 

 

The distinct count for each year would be as follows: 

2015 = 4 IDs

2016 = 4 IDs. (The two IDs indicated with a single asterisk (*) are dropped due to the phased out codes occurring in that year ("5" and "10"). These codes do not exist after 2016 and only need to be excluded from this 2016. 

2017 = 5 IDs. (The double asterisk (**) is dropped due to ID duplication within the same year. There are numerous duplicates in the actual dataset, so a distinct count would be needed here.) ***ID 0001 is counted since this person is present in a different year (2015), but it is not present within the same yearI failed to mention this in the original post. 

 

Thank you again for any help! 

 

------- END EDITS --------

 

All columns are from the same table name (TABLE1). 

 
Any help is so greatly appreciated! 
2 ACCEPTED SOLUTIONS
az38
Community Champion
Community Champion

@Mitig 

maybe you should try a measure like

Measure = CALCULATE(DISTINCTCOUNT(Table1[ID]), 
FILTER(ALL(Table1), Table1[YEAR]=SELECTEDVALUE(Table1[YEAR]) && (Table1[TERM]>10 || Table1[YEAR]<2016)))

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

View solution in original post

Anonymous
Not applicable

Try this

Measure=
Var a=Calculate(distinctcount(table[id]),filter(table,table[year]<2016))
Var b=Calculate(distinctcount(table[id]),filter(table,table[year]>=2016 && table[code]<> 5 &&table[code]<> 10))

Return
If(max(year)<2015,a,b)

Thanks
Pravin

View solution in original post

5 REPLIES 5
az38
Community Champion
Community Champion

Hi @Mitig 

its unclear without data example, but maybe you should use ALLEXCEPT() function like

Measure = 
CALCULATE(COUNTROWS('Table'), ALLEXCEPT('Table', 'Table'[YEAR], 'Table'[TERM]) )

 


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Mitig
Frequent Visitor

Thank you - I did look into this one after your reply, but due to there being duplicated rows, counting the raw number of rows would yield an excessive number of IDs. (I did not mention there were duplicates in the original posting, so that is definiely my bad for lack of clarity.) As such, it has to be a distinct count of IDs. I also edited the original post with a sample dataset and how the count would come out. Cheers! 

az38
Community Champion
Community Champion

@Mitig 

maybe you should try a measure like

Measure = CALCULATE(DISTINCTCOUNT(Table1[ID]), 
FILTER(ALL(Table1), Table1[YEAR]=SELECTEDVALUE(Table1[YEAR]) && (Table1[TERM]>10 || Table1[YEAR]<2016)))

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Anonymous
Not applicable

Try this

Measure=
Var a=Calculate(distinctcount(table[id]),filter(table,table[year]<2016))
Var b=Calculate(distinctcount(table[id]),filter(table,table[year]>=2016 && table[code]<> 5 &&table[code]<> 10))

Return
If(max(year)<2015,a,b)

Thanks
Pravin
Anonymous
Not applicable

Please share sample data and expected output.

 

Thanks,

PRavin

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