Forum Discussion
Snapshot fact table, Count Active measures
- 7 years ago
So the only change I would suggest to your fact table is to insert a high end date like "31-Dec-9999" instead of null when there is no Unsubscribed Date (it just makes the logic simpler.
Then if you create a date table (which is unreated to your fact) you can build measures with the following pattern
Topic Cnt = CALCULATE( COUNTROWS('Fact Topic subscription') , filter('Fact Topic subscription', MAX('Date'[Date]) >= 'Fact Topic subscription'[Subscribed date] && MIN('Date'[Date]) < 'Fact Topic subscription'[Unsubscribed date] ) )You can see in the screenshot below how this picks up the end subscription of the first row and the start of the second row. (you can choose to include the Unsubscribed date in the range by changing the filter statement). You can then add addition Year, Month, Quarter, etc attributes in your Date table and the same measures should continue to work.
- 7 years ago
SuraMan wrote:Hi d_gosbell ,
I can understand that what needs to happen is to count Fact table rows where Date[date] falls between "Subscribed date" and "Unsubscribed date", but cannot figure out how that translates to the dax expression.
Because the 2 tables do not have a relationship between them if we just did a measure with COUNTROWS('Fact topic subscription') it would return a value of 3 for every date (as there are 3 rows in the example fact table). This is because without a relationship the 'Date' table will not filter the fact table at all.
So the expression pretty much works exactly how you've described it above to filter the fact table.
Topic Cnt = CALCULATE( COUNTROWS('Fact Topic subscription') // Count the rows in the fact table , filter('Fact Topic subscription', // filtering the fact table
MAX('Date'[Date]) >= 'Fact Topic subscription'[Subscribed date] // where the date is after Subscribed Date && MIN('Date'[Date]) < 'Fact Topic subscription'[Unsubscribed date] ) // and before the Unsubscribed Date )The only "trick" is the MIN('Date'[Date]) and MAX('Date'[Date]) references. When you are at the grain of a single day these both return the same value. But if you had months on the rows the MIN would return the first day of the month and the MAX would return the last day of the month.
Is the MIN/MAX the bit that made it hard to understand?
SuraMan wrote:Hi d_gosbell ,
I ended up doing it the way I described, but I wasn't sure that is the correct way.
In general, if we want to restrict a list of data rows shown in a table (or any other visual) based on a measure, is the correct approach to put a conditional filter using that measure into the "Visual Level Filter"?
If you just want specific visuals filtered by this measure then putting a conditional filter based on that measure into the "Visual Level Filter" is fine. But if you want every visual on a page filtered by this measure then I'd move the filter up to the page level or to the report level if you want the whole report filtered that way.
In terms of query performance it does not matter which level you define the filters at as they all get merged together when the query for a given visual is generated. But it makes a big difference to how easy it is to edit the report in future if you ever need to alter the filters or duplicate a page and apply a different set of filter conditions.
If you just want specific visuals filtered by this measure then putting a conditional filter based on that measure into the "Visual Level Filter" is fine. But if you want every visual on a page filtered by this measure then I'd move the filter up to the page level or to the report level if you want the whole report filtered that way.
Hi d_gosbell ,
Can measures be included in Page level and Report level filters? The below article says they cannot. Power BI does not allow me to place a measure in Page level or Report level filter.
The Visual Level Filters section is the only one accepting a measure as a filter, whereas Page Level Filters and Report Level Filters only accept columns as a filter
https://www.sqlbi.com/articles/applying-a-measure-filter-in-power-bi/
- d_gosbell7 years agoSuper User
SuraMan wrote:Can measures be included in Page level and Report level filters? The below article says they cannot. Power BI does not allow me to place a measure in Page level or Report level filter.
No, you are right, the page/report level filters don't accept measures as filters.
I find this lack of consistency annoying, I don't think there is any logical reason for this. Filters based on measures are potentially worse from a performance perspective, but if you need that functionality this just forces you to apply the filter at the visual level.
There is a different arbitrary restriction like this on Slicers which don't accept any Visual level filters, not because of any technical reason, just because someone at Microsoft decided that people probably would not need to do this.