Forum Discussion
Count Products over time dealing with two date fields
- 2 years ago
tried it .
did work from my side . ( change my table to query2 , since i already have a table called exp_table)
ok i think i know the problem you are encountering .
change the code to this :
count of active products per day new = var calendar_per_product = FILTER( GENERATE( all(Query2[product]), var pr = Query2[product] var createddate = SELECTCOLUMNS(FILTER(Query2,Query2[product] = pr),"createddate",Query2[createddate]) var enddate = SELECTCOLUMNS(FILTER(Query2,Query2[product] = pr),"enddate",Query2[enddate]) return CALENDAR( if( ISBLANK(createddate) , DATEVALUE("2024-01-01"),createddate) , if( ISBLANK(enddate) , DATEVALUE("2024-12-31"),enddate) ) ), NOT ISBLANK(Query2[product]) ) var gds = GROUPBY( calendar_per_product, [Date], "c", countx(CURRENTGROUP() , DISTINCTCOUNT(Query2[product])) ) var res= MAXX( FILTER(gds,[Date] in VALUES(dimdate[Date])), [c]) return res
JHello tonyclifton ,
if you could provide some sample data, that would be helpful.
How to provide sample data in the Power BI Forum
https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216
How to Get Your Question Answered Quickly
https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523
Hello Daniel29195
The table below pretty much sums up my problem because I don't see how to dynamically calculate the valid date range and then show the differences over the months.
Example data:
| Product | CreatedDate | DiscontinuedDate |
| A | 01.01.2023 | |
| B | 01.01.2024 | 31.01.2024 |
| C | 03.01.2024 | 30.06.2024 |
| D | 04.01.2024 | 31.12.2024 |
The problem I see is that I would need to calculate a valid date range for both dates so that I can show the changes over time.
With the link provided by Ashish_Mathur I might create other problems by duplicating rows in the source. I will have a closer look though - it might work by using it as a lookup table or so.
Isn't there a way with DAX to dynamically calculate the valid date range?
Thanks alot.
- Daniel291952 years ago
Community Champion
base on this sample data :
this is the dax code in case you want to expand your data and get the number of active products per day : ( NB if enddate is blank i took the date to be 2024-12-31 , but it can be changed to take any dynamic value )
tttr = var calendar_per_product = FILTER( GENERATE( VALUES(exp_table[product]), var pr = exp_table[product] var createddate = SELECTCOLUMNS(FILTER(exp_table,exp_table[product] = pr),exp_table[createddate]) var enddate = SELECTCOLUMNS(FILTER(exp_table,exp_table[product] = pr),exp_table[enddate]) return CALENDAR( if( ISBLANK(createddate) , DATEVALUE("2024-01-01"),createddate) , if( ISBLANK(enddate) , DATEVALUE("2024-12-31"),enddate) ) ), NOT ISBLANK(exp_table[product]) ) var gds = GROUPBY( calendar_per_product, [Date], "c", countx(CURRENTGROUP() , DISTINCTCOUNT(exp_table[product])) ) return gdsoutput table :
let me know if this can help.
- Daniel291952 years ago
Community Champion
if you dont want to create a table :
this is the measure you can use :
sample data :
output : as you can see, base on the sample data, you have in 2024-01-03 , 3 products active, which is displayed in the visual.
measure :
count of active products per day = var calendar_per_product = FILTER( GENERATE( all(exp_table[product]), var pr = exp_table[product] var createddate = SELECTCOLUMNS(FILTER(exp_table,exp_table[product] = pr),exp_table[createddate]) var enddate = SELECTCOLUMNS(FILTER(exp_table,exp_table[product] = pr),exp_table[enddate]) return CALENDAR( if( ISBLANK(createddate) , DATEVALUE("2024-01-01"),createddate) , if( ISBLANK(enddate) , DATEVALUE("2024-12-31"),enddate) ) ), NOT ISBLANK(exp_table[product]) ) var gds = GROUPBY( calendar_per_product, [Date], "c", countx(CURRENTGROUP() , DISTINCTCOUNT(exp_table[product])) ) var res= MAXX( FILTER(gds,[Date] in VALUES(dimdate[Date])), [c]) return resNB: dimdate should not be linked to the table dim_products.
and the visual is reading date from dimdate.
NB: the measure may need some minor tweakings that i didnt do, just to know if this would be helpful to you .
- tonyclifton2 years ago
Helper III
Daniel29195thanks alot for your effort. I just tried to replicate your example with the same sample data but I get an error:
What am I missing?
Thank you.