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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
Anonymous
Not applicable

Counting matches within a table based on two other columns

Hi All,

 

I have a table of history data (one row per event) that looks like:

PBI Table Search.png

 

The _primarykey here is actually that of a parent table, not the history table... the parent item can have multiple rows in the history table.

 

What I'm looking for is the DAX to populate the "Renewed" column, based on finding another row (for the same parent primary key) that has a 'Start Date' that matches the 'End Date' of the current row.

 

I've tried the following as a calculated column, but i get a syntax error:

 

Renewed =
VAR vkey = history[_primarykey]
VAR vdate = history[End Date]
RETURN
    IF (
        COUNTROWS (
            FILTER (
                FILTER ( FILTER ( history, ALL ), history[_primarykey] = vkey ),
                history[Start Date] = vdate
            )
        )
            > 0,
        "Yes",
        "No"
    )

 

The logic being to expand the table to ALL rows, then filter back down to match the (parent) primary key, and then the dates, and to count the rows of that table... if the count is greater than zero then 'Renewed' = "Yes".

 

All help appreciated.

1 ACCEPTED SOLUTION

@Anonymous

 

This Column should work as well

 

Renewed =
VAR myenddate = Table1[End Date]
RETURN
    IF (
        CONTAINS (
            FILTER ( Table1, Table1[_primarykey] = EARLIER ( Table1[_primarykey] ) ),
            Table1[Start Date], myenddate
        ),
        "Yes",
        "No"
    )

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

Why not just:

 

Renewed Column = IF([EndDate] IN ALL(Matches[StartDate]),"Yes","No")


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...
Anonymous
Not applicable

Hi @Greg_Deckler... well I tried to do it with table filters and counts because I hadn't yet come across the IN operator...

 

The solution suggested doesn't take into account the match on the parent _primarykey also though... I need to match two items between the rows. Does IN handle multiple columns?

OK, try this then:

 

Renewed = IF([EndDate] IN FILTER(ALL(Matches[StartDate]),[_primarykey]=EARLIER([_primarykey])),"Yes","No")


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...

@Anonymous

 

This Column should work as well

 

Renewed =
VAR myenddate = Table1[End Date]
RETURN
    IF (
        CONTAINS (
            FILTER ( Table1, Table1[_primarykey] = EARLIER ( Table1[_primarykey] ) ),
            Table1[Start Date], myenddate
        ),
        "Yes",
        "No"
    )

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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