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

Count specific values based on latest date

Hello,

 

I would like to count data entries with a specific value based on latest selected less than or equal date and have created a date table with a separate slider in my report.

 

This is my data set:

 

Count.PNG

 

In my report I would like a card visualisation to count the number of different ID's with value1 and value 2 being equal to the number "2", that are closest less than or equal to the selected date.

 

For instance if the selected day is 2. january the count would be 3. If the selected day is 6. January the count would be 2 and if the selected day is 10. january the count would be 1.

 

Can someone help me how to acomplish this?

 

Thanks in advance

1 ACCEPTED SOLUTION

Hi  @anin ,

 

Modify the measure as below:

 

Measure = 
var _maxdate=CALCULATE(MAX('Table'[Date]),FILTER(ALLSELECTED('Table'),'Table'[Date]<=SELECTEDVALUE('calendar table'[Date])&&'Table'[ID]=MAX('Table'[ID])))
var _tab=SUMMARIZE('Table','Table'[ID],'Table'[Date],"value",IF(MAX('Table'[Date])=_maxdate&&MAX('Table'[Value1])=2&&MAX('Table'[Value2])=2,1,BLANK()))
Return
SUMX(_tab,[value])
sum = SUMX(VALUES('Table'[ID]),[Measure])

 

And you will see:

v-kelly-msft_0-1615353259457.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

Did I answer your question? Mark my post as a solution!

View solution in original post

6 REPLIES 6
v-kelly-msft
Community Support
Community Support

Hi  @anin ,

 

Sorry I'm a little confused about your expected output,you said if the selected date is Jan2,then the count should be 1,if the selected date is Jan1,the count should be 2,why?For Jan2,values for ID 1,2,3 both have number 2,so why the count is 1,not 3?

 

Best Regards,
Kelly

Did I answer your question? Mark my post as a solution!

Hi @v-kelly-msft ,

 

Sorry, there was an error in my expected output! I understand the confusion.

This is my expected outcome:

 

If the selected day is 2. january the count would be 3. If the selected day is 6. January the count would be 2 and if the selected day is 10. january the count would be 1. If the selected day is 1. january it should show 0.

Hi @anin ,

 

First create a calendar table;

Then create a measure as below:

Measure = 
var _maxdate=CALCULATE(MAX('Table'[Date]),FILTER(ALL('Table'),'Table'[Date]<=SELECTEDVALUE('calendar table'[Date])&&'Table'[ID]=MAX('Table'[ID])))
Return
CALCULATE(DISTINCTCOUNT('Table'[ID]),FILTER(ALL('Table'),'Table'[Value1]=2&&'Table'[Value2]=2&&'Table'[Date]=_maxdate))+0

And you will see:

v-kelly-msft_0-1615254070407.pngv-kelly-msft_1-1615254081749.pngv-kelly-msft_2-1615254088756.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

Did I answer your question? Mark my post as a solution!

Hello @v-kelly-msft ,

Thank you for your response and your help. It is very much appreciated!
I am getting closer, but not yet getting exactly the results I want.

The filter should not be set to exactly the defined _maxdate.
I want the count of all ID's where the entry is the closest equal/less than the defined _maxdate and Value1&2 = 2

 

Perhaps this dataset and desired outcome explains it better:

 

Klip1.PNG

 

If the selected day is 5th of february the highlighted entries are evaluated and the count is 2

Klip2.PNG

 

If the selected day is 8th of february the highlighted entries are evaluated and the count is 1

Klip3.PNG

 

Also I would like any other selected filters in the report page to remain selected for the card.

 

Here is a link to my dataset:

https://file.io/nYAxr2IMLCcU 

 

Once again thank you for your help!

 

Kind regards

Hi  @anin ,

 

Modify the measure as below:

 

Measure = 
var _maxdate=CALCULATE(MAX('Table'[Date]),FILTER(ALLSELECTED('Table'),'Table'[Date]<=SELECTEDVALUE('calendar table'[Date])&&'Table'[ID]=MAX('Table'[ID])))
var _tab=SUMMARIZE('Table','Table'[ID],'Table'[Date],"value",IF(MAX('Table'[Date])=_maxdate&&MAX('Table'[Value1])=2&&MAX('Table'[Value2])=2,1,BLANK()))
Return
SUMX(_tab,[value])
sum = SUMX(VALUES('Table'[ID]),[Measure])

 

And you will see:

v-kelly-msft_0-1615353259457.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

Did I answer your question? Mark my post as a solution!

amitchandak
Super User
Super User

@anin , Try a measure like. I have not tested this

 

measure =
Var _1 = calculateTable(all(Table), Table[Date] <=selectedvalue(Table[Date]))
return
counrows(distinct(union(all(_1[value1]), all(_1[value2]))))

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.