Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
gautampruthi
Helper II
Helper II

Measure to calculate occurrence of a character

Hi,

I have a table named srs_cap with columns that contain decision information (cap_dec1, cap_dec2, cap_dec3) and the corresponding dates when those decisions were made (cap_decd, cap_dcd2, cap_dcd3).

  • cap_dec1 corresponds to cap_decd,
  • cap_dec2 corresponds to cap_dcd2,
  • cap_dec3 corresponds to cap_dcd3.

The logic is as follows: Identify the most recent decision date from cap_decd, cap_dcd2, and cap_dcd3 that is on or before same date last year. If the corresponding decision (cap_dec1, cap_dec2, or cap_dec3) has the value 'C', then count the entries in cap_stuc.


i am using the below two measure one to check occurrence of C and second one to check U

Conditional_Count =
SUMX(
    srs_cap,
    VAR CutoffDate = DATE(YEAR(TODAY()) - 1, MONTH(TODAY()), DAY(TODAY()))
    VAR Date1 = IF(AND([cap_decd] <= CutoffDate, NOT(ISBLANK([cap_decd]))), [cap_decd], BLANK())
    VAR Date2 = IF(AND([cap_dcd2] <= CutoffDate, NOT(ISBLANK([cap_dcd2]))), [cap_dcd2], BLANK())
    VAR Date3 = IF(AND([cap_dcd3] <= CutoffDate, NOT(ISBLANK([cap_dcd3]))), [cap_dcd3], BLANK())
    VAR MostRecentDate = MAXX(
        { Date1, Date2, Date3 },
        [Value]
    )
    VAR IsDecisionC = SWITCH(
        TRUE,
        MostRecentDate = Date1 && [cap_dec1] = "C", 1,
        MostRecentDate = Date2 && [cap_dec2] = "C", 1,
        MostRecentDate = Date3 && [cap_dec3] = "C", 1,
        0
    )
    RETURN
        IF(IsDecisionC = 1, 1, 0)
)

The same logic but now we are looking for U instead of C
UnConditional_Count =
SUMX(
    srs_cap,
    VAR CutoffDate = DATE(YEAR(TODAY()) - 1, MONTH(TODAY()), DAY(TODAY()))
    VAR Date1 = IF(AND([cap_decd] <= CutoffDate, NOT(ISBLANK([cap_decd]))), [cap_decd], BLANK())
    VAR Date2 = IF(AND([cap_dcd2] <= CutoffDate, NOT(ISBLANK([cap_dcd2]))), [cap_dcd2], BLANK())
    VAR Date3 = IF(AND([cap_dcd3] <= CutoffDate, NOT(ISBLANK([cap_dcd3]))), [cap_dcd3], BLANK())
    VAR MostRecentDate = MAXX(
        { Date1, Date2, Date3 },
        [Value]
    )
    VAR IsDecisionU = SWITCH(
        TRUE,
        MostRecentDate = Date1 && [cap_dec1] = "U", 1,
        MostRecentDate = Date2 && [cap_dec2] = "U", 1,
        MostRecentDate = Date3 && [cap_dec3] = "U", 1,
        0
    )
    RETURN
        IF(IsDecisionU = 1, 1, 0)
)


But getting incorrect results, Sample data along with correct output and measure output is provided below, I am considering today data as cut off date (08/11/2023)
cap_stuccap_dec1cap_decdcap_dec2cap_dcd2cap_dec3cap_dcd3Correct outputConditional_CountUnConditional_Count
80171C01/08/2023 00:00U17/01/2024 00:00  Conditional10
80335C28/11/2023 00:00U09/01/2024 00:00   11
103278       00
132353C14/06/2023 00:00    Conditional10
160112C09/01/2024 00:00U18/01/2024 00:00   11
173991C01/08/2023 00:00U24/11/2023 00:00  Conditional10
190815       00
191899R07/11/2023 00:00     00
191925C13/09/2023 00:00U17/01/2024 00:00  Conditional10
190740C12/01/2023 00:00    Conditional10
19088R13/09/2023 00:00     00
200052       00
200169       00
200269C21/09/2023 00:00U17/01/2024 00:00  Conditional10
200284C11/05/2023 00:00    Conditional10
200301C01/08/2023 00:00U18/12/2023 00:00  Conditional10
200347C11/05/2023 00:00U17/11/2023 00:00  Conditional10
210582C13/09/2023 00:00U14/09/2023 00:00  Unconditional01
220366R04/08/2023 00:00     00
220371C14/06/2023 00:00U25/07/2023 00:00  Unconditional01
1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@gautampruthi No idea where the 9 comes from. Likely, you should unpivot those three columns to make the calculation easier. Otherwise, you could use something like MC Aggregations: Multi-Column Aggregations (MC Aggregations) - Microsoft Fabric Community



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
gautampruthi
Helper II
Helper II

i managed to get the desired output by changing the measure as

Conditional_Count =
SUMX(
    srs_cap,
    VAR CutoffDate = DATE(YEAR(TODAY()) - 1, MONTH(TODAY()), DAY(TODAY()))

    // Filter each date to ensure it's on or before the cutoff date
    VAR Date1 = IF([cap_decd] <= CutoffDate, [cap_decd], BLANK())
    VAR Date2 = IF([cap_dcd2] <= CutoffDate, [cap_dcd2], BLANK())
    VAR Date3 = IF([cap_dcd3] <= CutoffDate, [cap_dcd3], BLANK())
   
    // Find the latest date that is on or before the cutoff date
    VAR MostRecentDate = MAXX(
        FILTER(
            { Date1, Date2, Date3 },
            NOT(ISBLANK([Value]))
        ),
        [Value]
    )

    // Check if a valid MostRecentDate was found (i.e., it's not blank)
    VAR IsDecisionC = IF(
        NOT(ISBLANK(MostRecentDate)),
        SWITCH(
            TRUE,
            MostRecentDate = Date1 && [cap_dec1] = "C", 1,
            MostRecentDate = Date2 && [cap_dec2] = "C", 1,
            MostRecentDate = Date3 && [cap_dec3] = "C", 1,
            0
        ),
        0 // If MostRecentDate is blank, set IsDecisionC to 0
    )
   
    RETURN
        IF(IsDecisionC = 1, 1, 0)
)
Anonymous
Not applicable

Hi,@gautampruthi 
We are delighted that you have found a solution and are willing to share it.

 

Accepting your post as the solution is incredibly helpful to our community, as it enables members with similar issues to find answers more quickly.

 

Thank you for your valuable contribution to the community, and we wish you all the best in your work.

 

Best Regards,

Leroy Lu

gautampruthi
Helper II
Helper II

Hi @Greg_Deckler Thank you for looking into this.
i have added more information along with the measure i am using and output i am getting.


Greg_Deckler
Super User
Super User

@gautampruthi No idea where the 9 comes from. Likely, you should unpivot those three columns to make the calculation easier. Otherwise, you could use something like MC Aggregations: Multi-Column Aggregations (MC Aggregations) - Microsoft Fabric Community



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.