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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Find unique ids that appear in one year and not the other, and vice versa.

I'm still sort of new to DAX and I have a problem, wasn't sure exactly where to start.  If I have a situation where I have a table of Sales person ids, and I want to see which ones exist as of this year, but didn't exist in the same set last year, and need an additional measure of sales people that existed last year but no longer do this year, in SQL I can do a Count distinct and exists/not exists to get that. I'm really stuck on where to get started, I keep getting stuck thinking in SQL instead of DAX.  

So just to be clear, if my table was Sales, with a SalepersonId and SalesDate (a few other ones I don't really care about), and I wanted to see which salespeople's id had values for Last year, but not this year and then vice versa, another measure, does anyone have any pointers on where I'd get started?  I was able to use COUNTDISTINCT to get this year, but I can't use PREVIOUSYEAR b/c those people may exist in both and I'm trying to basically get the ones that aren't in both sets for the previous period and the current one if that makes sense?  I've been stumbling around with ALLEXCEPT and a few others but feel I'm just getting more lost.  Anyone have any ideas they'd care to share?

1 ACCEPTED SOLUTION

@Anonymous 

If you actually want the IDs rather than counting them, create calculate tables:

In2020NotIn2019_T =
    EXCEPT (
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2020 ),
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2019 )
    )

 

In2019NotIn2020_T =
    EXCEPT (
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2019 ),
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2020 )
    )

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

View solution in original post

2 REPLIES 2
AlB
Super User
Super User

@Anonymous 

Probably best to use a calendar table. If you don't have one, try something like htese measures in card visuals:

 

In2020NotIn2019 =
COUNTROWS (
    EXCEPT (
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2020 ),
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2019 )
    )
)

 

In2019NotIn2020 =
COUNTROWS (
    EXCEPT (
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2019 ),
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2020 )
    )
)

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

@Anonymous 

If you actually want the IDs rather than counting them, create calculate tables:

In2020NotIn2019_T =
    EXCEPT (
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2020 ),
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2019 )
    )

 

In2019NotIn2020_T =
    EXCEPT (
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2019 ),
        CALCULATETABLE ( DISTINCT ( Table1[ID] ), YEAR ( Table1[SalesDate] ) = 2020 )
    )

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors