Forum Discussion

ManuBK's avatar
ManuBK
Frequent Visitor
5 months ago
Solved

Identifying State history Count based on date slicer

Hi ,

 

I have a requirement where in i need to identify the Old state count based on the date slicer.

 

I need to indentify the state count based on the date selection on that period 

 

For example 1- If i select a date range from a slicer date as 3-9-2026 to 3-11-2026 then my state count for  Onhold should be 1 as In this date range Onhold is falling under

 

Example 2 - If i select date range from 3-14-2026 to 3-16-2026 then it should show 1 count under Inprogress 

 

Could you please help me on this 

PowerBI superuser mh2587 

 

IDOld StateNew StateStart dateNew State Date
1111 New3/2/20263/2/2026
1111NewInprogress3/2/20263/4/2026
1111InprogressInprogress3/4/20263/5/2026
1111InprogressOnhold3/5/20263/10/2026
1111OnholdOnhold3/10/20263/12/2026
1111OnholdOnhold3/12/20263/12/2026
1111OnholdIn progress3/12/20263/13/2026
1111InprogressCompleted3/13/20263/14/2026
2222 New3/14/20263/14/2026
2222NewInprogress3/14/20263/15/2026
2222InprogressInprogress3/15/20263/15/2026
2222InprogressOnhold3/16/20263/17/2026
2222OnholdInprogress3/17/20263/19/2026
2222InprogressOnhold3/19/20263/21/2026
2222OnholdIn progress3/21/20263/23/2026
2222InprogressCompleted3/23/20263/27/2026

8 Replies

  • Hello,

    I guess that the issue here is you’re treating states as events, while your logic really needs intervals, each state is valid from one date until the next change so instead of counting rows, you should build a “valid from / valid to” range per row, basically Start = New State Date, End = next New State Date (per ID), then your slicer should check if the selected period overlaps that interval in DAX terms, something like checking if Start <= slicer max date and End >= slicer min date, that way you count the state if it was active at any point in the selected range if you don’t do this, Power BI just counts transitions, not the actual state during the period, which is why your results feel off I’d try first creating a calculated column for the “next date” per ID (using LEAD logic or EARLIER pattern), then build the measure on top of that, much easier to control

    Best regards,
    Daniele

  • Hi ManuBK 

    From your examples, it looks like you do not want to count only the rows where the status changed inside the selected dates. You want to count IDs whose state was active at any point during the selected date range.

    So the logic should be based on overlap between the slicer period and the status period. In other words, a state should be counted when its start date is on or before the end of the selected range, and its end date is on or after the start of the selected range.

    A measure like this should be closer to what you need:

    State Count In Selected Period =
    VAR MinSelDate = MIN ( 'Calendar'[Date] )
    VAR MaxSelDate = MAX ( 'Calendar'[Date] )
    RETURN
    CALCULATE (
    DISTINCTCOUNT ( History[ID] ),
    FILTER (
    ALL ( History ),
    History[Start date] <= MaxSelDate &&
    History[New State Date] >= MinSelDate
    )
    )

    If your visual already uses New State on rows, this measure should return the count for each state automatically.

    One thing to verify is whether New State Date represents the last day of the current state or the first day of the next state, because that affects whether the end boundary should be inclusive or exclusive.

    Also, if your Calendar table is actively related to only one date column in the history table, that relationship can sometimes interfere with this kind of logic. In that case, using a disconnected date table for the slicer is often a cleaner approach.

    If possible, please also share a small sample PBIX or Excel file together with the exact expected result for a few selected date ranges. A public cloud link such as OneDrive, Google Drive, or Dropbox would make it much easier for others to reproduce the scenario and give you a precise solution.

    If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly

  • ManuBK 

     

    Old State Count = 
    VAR SlicerMin = MIN('Date'[Date])
    VAR SlicerMax = MAX('Date'[Date])
    RETURN
    COUNTROWS(
    FILTER(
    ADDCOLUMNS(
    VALUES('Table'[ID]),
    "@State",
    CALCULATE(
    MAX('Table'[Old State]),
    'Table'[Start date] <= SlicerMax,
    'Table'[New State Date] >= SlicerMin
    )
    ),
    [@State] = SELECTEDVALUE('State'[State])
    )
    )

    Counts IDs where state record overlaps slicer range

  • HI ManuBK,

    Checking in to see if your issue has been resolved. let us know if you still need any assistance.

     

    Thank you.

    • ManuBK's avatar
      ManuBK
      Frequent Visitor

      Hi v-saisrao-msft Issue has been resolved. Thank you soo much for your help . Really appreciated