The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
I have a dataset that populates a new row every year for each unique ID. I want to count the number of rows for the preceding 12 months from the current month being viewed in a matrix table. So the Column field is populated with "Date" and these dates are the 1st of every month:
To do this I created a measure with a variable identifying what the oldest month would be:
Solved! Go to Solution.
Thanks Xiaoxin for the response, this looks good.
But I have a measure below that works and did the job 👍
Measure =
VAR EndDate =
EDATE ( MAX ( 'Data'[DATE_MONTH] ), -1 ) + 1 -- adjusts End Date to previous month (so pulls excluding current month)
VAR StartDate =
EDATE ( EndDate, -12 ) + 1 -- sets Start Date 12 months prior to End Date above
VAR TotalActive =
CALCULATE (
COUNTROWS (
FILTER ( 'Data', 'Data'[Status] = "Active" )
),
-- counts rows filtered for Active only in table rather than all rows
DATESBETWEEN (
'Data'[DATE_MONTH],
-- indicates column for dates between
StartDate,
-- Start Date defined above --
EndDate -- End Date defined above --
)
)
VAR TotalInactive =
CALCULATE (
COUNTROWS ( FILTER ( 'Lapse Data', 'Lapse Data'[Status] = "Inactive" ) ),
-- counts rows filtered for all Inactive only in table rather than all rows
DATESBETWEEN (
'Data'[DATE_MONTH],
-- indicates column for dates between
StartDate,
-- Start Date defined above --
EndDate -- End Date defined above --
)
)
RETURN
CALCULATE ( DIVIDE ( TotalActive, TotalInactive, 0 ) )
HI @Muzaffar_Ali,
You can try to use the following measure formula if this suitable for your requirement:
formula =
VAR currDate =
MAX ( 'Data'[DATE_MONTH] )
VAR StartofCalc =
CALCULATE (
MAX ( 'Data'[DATE_MONTH] ),
FILTER (
ALLSELECTED ( 'Data' ),
'Data'[DATE_MONTH]
< DATE ( YEAR ( currDate ), MONTH ( currDate ) - 13, DAY ( currDate ) )
)
)
VAR EndofCalc =
CALCULATE (
MAX ( 'Data'[DATE_MONTH] ),
FILTER (
ALLSELECTED ( 'Data' ),
'Data'[DATE_MONTH]
< DATE ( YEAR ( currDate ), MONTH ( currDate ) - 1, DAY ( currDate ) )
)
)
RETURN
CALCULATE (
COUNT ( 'Data'[ID_NBR] ),
FILTER (
ALL ( 'Data' ),
'Data'[DATE_MONTH] >= DATEVALUE ( StartofCalc )
&& 'Data'[DATE_MONTH] <= DATEVALUE ( EndofCalc )
&& 'Data'[Status] = "Active"
)
)
Regards,
Xiaoxin Sheng
Thanks Xiaoxin for the response, this looks good.
But I have a measure below that works and did the job 👍
Measure =
VAR EndDate =
EDATE ( MAX ( 'Data'[DATE_MONTH] ), -1 ) + 1 -- adjusts End Date to previous month (so pulls excluding current month)
VAR StartDate =
EDATE ( EndDate, -12 ) + 1 -- sets Start Date 12 months prior to End Date above
VAR TotalActive =
CALCULATE (
COUNTROWS (
FILTER ( 'Data', 'Data'[Status] = "Active" )
),
-- counts rows filtered for Active only in table rather than all rows
DATESBETWEEN (
'Data'[DATE_MONTH],
-- indicates column for dates between
StartDate,
-- Start Date defined above --
EndDate -- End Date defined above --
)
)
VAR TotalInactive =
CALCULATE (
COUNTROWS ( FILTER ( 'Lapse Data', 'Lapse Data'[Status] = "Inactive" ) ),
-- counts rows filtered for all Inactive only in table rather than all rows
DATESBETWEEN (
'Data'[DATE_MONTH],
-- indicates column for dates between
StartDate,
-- Start Date defined above --
EndDate -- End Date defined above --
)
)
RETURN
CALCULATE ( DIVIDE ( TotalActive, TotalInactive, 0 ) )