Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers!
Enter the sweepstakes now!Prepping for a Fabric certification exam? Join us for a live prep session with exam experts to learn how to pass the exam. Register now.
I have following table:
id date
1a 01.01.19
1a 02.01.19
2a 03.01.19
3a 04.01.19
4a 02.02.19
4a 03.02.19
2a 04.02.19
3a 05.02.19
I'm trying to calculate amount of distinct ID's in selected period which has appeared more than 1 time in the period earlier to the latest date in selected period
So selecting Jan'2019 should return - 1 ("1a" counted, "2a" and "3a" skipped, since only 1 time appeared)
Selecting Feb'2019 returns 3 ("2a", "3a" and "4a" counted since appeared >1 time in Jan/Feb, "1a" is skipped since not appeared in selected period)
Solved! Go to Solution.
Hey @Anonymous
first I created a dedicated Calendar Table (this is always a best practice), I used this DAX statement (a very simplified one) to create this table:
Calendar =
ADDCOLUMNS(
CALENDAR("2019-01-01" , "2019-02-28")
, "Year-Month" , FORMAT(''[Date] , "YYYY-MM")
)
Then I created a relationship between both tables like this:
Finally, I created this measure:
Measure =
SUMX(
VALUES('Table'[id])
,
var _maxdate = MAX('Calendar'[Date])
var _count =
CALCULATE(COUNT('Table'[id])
, FILTER(
ALL('Calendar'[Date]) ,
'Calendar'[Date] <= _maxdate
-- uncomment if you have more than one year
-- && YEAR('Calendar'[Date]) = YEAR(_maxdate)
)
)
return
IF(_count > 1 , 1 , BLANK())
)
This allows to create this table visual (be aware that it's also a good idea to use the columns from the Calendar table):
Hopefully, this is what you are looking for.
Regards,
Tom
Hi @Anonymous
try the next technique
create 2 columns:
1.
DateKey = FORMAT([date];"YYYY-MM")
2.
CountBeforePeriod =
CALCULATE(COUNTROWS('Table');ALL('Table');'Table'[id]=EARLIER([id]);'Table'[date]<=EOMONTH(EARLIER([date]);0))
then create a final measure
Measure = calculate(DISTINCTCOUNT('Table'[id]);ALLEXCEPT('Table';'Table'[DateKey]);'Table'[CountBeforePeriod]>1)
do not hesitate to give a kudo to useful posts and mark solutions as solution
Hey @Anonymous
first I created a dedicated Calendar Table (this is always a best practice), I used this DAX statement (a very simplified one) to create this table:
Calendar =
ADDCOLUMNS(
CALENDAR("2019-01-01" , "2019-02-28")
, "Year-Month" , FORMAT(''[Date] , "YYYY-MM")
)
Then I created a relationship between both tables like this:
Finally, I created this measure:
Measure =
SUMX(
VALUES('Table'[id])
,
var _maxdate = MAX('Calendar'[Date])
var _count =
CALCULATE(COUNT('Table'[id])
, FILTER(
ALL('Calendar'[Date]) ,
'Calendar'[Date] <= _maxdate
-- uncomment if you have more than one year
-- && YEAR('Calendar'[Date]) = YEAR(_maxdate)
)
)
return
IF(_count > 1 , 1 , BLANK())
)
This allows to create this table visual (be aware that it's also a good idea to use the columns from the Calendar table):
Hopefully, this is what you are looking for.
Regards,
Tom
@TomMartens thanks, works perfectly.
Maybe you are also aware of how to handle the following issue (just created new post 🙂) https://community.powerbi.com/t5/Desktop/in-DAX-count-distinct-values-from-previous-dates-missing-in...
User | Count |
---|---|
77 | |
70 | |
68 | |
53 | |
48 |
User | Count |
---|---|
45 | |
38 | |
35 | |
31 | |
28 |